CChengzhongWnd.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "../pch/pch.h"
  2. #include "CChengzhongWnd.h"
  3. #include "../worker/CChengzhongWorker.h"
  4. LRESULT CChengzhongWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  5. {
  6. POINT pt;
  7. pt.x = GET_X_LPARAM(lParam);
  8. pt.y = GET_Y_LPARAM(lParam);
  9. ::ScreenToClient(*this, &pt);
  10. RECT rcClient;
  11. ::GetClientRect(*this, &rcClient);
  12. RECT rcCaption = m_pm.GetCaptionRect();
  13. if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  14. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom)
  15. {
  16. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  17. if (pControl && _tcscmp(pControl->GetClass(), L"ButtonUI") != 0)
  18. {
  19. return HTCAPTION;
  20. }
  21. }
  22. return HTCLIENT;
  23. }
  24. LRESULT CChengzhongWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  25. {
  26. SIZE szRoundCorner = m_pm.GetRoundCorner();
  27. if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0))
  28. {
  29. CDuiRect rcWnd;
  30. ::GetWindowRect(*this, &rcWnd);
  31. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  32. rcWnd.right++;
  33. rcWnd.bottom++;
  34. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  35. ::SetWindowRgn(*this, hRgn, TRUE);
  36. ::DeleteObject(hRgn);
  37. }
  38. bHandled = FALSE;
  39. return 0;
  40. }
  41. LRESULT CChengzhongWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  42. {
  43. LRESULT lRes = 0;
  44. BOOL bHandled = TRUE;
  45. switch (uMsg)
  46. {
  47. case WM_CREATE:
  48. lRes = OnCreate(uMsg, wParam, lParam, bHandled);
  49. break;
  50. case WM_CLOSE:
  51. lRes = OnClose(uMsg, wParam, lParam, bHandled);
  52. break;
  53. case WM_NCACTIVATE:
  54. lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
  55. break;
  56. case WM_NCCALCSIZE:
  57. lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
  58. break;
  59. case WM_NCPAINT:
  60. lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
  61. break;
  62. case WM_NCHITTEST:
  63. lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
  64. break;
  65. case WM_SIZE:
  66. lRes = OnSize(uMsg, wParam, lParam, bHandled);
  67. break;
  68. case WM_CHENGZHONG_SUCCESS:
  69. lRes = OnChengzhongSuccess(uMsg, wParam, lParam, bHandled);
  70. break;
  71. default:
  72. bHandled = FALSE;
  73. }
  74. if (bHandled)
  75. {
  76. return lRes;
  77. }
  78. if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes))
  79. {
  80. return lRes;
  81. }
  82. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  83. }
  84. LRESULT CChengzhongWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  85. {
  86. if (uMsg == WM_KEYDOWN)
  87. {
  88. if (wParam == VK_RETURN)
  89. {
  90. this->SaveWeight();
  91. }
  92. else if (wParam == VK_ESCAPE)
  93. {
  94. Quit();
  95. }
  96. }
  97. return false;
  98. }
  99. LRESULT CChengzhongWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  100. {
  101. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  102. styleValue &= ~WS_CAPTION;
  103. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  104. // 把自己的窗口句柄与窗口绘制管理器挂接在一起
  105. m_pm.Init(m_hWnd);
  106. m_pm.AddPreMessageFilter(this);
  107. CDialogBuilder builder;
  108. CControlUI* pRoot = builder.Create(_T("chengzhong_wnd.xml"), (UINT)0, NULL, &m_pm);
  109. ASSERT(pRoot && "Failed to parse XML");
  110. // 把这些控件绘制到本窗口上
  111. m_pm.AttachDialog(pRoot);
  112. // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
  113. m_pm.AddNotifier(this);
  114. Init();
  115. return 0;
  116. }
  117. LRESULT CChengzhongWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  118. {
  119. bHandled = FALSE;
  120. return 0;
  121. }
  122. void CChengzhongWnd::Notify(TNotifyUI& msg)
  123. {
  124. if (msg.sType == _T("click"))
  125. {
  126. DuiLib::CDuiString senderName = msg.pSender->GetName();
  127. if (senderName == _T("closebtn"))
  128. {
  129. Quit();
  130. return;
  131. }
  132. else if (senderName == _T("save"))
  133. {
  134. this->SaveWeight();
  135. }
  136. else if (senderName == L"jianpan_1")
  137. {
  138. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  139. std::wstring wsReason = pContent->GetText();
  140. wsReason += L"1";
  141. pContent->SetText(wsReason.c_str());
  142. pContent->SetFocus();
  143. }
  144. else if (senderName == L"jianpan_2")
  145. {
  146. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  147. std::wstring wsReason = pContent->GetText();
  148. wsReason += L"2";
  149. pContent->SetText(wsReason.c_str());
  150. pContent->SetFocus();
  151. }
  152. else if (senderName == L"jianpan_3")
  153. {
  154. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  155. std::wstring wsReason = pContent->GetText();
  156. wsReason += L"3";
  157. pContent->SetText(wsReason.c_str());
  158. pContent->SetFocus();
  159. }
  160. else if (senderName == L"jianpan_4")
  161. {
  162. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  163. std::wstring wsReason = pContent->GetText();
  164. wsReason += L"4";
  165. pContent->SetText(wsReason.c_str());
  166. pContent->SetFocus();
  167. }
  168. else if (senderName == L"jianpan_5")
  169. {
  170. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  171. std::wstring wsReason = pContent->GetText();
  172. wsReason += L"5";
  173. pContent->SetText(wsReason.c_str());
  174. pContent->SetFocus();
  175. }
  176. else if (senderName == L"jianpan_6")
  177. {
  178. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  179. std::wstring wsReason = pContent->GetText();
  180. wsReason += L"6";
  181. pContent->SetText(wsReason.c_str());
  182. pContent->SetFocus();
  183. }
  184. else if (senderName == L"jianpan_7")
  185. {
  186. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  187. std::wstring wsReason = pContent->GetText();
  188. wsReason += L"7";
  189. pContent->SetText(wsReason.c_str());
  190. pContent->SetFocus();
  191. }
  192. else if (senderName == L"jianpan_8")
  193. {
  194. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  195. std::wstring wsReason = pContent->GetText();
  196. wsReason += L"8";
  197. pContent->SetText(wsReason.c_str());
  198. pContent->SetFocus();
  199. }
  200. else if (senderName == L"jianpan_9")
  201. {
  202. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  203. std::wstring wsReason = pContent->GetText();
  204. wsReason += L"9";
  205. pContent->SetText(wsReason.c_str());
  206. pContent->SetFocus();
  207. }
  208. else if (senderName == L"jianpan_0")
  209. {
  210. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  211. std::wstring wsReason = pContent->GetText();
  212. wsReason += L"0";
  213. pContent->SetText(wsReason.c_str());
  214. pContent->SetFocus();
  215. }
  216. else if (senderName == L"jianpan_x")
  217. {
  218. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  219. std::wstring wsReason = pContent->GetText();
  220. wsReason = wsReason.substr(0, wsReason.length() - 1);
  221. pContent->SetText(wsReason.c_str());
  222. pContent->SetFocus();
  223. }
  224. else if (senderName == L"jianpan_dian")
  225. {
  226. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  227. std::wstring wsReason = pContent->GetText();
  228. wsReason += L".";
  229. pContent->SetText(wsReason.c_str());
  230. pContent->SetFocus();
  231. }
  232. }
  233. else if (msg.sType == _T("textchanged"))
  234. {
  235. //只能输入数字,其他的全部删除
  236. CEditUI* m_pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  237. if (!m_pEdit->GetText().IsEmpty())
  238. {
  239. std::wstring ws_Value = m_pEdit->GetText();
  240. std::string strValue = CLewaimaiString::UnicodeToUTF8(ws_Value);
  241. for (size_t nIndex = 0; nIndex < strValue.length(); nIndex++)
  242. {
  243. unsigned char tmp = strValue.at(nIndex);
  244. if ((tmp > 57 || tmp < 48) && tmp != 46)
  245. {
  246. //ASCII码不是数字的都过滤掉
  247. strValue = strValue.substr(0, nIndex);
  248. m_pEdit->SetText(CLewaimaiString::UTF8ToUnicode(strValue).c_str());
  249. m_pEdit->SetSel(strValue.length(), strValue.length());//重设给光标设置位置
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. else if (msg.sType == L"windowinit")
  256. {
  257. m_pm.SetNextTabControl(false);
  258. CEditUI* pFukuanEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  259. pFukuanEdit->SetFocus();
  260. }
  261. }
  262. LRESULT CChengzhongWnd::OnChengzhongSuccess(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  263. {
  264. UpdateWeightFromWorker();
  265. bHandled = TRUE;
  266. return 0;
  267. }
  268. void CChengzhongWnd::Init()
  269. {
  270. //启动一个线程,开始串口监听
  271. CChengzhongWorker::GetInstance()->SetHWND(this->m_hWnd);
  272. if (CChengzhongWorker::GetInstance()->GetIsWork() == false)
  273. {
  274. //电子秤连接串口失败了
  275. CLabelUI* pErrorInfo = static_cast<CLabelUI*>(m_pm.FindControl(_T("errinfo")));
  276. pErrorInfo->SetText(L"连接电子秤失败,请检查电子秤设置");
  277. pErrorInfo->SetVisible(true);
  278. }
  279. //先设置一次已经存取的最新weight
  280. UpdateWeightFromWorker();
  281. }
  282. std::string CChengzhongWnd::getContent()
  283. {
  284. return m_weight;
  285. }
  286. void CChengzhongWnd::SetTitle(std::wstring title)
  287. {
  288. CLabelUI* pLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("memo_dlg_title")));
  289. pLabel->SetText(title.c_str());
  290. }
  291. void CChengzhongWnd::SetPrice(std::string price)
  292. {
  293. m_price = price;
  294. std::wstring ws_price = CLewaimaiString::UTF8ToUnicode(m_price);
  295. CLabelUI* pLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("price")));
  296. pLabel->SetText((L"单价:" + ws_price + L"元/公斤").c_str());
  297. }
  298. void CChengzhongWnd::UpdateWeightFromWorker()
  299. {
  300. m_weight = CChengzhongWorker::GetInstance()->GetWeight();
  301. if (m_weight.length() == 0)
  302. {
  303. return;
  304. }
  305. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  306. pContent->SetText(CLewaimaiString::UTF8ToUnicode(m_weight).c_str());
  307. std::wstring ws_Value = pContent->GetText();
  308. std::string strValue = CLewaimaiString::UnicodeToUTF8(ws_Value);
  309. pContent->SetFocus();
  310. pContent->SetSel(strValue.length(), strValue.length());//重设给光标设置位置
  311. }
  312. void CChengzhongWnd::SaveWeight()
  313. {
  314. CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
  315. std::wstring wsReason = pContent->GetText();
  316. m_weight = CLewaimaiString::UnicodeToUTF8(wsReason);
  317. if (m_weight == "" || atof(m_weight.c_str()) < 0.001)
  318. {
  319. CLabelUI* pErrorInfo = static_cast<CLabelUI*>(m_pm.FindControl(_T("errinfo")));
  320. pErrorInfo->SetText(L"商品重量未获取,请重新称重");
  321. pErrorInfo->SetVisible(true);
  322. return;
  323. }
  324. Close(IDOK);
  325. }
  326. void CChengzhongWnd::Quit()
  327. {
  328. Close(IDCANCEL);
  329. }