CMainWnd.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. #include "../pch/pch.h"
  2. #include "CMainWnd.h"
  3. #include "CLoginWnd.h"
  4. #include "../control/ControlEx.h"
  5. #include "../page/CWaimaiOrderInfoPageUI.h"
  6. #include "../network/CMessagePush.h"
  7. #include "../print/CPosPrinter.h"
  8. #include <urlmon.h>
  9. #include "../wnd/CToastWnd.h"
  10. #include "../tool/CChengzhongWorker.h"
  11. void CMainWnd::Init()
  12. {
  13. //设置店铺名字
  14. CLabelUI* pShopname = static_cast<CLabelUI*>(m_pm.FindControl(_T("main_shopname")));
  15. pShopname->SetText(CLewaimaiString::UTF8ToUnicode(CShopinfo::m_shopinfo.m_shop_name).c_str());
  16. m_curOptionName = L"main_diandan";
  17. m_curOption = static_cast<CControlUI*>(m_pm.FindControl(_T("main_diandan")));
  18. m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("closebtn")));
  19. m_pMaxBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("maxbtn")));
  20. m_pRestoreBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("restorebtn")));
  21. m_pMinBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("minbtn")));
  22. //默认选择点单页面
  23. this->SwitchPage(DIANDAN);
  24. //开启键盘输入监听
  25. RegKeyboardRawInput();
  26. //登录成功,启动消息和任务处理
  27. m_push = new CMessagePush(m_hWnd);
  28. m_push->Start();
  29. //启动打印队列
  30. CPosPrinterQueue::GetInstance()->StartWork();
  31. //启动称重任务
  32. CChengzhongWorker::GetInstance()->StartWork();
  33. //启动一个线程,开始同步商品图片
  34. std::thread(&CMainWnd::UpdateFoodImage, this).detach();
  35. }
  36. void CMainWnd::UpdateFoodImage()
  37. {
  38. //先判断并创建临时目录
  39. string folderPath = CLewaimaiString::UnicodeToANSI(CSystem::GetProgramDir()) + "\\tmp\\image";
  40. std::wstring ws_folderPath = CLewaimaiString::ANSIToUnicode(folderPath);
  41. if (!CSystem::IsDirExist(ws_folderPath))
  42. {
  43. LOG_INFO("folderPath:" << folderPath.c_str() << ",没有找到对应的目录,即将创建");
  44. CSystem::CreateMultiLevel(folderPath);
  45. }
  46. CSqlite3 sqlite;
  47. std::vector<CFood> foodlist = sqlite.GetFoodByTypeid("0", true);
  48. for (std::vector<CFood>::iterator it = foodlist.begin(); it != foodlist.end(); it++)
  49. {
  50. CFood food = *it;
  51. if (food.goods_img.size() == 0)
  52. {
  53. //没有图片,直接跳过
  54. continue;
  55. }
  56. std::wstring ws_goods_img = food.getImageUrl();
  57. ws_goods_img += L"!max200"; //下载小图
  58. wstring imagePath = food.getImageTmpPath();
  59. if (CSystem::IsFileExist(imagePath))
  60. {
  61. //如果图片文件已经存在,直接跳过
  62. continue;
  63. }
  64. //图片还不存在,开始下载
  65. if (URLDownloadToFile(NULL, ws_goods_img.c_str(), imagePath.c_str(), 0, NULL) == S_OK)
  66. {
  67. //图片下载成功了,发个消息,更新图片
  68. }
  69. else
  70. {
  71. LOG_INFO("URLDownloadToFile Fail,Error"<<GetLastError());
  72. }
  73. }
  74. //再处理商品套餐的图片
  75. std::vector<CFoodpackage> foodpackagelist = sqlite.GetFoodpackages(true);
  76. for (std::vector<CFoodpackage>::iterator it = foodpackagelist.begin(); it != foodpackagelist.end(); it++)
  77. {
  78. CFoodpackage food = *it;
  79. if (food.goods_img.size() == 0)
  80. {
  81. //没有图片,直接跳过
  82. continue;
  83. }
  84. std::wstring ws_goods_img = food.getImageUrl();
  85. ws_goods_img += L"!max200"; //下载小图
  86. wstring imagePath = food.getImageTmpPath();
  87. if (CSystem::IsFileExist(imagePath))
  88. {
  89. //如果图片文件已经存在,直接跳过
  90. continue;
  91. }
  92. //图片还不存在,开始下载
  93. if (URLDownloadToFile(NULL, ws_goods_img.c_str(), imagePath.c_str(), 0, NULL) == S_OK)
  94. {
  95. //图片下载成功了,发个消息,更新图片
  96. }
  97. else
  98. {
  99. LOG_INFO("URLDownloadToFile Fail,Error" << GetLastError());
  100. }
  101. }
  102. }
  103. void CMainWnd::SwitchPage(MainPageName name)
  104. {
  105. if (m_curPageName == name)
  106. {
  107. return;
  108. }
  109. //先删除现在的子对象
  110. CContainerUI* pMainContentLayout = static_cast<CContainerUI*>(m_pm.FindControl(_T("main_content_layout")));
  111. pMainContentLayout->RemoveAll();
  112. //再创建一个对象
  113. CDialogBuilder builder;
  114. CDialogBuilderCallbackEx cb;
  115. CBasePageUI* pChildContainer = NULL;
  116. //注意事项:所有的子页面中的option不能带有selected="true"属性,如果带有这个属性,在下面的Create调用的时候就直接会生成一个selectchanged事件,但是这个时候
  117. //页面才刚刚Create,还没加入到窗口中,相关的变量对应关系也还没设置成功,会导致事件处理对象出现问题
  118. if (name == DIANDAN)
  119. {
  120. pChildContainer = static_cast<CBasePageUI*>(builder.Create(_T("diandan_page.xml"), (UINT)0, &cb, &m_pm));
  121. }
  122. else if (name == DINGDAN)
  123. {
  124. pChildContainer = static_cast<CBasePageUI*>(builder.Create(_T("waimaiorder_list_page.xml"), (UINT)0, &cb, &m_pm));
  125. }
  126. else if (name == SHEZHI)
  127. {
  128. pChildContainer = static_cast<CBasePageUI*>(builder.Create(_T("setting_page.xml"), (UINT)0, &cb, &m_pm));
  129. }
  130. else if (name == WAIMAIINFO)
  131. {
  132. pChildContainer = static_cast<CBasePageUI*>(builder.Create(_T("waimaiorder_info_page.xml"), (UINT)0, &cb, &m_pm));
  133. CWaimaiOrderInfoPageUI* pInfoPage = static_cast<CWaimaiOrderInfoPageUI*>(pChildContainer);
  134. pInfoPage->m_order_id = m_infopage_waimaiorder_id;
  135. pInfoPage->m_order_no = m_infopage_waimaiorder_no;
  136. }
  137. pChildContainer->SetMainWnd(this);
  138. pMainContentLayout->Add(pChildContainer);
  139. m_curPageName = name;
  140. m_curPageUI = pChildContainer;
  141. pChildContainer->InitShow();
  142. }
  143. LRESULT CMainWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  144. {
  145. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  146. styleValue &= ~WS_CAPTION;
  147. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  148. m_pm.Init(m_hWnd);
  149. CDialogBuilder builder;
  150. CDialogBuilderCallbackEx cb;
  151. CControlUI* pRoot = builder.Create(_T("main.xml"), (UINT)0, &cb, &m_pm);
  152. ASSERT(pRoot && "Failed to parse XML");
  153. m_pm.AttachDialog(pRoot);
  154. m_pm.AddNotifier(this);
  155. Init();
  156. return 0;
  157. }
  158. void CMainWnd::Notify(TNotifyUI& msg)
  159. {
  160. if(msg.sType == _T("windowinit"))
  161. {
  162. }
  163. else if(msg.sType == _T("click"))
  164. {
  165. HandleClickMsg(msg);
  166. }
  167. else if(msg.sType == _T("selectchanged"))
  168. {
  169. HandleSelectChangeMsg(msg);
  170. }
  171. else if(msg.sType == _T("itemclick"))
  172. {
  173. }
  174. else if(msg.sType == _T("itemactivate"))
  175. {
  176. }
  177. else if(msg.sType == _T("itemselect"))
  178. {
  179. HandleItemSelectMsg(msg);
  180. }
  181. else if (msg.sType == _T("textchanged"))
  182. {
  183. //编辑框内容改变事件
  184. HandleTextChangedMsg(msg);
  185. }
  186. }
  187. void CMainWnd::HandleClickMsg(TNotifyUI& msg)
  188. {
  189. if(msg.pSender == m_pCloseBtn)
  190. {
  191. if(CSetting::GetParam("setting_is_close_min") == "1")
  192. {
  193. AddTrayIcon();
  194. }
  195. else
  196. {
  197. PostQuitMessage(0);
  198. }
  199. return;
  200. }
  201. else if(msg.pSender == m_pMinBtn)
  202. {
  203. SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
  204. return;
  205. }
  206. else if(msg.pSender == m_pMaxBtn)
  207. {
  208. SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  209. return;
  210. }
  211. else if(msg.pSender == m_pRestoreBtn)
  212. {
  213. SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
  214. return;
  215. }
  216. CDuiString name = msg.pSender->GetName();
  217. if(name == _T("quitbtn"))
  218. {
  219. /*Close()*/
  220. PostQuitMessage(0); // 因为activex的原因,使用close可能会出现错误
  221. }
  222. else
  223. {
  224. this->m_curPageUI->HandleClickMsg(msg);
  225. }
  226. }
  227. /**
  228. * 这个处理Option的切换
  229. */
  230. void CMainWnd::HandleSelectChangeMsg(TNotifyUI& msg)
  231. {
  232. CDuiString name = msg.pSender->GetName();
  233. std::wstring ws_name = name;
  234. if (ws_name == _T("main_diandan") || ws_name == _T("main_waimai") || ws_name == _T("main_huiyuan") || ws_name == _T("main_baobiao") || ws_name == _T("main_jiaoban") || ws_name == _T("main_setting") || ws_name == _T("main_gengduo"))
  235. {
  236. if (m_curOptionName != ws_name)
  237. {
  238. //表示切换了tab
  239. m_curOption->SetBkColor(0x00000000);
  240. msg.pSender->SetBkColor(0xFF3CB371);
  241. m_curOptionName = ws_name;
  242. m_curOption = static_cast<CControlUI*>(msg.pSender);
  243. }
  244. }
  245. //先判断主页面的tab
  246. if (name == _T("main_diandan"))
  247. {
  248. this->SwitchPage(DIANDAN);
  249. }
  250. else if(name == _T("main_waimai"))
  251. {
  252. this->SwitchPage(DINGDAN);
  253. }
  254. else if (name == L"main_huiyuan")
  255. {
  256. }
  257. else if (name == L"main_baobiao")
  258. {
  259. }
  260. else if (name == L"main_jiaoban")
  261. {
  262. }
  263. else if(name == _T("main_setting"))
  264. {
  265. this->SwitchPage(SHEZHI);
  266. }
  267. else if (name == L"main_gengduo")
  268. {
  269. }
  270. else
  271. {
  272. //除了主界面固定区域的事件,其他的事件全部分发给子页面
  273. this->m_curPageUI->HandleSelectChangeMsg(msg);
  274. }
  275. }
  276. void CMainWnd::HandleItemSelectMsg(TNotifyUI& msg)
  277. {
  278. this->m_curPageUI->HandleItemSelectMsg(msg);
  279. }
  280. void CMainWnd::HandleTextChangedMsg(TNotifyUI& msg)
  281. {
  282. this->m_curPageUI->HandleTextChangedMsg(msg);
  283. }
  284. LRESULT CMainWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  285. {
  286. LRESULT lRes = 0;
  287. BOOL bHandled = TRUE;
  288. switch(uMsg)
  289. {
  290. case WM_CREATE:
  291. lRes = OnCreate(uMsg, wParam, lParam, bHandled);
  292. break;
  293. case WM_CLOSE:
  294. lRes = OnClose(uMsg, wParam, lParam, bHandled);
  295. break;
  296. case WM_DESTROY:
  297. lRes = OnDestroy(uMsg, wParam, lParam, bHandled);
  298. break;
  299. case WM_NCACTIVATE:
  300. lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
  301. break;
  302. case WM_NCCALCSIZE:
  303. lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
  304. break;
  305. case WM_NCPAINT:
  306. lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
  307. break;
  308. case WM_NCHITTEST:
  309. lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
  310. break;
  311. case WM_SIZE:
  312. lRes = OnSize(uMsg, wParam, lParam, bHandled);
  313. break;
  314. case WM_GETMINMAXINFO:
  315. lRes = OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled);
  316. break;
  317. case WM_SYSCOMMAND:
  318. lRes = OnSysCommand(uMsg, wParam, lParam, bHandled);
  319. break;
  320. case WM_SHOWTASK:
  321. lRes = OnTrayIcon(uMsg, wParam, lParam, bHandled);
  322. break;
  323. case WM_INPUT:
  324. lRes = OnInput(uMsg, wParam, lParam, bHandled);
  325. break;
  326. case WM_KEYDOWN:
  327. lRes = OnKeydown(uMsg, wParam, lParam, bHandled);
  328. break;
  329. case WM_KILLFOCUS:
  330. break;
  331. default:
  332. bHandled = FALSE;
  333. }
  334. if(bHandled)
  335. {
  336. return lRes;
  337. }
  338. //走到这里,说明消息还没有处理,应该是自定义的消息
  339. bool ret = this->HandleCustomMessage(uMsg, wParam, lParam);
  340. if (ret)
  341. {
  342. //自定义消息已经处理
  343. return 0;
  344. }
  345. if(m_pm.MessageHandler(uMsg, wParam, lParam, lRes))
  346. {
  347. return lRes;
  348. }
  349. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  350. }
  351. bool CMainWnd::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  352. {
  353. if (uMsg == WM_ORDERLIST_REFRESH)
  354. {
  355. if (m_curPageName != DINGDAN)
  356. {
  357. return true;
  358. }
  359. static_cast<CWaimaiOrderListPageUI*>(this->m_curPageUI)->DoRefresh();
  360. return true;
  361. }
  362. else if (uMsg == WM_LOGIN_AGAIN_OUT)
  363. {
  364. LoginOut(2);
  365. return true;
  366. }
  367. return false;
  368. }
  369. LRESULT CMainWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  370. {
  371. return false;
  372. }
  373. LRESULT CMainWnd::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  374. {
  375. // 有时会在收到WM_NCDESTROY后收到wParam为SC_CLOSE的WM_SYSCOMMAND
  376. if(wParam == SC_CLOSE)
  377. {
  378. ::PostQuitMessage(0L);
  379. bHandled = TRUE;
  380. return 0;
  381. }
  382. BOOL bZoomed = ::IsZoomed(*this);
  383. LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  384. if(::IsZoomed(*this) != bZoomed)
  385. {
  386. if(!bZoomed)
  387. {
  388. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  389. if(pControl)
  390. {
  391. pControl->SetVisible(false);
  392. }
  393. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  394. if(pControl)
  395. {
  396. pControl->SetVisible(true);
  397. }
  398. }
  399. else
  400. {
  401. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  402. if(pControl)
  403. {
  404. pControl->SetVisible(true);
  405. }
  406. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  407. if(pControl)
  408. {
  409. pControl->SetVisible(false);
  410. }
  411. }
  412. }
  413. return lRes;
  414. }
  415. LRESULT CMainWnd::OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  416. {
  417. int primaryMonitorWidth = ::GetSystemMetrics(SM_CXSCREEN);
  418. int primaryMonitorHeight = ::GetSystemMetrics(SM_CYSCREEN);
  419. MONITORINFO oMonitor = {};
  420. oMonitor.cbSize = sizeof(oMonitor);
  421. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
  422. CDuiRect rcWork = oMonitor.rcWork;
  423. rcWork.Offset(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  424. if(rcWork.right > primaryMonitorWidth)
  425. {
  426. rcWork.right = primaryMonitorWidth;
  427. }
  428. if(rcWork.bottom > primaryMonitorHeight)
  429. {
  430. rcWork.right = primaryMonitorHeight;
  431. }
  432. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  433. lpMMI->ptMaxPosition.x = rcWork.left;
  434. lpMMI->ptMaxPosition.y = rcWork.top;
  435. lpMMI->ptMaxSize.x = rcWork.right;
  436. lpMMI->ptMaxSize.y = rcWork.bottom;
  437. bHandled = FALSE;
  438. return 0;
  439. }
  440. LRESULT CMainWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  441. {
  442. SIZE szRoundCorner = m_pm.GetRoundCorner();
  443. if(!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0))
  444. {
  445. CDuiRect rcWnd;
  446. ::GetWindowRect(*this, &rcWnd);
  447. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  448. rcWnd.right++;
  449. rcWnd.bottom++;
  450. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  451. ::SetWindowRgn(*this, hRgn, TRUE);
  452. ::DeleteObject(hRgn);
  453. }
  454. bHandled = FALSE;
  455. return 0;
  456. }
  457. LRESULT CMainWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  458. {
  459. bHandled = FALSE;
  460. return 0;
  461. }
  462. /*
  463. *这个是窗口被销毁的时候调用的
  464. **/
  465. LRESULT CMainWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  466. {
  467. //直接退出程序,或者退出登录都会执行这个
  468. m_push->Stop();
  469. //停止打印任务
  470. CPosPrinterQueue::GetInstance()->StopWork();
  471. //停止称重任务
  472. CChengzhongWorker::GetInstance()->StopWork();
  473. bHandled = FALSE;
  474. return 0;
  475. }
  476. LRESULT CMainWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  477. {
  478. if(::IsIconic(*this))
  479. {
  480. bHandled = FALSE;
  481. }
  482. return (wParam == 0) ? TRUE : FALSE;
  483. }
  484. LRESULT CMainWnd::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  485. {
  486. return 0;
  487. }
  488. LRESULT CMainWnd::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  489. {
  490. return 0;
  491. }
  492. LRESULT CMainWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  493. {
  494. POINT pt;
  495. pt.x = GET_X_LPARAM(lParam);
  496. pt.y = GET_Y_LPARAM(lParam);
  497. ::ScreenToClient(*this, &pt);
  498. RECT rcClient;
  499. ::GetClientRect(*this, &rcClient);
  500. if(!::IsZoomed(*this))
  501. {
  502. RECT rcSizeBox = m_pm.GetSizeBox();
  503. if(pt.y < rcClient.top + rcSizeBox.top)
  504. {
  505. if(pt.x < rcClient.left + rcSizeBox.left)
  506. {
  507. return HTTOPLEFT;
  508. }
  509. if(pt.x > rcClient.right - rcSizeBox.right)
  510. {
  511. return HTTOPRIGHT;
  512. }
  513. return HTTOP;
  514. }
  515. else if(pt.y > rcClient.bottom - rcSizeBox.bottom)
  516. {
  517. if(pt.x < rcClient.left + rcSizeBox.left)
  518. {
  519. return HTBOTTOMLEFT;
  520. }
  521. if(pt.x > rcClient.right - rcSizeBox.right)
  522. {
  523. return HTBOTTOMRIGHT;
  524. }
  525. return HTBOTTOM;
  526. }
  527. if(pt.x < rcClient.left + rcSizeBox.left)
  528. {
  529. return HTLEFT;
  530. }
  531. if(pt.x > rcClient.right - rcSizeBox.right)
  532. {
  533. return HTRIGHT;
  534. }
  535. }
  536. RECT rcCaption = m_pm.GetCaptionRect();
  537. if(pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  538. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom)
  539. {
  540. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  541. if(pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0 &&
  542. _tcscmp(pControl->GetClass(), DUI_CTR_OPTION) != 0 &&
  543. _tcscmp(pControl->GetClass(), DUI_CTR_TEXT) != 0)
  544. {
  545. return HTCAPTION;
  546. }
  547. }
  548. return HTCLIENT;
  549. }
  550. LRESULT CMainWnd::OnInput(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  551. {
  552. UINT dwSize = 0;
  553. GetRawInputData((HRAWINPUT)lParam, (UINT)RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));//取数据,第一次调用函数将获取需要的字节大小
  554. LPBYTE lpbBuffer = new BYTE[dwSize];//分配指定的缓冲区大小
  555. GetRawInputData((HRAWINPUT)lParam, (UINT)RID_INPUT, (LPVOID)lpbBuffer, (PUINT)&dwSize, (UINT)sizeof(RAWINPUTHEADER));//第二次调用获取原始输入数据,读入lpbBuffer
  556. RAWINPUT * raw = (RAWINPUT *)lpbBuffer;
  557. if (raw->header.dwType == RIM_TYPEKEYBOARD)//这里可以截获所有键盘信息,如需区分不同的键盘输入信息,可以通过设备句柄判断。
  558. {
  559. if (raw->data.keyboard.Message == WM_KEYDOWN)
  560. {
  561. }
  562. else if (raw->data.keyboard.Message == WM_KEYUP)
  563. {
  564. char keytext[10] = { 0 };
  565. BYTE state[256] = { 0 };
  566. //通过虚拟键盘码得到名字
  567. ToAscii(raw->data.keyboard.VKey, raw->data.keyboard.MakeCode, state, (LPWORD)keytext, 0);
  568. //LOG_INFO("vkey:" << raw->data.keyboard.VKey);
  569. if (raw->data.keyboard.VKey >= 48 && raw->data.keyboard.VKey <= 122)
  570. {
  571. //这里是数字和英文字母之间的字符,包含部分标点
  572. if (m_is_start_catch == false)
  573. {
  574. m_is_start_catch = true;
  575. m_catch_string = "";
  576. m_is_next_daxie = false;
  577. char keytext[10] = { 0 };
  578. BYTE state[256] = { 0 };
  579. //通过虚拟键盘码得到名字
  580. ToAscii(raw->data.keyboard.VKey, raw->data.keyboard.MakeCode, state, (LPWORD)keytext, 0);
  581. m_catch_string += string(keytext);
  582. //LOG_INFO("起始字符:" << keytext << ",累积:" << m_catch_string.c_str());
  583. m_last_catch_clock = clock();
  584. }
  585. else
  586. {
  587. //已经是在抓取过程中了
  588. clock_t now = clock();
  589. double total_t = (double)(now - m_last_catch_clock) / CLOCKS_PER_SEC;
  590. if (total_t > 0.05)
  591. {
  592. //LOG_INFO("人工输入");
  593. //如果超过了50ms,说明是人工输入的了,就重置所有状态
  594. m_is_start_catch = false;
  595. m_catch_string = "";
  596. m_is_next_daxie = false;
  597. }
  598. else
  599. {
  600. char keytext[10] = { 0 };
  601. BYTE state[256] = { 0 };
  602. //通过虚拟键盘码得到名字
  603. ToAscii(raw->data.keyboard.VKey, raw->data.keyboard.MakeCode, state, (LPWORD)keytext, 0);
  604. std::string s_keytext = keytext;
  605. if (m_is_next_daxie == true)
  606. {
  607. //要把抓取的这个字符转为大写
  608. transform(s_keytext.begin(), s_keytext.end(), s_keytext.begin(), ::toupper);
  609. }
  610. //转换完了,再把状态还原
  611. m_is_next_daxie = false;
  612. m_catch_string += s_keytext;
  613. //LOG_INFO("抓取字符:" << s_keytext.c_str() << ",累积:" << m_catch_string.c_str());
  614. m_last_catch_clock = clock();
  615. }
  616. }
  617. }
  618. else if (raw->data.keyboard.VKey == 13)
  619. {
  620. //输入了enter
  621. if (m_is_start_catch == true)
  622. {
  623. //正在抓取中,判断enter才有意义,否则没有意义
  624. clock_t now = clock();
  625. double total_t = (double)(now - m_last_catch_clock) / CLOCKS_PER_SEC;
  626. if (total_t > 0.05)
  627. {
  628. //LOG_INFO("抓取enter人工");
  629. //如果超过了50ms,说明是人工输入的了,就重置所有状态
  630. m_is_start_catch = false;
  631. m_catch_string = "";
  632. m_is_next_daxie = false;
  633. }
  634. else
  635. {
  636. //LOG_INFO("抓取enter结束");
  637. //这种说明是扫码的
  638. std::string last = m_catch_string;
  639. //对抓取结果,进行处理
  640. m_curPageUI->HandleTextCapture(last);
  641. //处理完成后,清空重来
  642. m_is_start_catch = false;
  643. m_catch_string = "";
  644. m_is_next_daxie = false;
  645. }
  646. }
  647. else
  648. {
  649. m_is_start_catch = false;
  650. m_catch_string = "";
  651. m_is_next_daxie = false;
  652. }
  653. }
  654. else if (raw->data.keyboard.VKey == 16)
  655. {
  656. //这个是大小写切换的按键,扫描大写字母的时候会用到,每个大写字母都是由2个组成,先来个16,再来个对应小写字母的asicc
  657. if (m_is_start_catch == false)
  658. {
  659. //说明第一个字符就是大写字母
  660. m_is_start_catch = true;
  661. m_catch_string = "";
  662. m_is_next_daxie = true;
  663. m_last_catch_clock = clock();
  664. }
  665. else
  666. {
  667. //已经在抓取中了
  668. clock_t now = clock();
  669. double total_t = (double)(now - m_last_catch_clock) / CLOCKS_PER_SEC;
  670. if (total_t > 0.05)
  671. {
  672. //LOG_INFO("抓取16人工");
  673. //如果超过了50ms,说明是人工输入的了,就重置所有状态
  674. m_is_start_catch = false;
  675. m_catch_string = "";
  676. m_is_next_daxie = false;
  677. }
  678. else
  679. {
  680. m_is_next_daxie = true;
  681. m_last_catch_clock = clock();
  682. }
  683. }
  684. }
  685. else
  686. {
  687. //LOG_INFO("抓取结束,重新开始");
  688. //这种直接忽略,重新开始
  689. m_is_start_catch = false;
  690. m_catch_string = "";
  691. }
  692. }
  693. }
  694. delete[] lpbBuffer;
  695. bHandled = true;
  696. return 0;
  697. }
  698. LRESULT CMainWnd::OnKeydown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  699. {
  700. if (wParam == VK_RETURN)
  701. {
  702. //这里目前主要作用是,拦截按钮自带的焦点enter响应
  703. bHandled = true;
  704. return 0;
  705. }
  706. else if (wParam == VK_ESCAPE)
  707. {
  708. bHandled = true;
  709. return 0;
  710. }
  711. //其他的按键事件,不能返回0
  712. bHandled = false;
  713. return -1;
  714. }
  715. void CMainWnd::AddTrayIcon()
  716. {
  717. memset(&m_trayIcon, 0, sizeof(NOTIFYICONDATA));
  718. m_trayIcon.cbSize = sizeof(NOTIFYICONDATA);
  719. m_trayIcon.hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_DUILIB));
  720. m_trayIcon.hWnd = m_hWnd;
  721. lstrcpy(m_trayIcon.szTip, _T("智铺子收银系统"));
  722. m_trayIcon.uCallbackMessage = WM_SHOWTASK;
  723. m_trayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  724. Shell_NotifyIcon(NIM_ADD, &m_trayIcon);
  725. ShowWindow(SW_HIDE);
  726. }
  727. LRESULT CMainWnd::OnTrayIcon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  728. {
  729. //如果在图标中单击左键则还原
  730. if(lParam == WM_LBUTTONDOWN)
  731. {
  732. //删除托盘图标
  733. Shell_NotifyIcon(NIM_DELETE, &m_trayIcon);
  734. //显示主窗口
  735. ShowWindow(SW_SHOWNORMAL);
  736. //窗口最大化
  737. SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  738. }
  739. //如果在图标中单击右键则弹出声明式菜单
  740. if(lParam == WM_RBUTTONDOWN)
  741. {
  742. //获取鼠标坐标
  743. POINT pt;
  744. GetCursorPos(&pt);
  745. //右击后点别地可以清除“右击出来的菜单”
  746. SetForegroundWindow(m_hWnd);
  747. //托盘菜单 win32程序使用的是HMENU,如果是MFC程序可以使用CMenu
  748. HMENU hMenu;
  749. //生成托盘菜单
  750. hMenu = CreatePopupMenu();
  751. //添加菜单,关键在于设置的一个标识符 WM_ONCLOSE 点击后会用到
  752. AppendMenu(hMenu, MF_STRING, WM_ONCLOSE, _T("退出"));
  753. //弹出菜单,并把用户所选菜单项的标识符返回
  754. int cmd = TrackPopupMenu(hMenu, TPM_RETURNCMD, pt.x, pt.y, NULL, m_hWnd, NULL);
  755. //如果标识符是WM_ONCLOSE则关闭
  756. if(cmd == WM_ONCLOSE)
  757. {
  758. m_trayIcon.hIcon = NULL;
  759. Shell_NotifyIcon(NIM_DELETE, &m_trayIcon);
  760. //退出程序
  761. ::PostQuitMessage(0);
  762. }
  763. }
  764. bHandled = true;
  765. return 0;
  766. }
  767. void CMainWnd::LoginOut(int mode)
  768. {
  769. CSetting::SetParam("setting_is_auto_login", "0", true);
  770. CLoginWnd* pLogin = new CLoginWnd();
  771. if(pLogin == NULL)
  772. {
  773. return;
  774. }
  775. //设置模式
  776. pLogin->SetMode(mode);
  777. pLogin->Create(NULL, _T("智铺子收银系统登录"), UI_WNDSTYLE_DIALOG, 0, 0, 0, 0, 0, NULL);
  778. pLogin->SetIcon(IDI_ICON_DUILIB);
  779. pLogin->CenterWindow();
  780. ::ShowWindow(*pLogin, SW_SHOWNORMAL);
  781. Close();
  782. }
  783. CMessagePush* CMainWnd::getMessagePush()
  784. {
  785. return m_push;
  786. }
  787. void CMainWnd::SetInfopageWaimaiorderParam(std::string order_id, std::string order_no)
  788. {
  789. m_infopage_waimaiorder_id = order_id;
  790. m_infopage_waimaiorder_no = order_no;
  791. }
  792. void CMainWnd::ShowToast(std::wstring toast_value)
  793. {
  794. CToastWnd* pYouhuiDlg = new CToastWnd();
  795. if (pYouhuiDlg != NULL)
  796. {
  797. pYouhuiDlg->Create(m_pm.GetPaintWindow(), _T(""), UI_WNDSTYLE_DIALOG, WS_EX_WINDOWEDGE);
  798. pYouhuiDlg->SetIcon(IDI_ICON_DUILIB);
  799. pYouhuiDlg->CenterWindow();
  800. pYouhuiDlg->SetToast(toast_value);
  801. pYouhuiDlg->ShowWindow();
  802. }
  803. }
  804. //全局监听键盘输入
  805. void CMainWnd::RegKeyboardRawInput()
  806. {
  807. RAWINPUTDEVICE rawInputDevice[1];
  808. rawInputDevice[0].usUsagePage = 0x01;//设备类
  809. rawInputDevice[0].usUsage = 0x06;//设备类内的具体设备
  810. rawInputDevice[0].dwFlags = RIDEV_INPUTSINK;//意味着即使窗口失去焦点位置,仍然会一直接收输入消息
  811. rawInputDevice[0].hwndTarget = m_hWnd;
  812. if (RegisterRawInputDevices(rawInputDevice, 1, sizeof(rawInputDevice[0])) == FALSE)
  813. {
  814. printf("RegisterRawInputDevices failed");
  815. }
  816. }