| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- #include "../pch/pch.h"
- #include "CChengzhongWnd.h"
- #include "../worker/CChengzhongWorker.h"
- LRESULT CChengzhongWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- POINT pt;
- pt.x = GET_X_LPARAM(lParam);
- pt.y = GET_Y_LPARAM(lParam);
- ::ScreenToClient(*this, &pt);
- RECT rcClient;
- ::GetClientRect(*this, &rcClient);
- RECT rcCaption = m_pm.GetCaptionRect();
- if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
- && pt.y >= rcCaption.top && pt.y < rcCaption.bottom)
- {
- CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
- if (pControl && _tcscmp(pControl->GetClass(), L"ButtonUI") != 0)
- {
- return HTCAPTION;
- }
- }
- return HTCLIENT;
- }
- LRESULT CChengzhongWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- SIZE szRoundCorner = m_pm.GetRoundCorner();
- if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0))
- {
- CDuiRect rcWnd;
- ::GetWindowRect(*this, &rcWnd);
- rcWnd.Offset(-rcWnd.left, -rcWnd.top);
- rcWnd.right++;
- rcWnd.bottom++;
- HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
- ::SetWindowRgn(*this, hRgn, TRUE);
- ::DeleteObject(hRgn);
- }
- bHandled = FALSE;
- return 0;
- }
- LRESULT CChengzhongWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- LRESULT lRes = 0;
- BOOL bHandled = TRUE;
- switch (uMsg)
- {
- case WM_CREATE:
- lRes = OnCreate(uMsg, wParam, lParam, bHandled);
- break;
- case WM_CLOSE:
- lRes = OnClose(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCACTIVATE:
- lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCCALCSIZE:
- lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCPAINT:
- lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
- break;
- case WM_NCHITTEST:
- lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
- break;
- case WM_SIZE:
- lRes = OnSize(uMsg, wParam, lParam, bHandled);
- break;
- case WM_CHENGZHONG_SUCCESS:
- lRes = OnChengzhongSuccess(uMsg, wParam, lParam, bHandled);
- break;
- default:
- bHandled = FALSE;
- }
- if (bHandled)
- {
- return lRes;
- }
- if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes))
- {
- return lRes;
- }
- return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
- }
- LRESULT CChengzhongWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
- {
- if (uMsg == WM_KEYDOWN)
- {
- if (wParam == VK_RETURN)
- {
- this->SaveWeight();
- }
- else if (wParam == VK_ESCAPE)
- {
- Quit();
- }
- }
- return false;
- }
- LRESULT CChengzhongWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
- styleValue &= ~WS_CAPTION;
- ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
- // 把自己的窗口句柄与窗口绘制管理器挂接在一起
- m_pm.Init(m_hWnd);
- m_pm.AddPreMessageFilter(this);
- CDialogBuilder builder;
- CControlUI* pRoot = builder.Create(_T("chengzhong_wnd.xml"), (UINT)0, NULL, &m_pm);
- ASSERT(pRoot && "Failed to parse XML");
- // 把这些控件绘制到本窗口上
- m_pm.AttachDialog(pRoot);
- // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
- m_pm.AddNotifier(this);
- Init();
- return 0;
- }
- LRESULT CChengzhongWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- bHandled = FALSE;
- return 0;
- }
- void CChengzhongWnd::Notify(TNotifyUI& msg)
- {
- if (msg.sType == _T("click"))
- {
- DuiLib::CDuiString senderName = msg.pSender->GetName();
- if (senderName == _T("closebtn"))
- {
- Quit();
- return;
- }
- else if (senderName == _T("save"))
- {
- this->SaveWeight();
- }
- else if (senderName == L"jianpan_1")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"1";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_2")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"2";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_3")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"3";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_4")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"4";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_5")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"5";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_6")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"6";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_7")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"7";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_8")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"8";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_9")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"9";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_0")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L"0";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_x")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason = wsReason.substr(0, wsReason.length() - 1);
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- else if (senderName == L"jianpan_dian")
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- wsReason += L".";
- pContent->SetText(wsReason.c_str());
- pContent->SetFocus();
- }
- }
- else if (msg.sType == _T("textchanged"))
- {
- //只能输入数字,其他的全部删除
- CEditUI* m_pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- if (!m_pEdit->GetText().IsEmpty())
- {
- std::wstring ws_Value = m_pEdit->GetText();
- std::string strValue = CLewaimaiString::UnicodeToUTF8(ws_Value);
- for (size_t nIndex = 0; nIndex < strValue.length(); nIndex++)
- {
- unsigned char tmp = strValue.at(nIndex);
- if ((tmp > 57 || tmp < 48) && tmp != 46)
- {
- //ASCII码不是数字的都过滤掉
- strValue = strValue.substr(0, nIndex);
- m_pEdit->SetText(CLewaimaiString::UTF8ToUnicode(strValue).c_str());
- m_pEdit->SetSel(strValue.length(), strValue.length());//重设给光标设置位置
- break;
- }
- }
- }
- }
- else if (msg.sType == L"windowinit")
- {
- m_pm.SetNextTabControl(false);
- CEditUI* pFukuanEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- pFukuanEdit->SetFocus();
- }
- }
- LRESULT CChengzhongWnd::OnChengzhongSuccess(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- UpdateWeightFromWorker();
- bHandled = TRUE;
- return 0;
- }
- void CChengzhongWnd::Init()
- {
- //启动一个线程,开始串口监听
- CChengzhongWorker::GetInstance()->SetHWND(this->m_hWnd);
- if (CChengzhongWorker::GetInstance()->GetIsWork() == false)
- {
- //电子秤连接串口失败了
- CLabelUI* pErrorInfo = static_cast<CLabelUI*>(m_pm.FindControl(_T("errinfo")));
- pErrorInfo->SetText(L"连接电子秤失败,请检查电子秤设置");
- pErrorInfo->SetVisible(true);
- }
- //先设置一次已经存取的最新weight
- UpdateWeightFromWorker();
- }
- std::string CChengzhongWnd::getContent()
- {
- return m_weight;
- }
- void CChengzhongWnd::SetTitle(std::wstring title)
- {
- CLabelUI* pLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("memo_dlg_title")));
- pLabel->SetText(title.c_str());
- }
- void CChengzhongWnd::SetPrice(std::string price)
- {
- m_price = price;
- std::wstring ws_price = CLewaimaiString::UTF8ToUnicode(m_price);
- CLabelUI* pLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("price")));
- pLabel->SetText((L"单价:" + ws_price + L"元/公斤").c_str());
- }
- void CChengzhongWnd::UpdateWeightFromWorker()
- {
- m_weight = CChengzhongWorker::GetInstance()->GetWeight();
- if (m_weight.length() == 0)
- {
- return;
- }
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- pContent->SetText(CLewaimaiString::UTF8ToUnicode(m_weight).c_str());
- std::wstring ws_Value = pContent->GetText();
- std::string strValue = CLewaimaiString::UnicodeToUTF8(ws_Value);
- pContent->SetFocus();
- pContent->SetSel(strValue.length(), strValue.length());//重设给光标设置位置
- }
- void CChengzhongWnd::SaveWeight()
- {
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
- std::wstring wsReason = pContent->GetText();
- m_weight = CLewaimaiString::UnicodeToUTF8(wsReason);
- if (m_weight == "" || atof(m_weight.c_str()) < 0.001)
- {
- CLabelUI* pErrorInfo = static_cast<CLabelUI*>(m_pm.FindControl(_T("errinfo")));
- pErrorInfo->SetText(L"商品重量未获取,请重新称重");
- pErrorInfo->SetVisible(true);
- return;
- }
- Close(IDOK);
- }
- void CChengzhongWnd::Quit()
- {
- Close(IDCANCEL);
- }
|