CValueWnd.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. #include "../pch/pch.h"
  2. #include "CValueWnd.h"
  3. #include "../tool/CComHelper.h"
  4. #include "../tool/CSerialPort.h"
  5. #include "../helper/CBitmapHelper.h"
  6. void CValueWnd::Notify(TNotifyUI& msg)
  7. {
  8. if(msg.sType == _T("click"))
  9. {
  10. if(msg.pSender->GetName() == _T("closebtn"))
  11. {
  12. PostQuitMessage(0);
  13. return;
  14. }
  15. }
  16. }
  17. void CValueWnd::Init()
  18. {
  19. //初始化窗口位置
  20. InitWndPos();
  21. //抢焦点
  22. std::thread(&CValueWnd::TopMostWnd, this).detach();
  23. //注册热键
  24. UpdateRegisterHotKey();
  25. //添加托盘图标
  26. AddTrayIcon();
  27. //初始化菜单窗口
  28. pMenu = new CMenuWnd(this);
  29. pMenu->Create(NULL, L"智铺子收银插件菜单", WS_POPUP, WS_EX_TOOLWINDOW);
  30. pMenu->ShowWindow(false);
  31. //定位窗口的初始化
  32. pDingweiWnd = new CDingweiWnd();
  33. pDingweiWnd->Create(NULL, _T("智铺子收银插件窗口定位"), UI_WNDSTYLE_DIALOG, WS_EX_TOOLWINDOW, 0, 0, 0, 0, NULL);
  34. pDingweiWnd->ShowWindow(false);
  35. pDingweiInfoWnd = new CDingweiInfoWnd(this);
  36. pDingweiInfoWnd->Create(NULL, _T("智铺子收银插件窗口定位信息"), UI_WNDSTYLE_DIALOG, WS_EX_TOOLWINDOW, 0, 0, 0, 0, NULL);
  37. pDingweiInfoWnd->ShowWindow(false);
  38. //Ocr窗口初始化
  39. pOcrWnd = new COcrWnd(this);
  40. pOcrWnd->Create(NULL, _T("智铺子收银插件OCR定位"), UI_WNDSTYLE_FRAME, WS_EX_TOOLWINDOW , 0, 0, 0, 0, NULL);
  41. pOcrWnd->ShowWindow(false);
  42. //OCR的数据初始化
  43. if (m_tess.Init("./tessdata", "eng"))
  44. {
  45. LOG_INFO("OCRTesseract: Could not initialize tesseract.");
  46. MessageBox(m_hWnd, L"ocr语言库加载失败", L"错误", MB_OK);
  47. }
  48. m_tess.SetPageSegMode(tesseract::PageSegMode::PSM_SINGLE_LINE);
  49. m_tess.SetVariable("save_best_choices", "T");
  50. //再安装钩子
  51. BOOL ret = InstallHook();
  52. //开始启动监听
  53. RestartWatch();
  54. }
  55. /*
  56. *抢窗口前端,抢焦点
  57. **/
  58. void CValueWnd::TopMostWnd()
  59. {
  60. while (true)
  61. {
  62. ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  63. if (m_is_show_shoukuan && m_shoukuanHWND != NULL)
  64. {
  65. //::SendMessage(m_shoukuanHWND, WM_SHOUKUAN_UPDATE_FOCUS, NULL, NULL);
  66. }
  67. Sleep(300);
  68. }
  69. }
  70. void CValueWnd::RestartWatch()
  71. {
  72. std::thread(&CValueWnd::StartWatchWork, this).detach();
  73. }
  74. /*
  75. *根据当前数据库的设置,重启监听
  76. **/
  77. void CValueWnd::StartWatchWork()
  78. {
  79. //先暂停已有的监控
  80. m_isWatchWork = false;
  81. //等待2秒,这2秒就是等已有的监控线程自动退出
  82. Sleep(2000);
  83. //然后开始工作
  84. m_isWatchWork = true;
  85. std::string system_setting_jinezhuaqu_setting_type = CSetting::GetParam("system_setting_jinezhuaqu_setting_type");
  86. m_nWatchType = atoi(system_setting_jinezhuaqu_setting_type.c_str());
  87. if(m_nWatchType == 1)
  88. {
  89. //更新监听的端口号
  90. std::string system_setting_jinezhuaqu_setting_chuankou_num = CSetting::GetParam("system_setting_jinezhuaqu_setting_chuankou_num");
  91. if(system_setting_jinezhuaqu_setting_chuankou_num == "")
  92. {
  93. //参数错误
  94. return;
  95. }
  96. std::wstring ws_system_setting_jinezhuaqu_setting_chuankou_num = CLewaimaiString::UTF8ToUnicode(system_setting_jinezhuaqu_setting_chuankou_num);
  97. //设置窗口句柄
  98. ::SetHwnd(m_hWnd);
  99. //设置串口端口号
  100. std::transform(system_setting_jinezhuaqu_setting_chuankou_num.begin(), system_setting_jinezhuaqu_setting_chuankou_num.end(), system_setting_jinezhuaqu_setting_chuankou_num.begin(), ::toupper);
  101. std::transform(ws_system_setting_jinezhuaqu_setting_chuankou_num.begin(), ws_system_setting_jinezhuaqu_setting_chuankou_num.end(), ws_system_setting_jinezhuaqu_setting_chuankou_num.begin(), ::toupper);
  102. ::SetComNum(system_setting_jinezhuaqu_setting_chuankou_num.c_str(), ws_system_setting_jinezhuaqu_setting_chuankou_num.c_str(), system_setting_jinezhuaqu_setting_chuankou_num.size());
  103. }
  104. else if(m_nWatchType == 2)
  105. {
  106. //虚拟串口的模式
  107. std::thread(&CValueWnd::ReadXunichuankouValue, this).detach();
  108. }
  109. else if(m_nWatchType == 3)
  110. {
  111. //窗口模式
  112. m_watchWnd = NULL;
  113. std::thread(&CValueWnd::WatchWnd, this).detach();
  114. }
  115. else if (m_nWatchType == 4)
  116. {
  117. std::thread(&CValueWnd::StartOcrWork, this).detach();
  118. }
  119. }
  120. /*
  121. *读取客显金额的线程,一旦m_nWatchType不为2,那么就直接退出这个线程,下次设置的时候再开启
  122. **/
  123. void CValueWnd::ReadXunichuankouValue()
  124. {
  125. //读取虚拟串口的模式
  126. std::string system_setting_jinezhuaqu_setting_xunichuankou_num_2 = CSetting::GetParam("system_setting_jinezhuaqu_setting_xunichuankou_num_2");
  127. std::string system_setting_jinezhuaqu_setting_xunichuankou_botelv = CSetting::GetParam("system_setting_jinezhuaqu_setting_xunichuankou_botelv");
  128. CSerialPort serial(CLewaimaiString::UTF8ToUnicode(system_setting_jinezhuaqu_setting_xunichuankou_num_2), atoi(system_setting_jinezhuaqu_setting_xunichuankou_botelv.c_str()));
  129. bool ret = serial.openComm();
  130. if(!ret)
  131. {
  132. LOG_INFO("open com failed!");
  133. return;
  134. }
  135. char a[1024] = { 0 };
  136. //检查监控模式和波特率,如果变了就关闭掉
  137. while(m_nWatchType == 2 && m_isWatchWork)
  138. {
  139. memset(a, 0, 1024);
  140. //开始读取串口的数据
  141. DWORD nReaded = 0;
  142. serial.readFromComm(a, 1024, &nReaded);
  143. if(nReaded > 0)
  144. {
  145. UpdateKexian(a, nReaded);
  146. }
  147. Sleep(200);
  148. }
  149. serial.closeComm();
  150. }
  151. void CValueWnd::WatchWnd()
  152. {
  153. std::string system_setting_jinezhuaqu_setting_chuangkou_process_name = CSetting::GetParam("system_setting_jinezhuaqu_setting_chuangkou_process_name");
  154. std::string system_setting_jinezhuaqu_setting_chuangkou_title = CSetting::GetParam("system_setting_jinezhuaqu_setting_chuangkou_title");
  155. std::string system_setting_jinezhuaqu_setting_chuangkou_zorder = CSetting::GetParam("system_setting_jinezhuaqu_setting_chuangkou_zorder");
  156. //如果模式变了,或者参数变了,就退出(因为会重新启动一个新的监控)
  157. while(m_nWatchType == 3 && m_isWatchWork)
  158. {
  159. if(m_watchWnd != NULL)
  160. {
  161. std::wstring value;
  162. TCHAR szTitle[MAX_PATH] = { 0 };
  163. LPWSTR lpWindowName = szTitle;
  164. int ret = ::SendMessageW(m_watchWnd, WM_GETTEXT, (WPARAM)(MAX_PATH), (LPARAM)lpWindowName);
  165. if(ret == 0)
  166. {
  167. //说明m_watchWnd已经失效了
  168. value = L"0.00";
  169. m_watchWnd = NULL;
  170. }
  171. else
  172. {
  173. value = lpWindowName;
  174. //先对获取到的值做一下处理,删除掉人民币符号
  175. CLewaimaiString::Replace(value, L"¥", L"");
  176. }
  177. if(value != m_watchValue)
  178. {
  179. //保存下来窗口获取到的值
  180. m_watchValue = value;
  181. SendMessage(WM_WATCH_WND_UPDATE, NULL, NULL);
  182. }
  183. }
  184. else
  185. {
  186. //获得进程名字
  187. std::wstring processName = CLewaimaiString::UTF8ToUnicode(system_setting_jinezhuaqu_setting_chuangkou_process_name);
  188. //根据进程名字,找到对应的主窗口
  189. HWND mainWnd = GetWndHwnd(processName.c_str(), system_setting_jinezhuaqu_setting_chuangkou_title);
  190. if(mainWnd == NULL)
  191. {
  192. //说明这个进程还没有启动
  193. m_watchWnd = NULL;
  194. }
  195. else
  196. {
  197. //根据Z序查找目标窗口
  198. ValueWnd newWnd;
  199. newWnd.wndTitle = system_setting_jinezhuaqu_setting_chuangkou_title;
  200. newWnd.TopWnd = mainWnd;
  201. newWnd.z_order = atoi(system_setting_jinezhuaqu_setting_chuangkou_zorder.c_str());
  202. newWnd.processName = processName;
  203. newWnd.selfWnd = NULL;
  204. g_zorder = 0;
  205. EnumChildWindows(mainWnd, EnumMainwndChildProc, (LPARAM)(&newWnd));
  206. if(newWnd.selfWnd != NULL)
  207. {
  208. //找到了
  209. m_watchWnd = newWnd.selfWnd;
  210. }
  211. }
  212. }
  213. Sleep(200);
  214. }
  215. }
  216. void CValueWnd::StartOcrWork()
  217. {
  218. int system_setting_jinezhuaqu_setting_ocr_left = atoi(CSetting::GetParam("system_setting_jinezhuaqu_setting_ocr_left").c_str());
  219. int system_setting_jinezhuaqu_setting_ocr_top = atoi(CSetting::GetParam("system_setting_jinezhuaqu_setting_ocr_top").c_str());
  220. int system_setting_jinezhuaqu_setting_ocr_right = atoi(CSetting::GetParam("system_setting_jinezhuaqu_setting_ocr_right").c_str());
  221. int system_setting_jinezhuaqu_setting_ocr_bottom = atoi(CSetting::GetParam("system_setting_jinezhuaqu_setting_ocr_bottom").c_str());
  222. //如果模式变了,或者参数变了,就退出(因为会重新启动一个新的监控)
  223. while (m_nWatchType == 4 && m_isWatchWork)
  224. {
  225. RECT rect;
  226. rect.left = system_setting_jinezhuaqu_setting_ocr_left;
  227. rect.top = system_setting_jinezhuaqu_setting_ocr_top;
  228. rect.right = system_setting_jinezhuaqu_setting_ocr_right;
  229. rect.bottom = system_setting_jinezhuaqu_setting_ocr_bottom;
  230. if (IsRectEmpty(&rect))
  231. {
  232. }
  233. else
  234. {
  235. CBitmapHelper helper;
  236. std::string ocr_result;
  237. bool ret = helper.OcrRect(m_tess, &rect, ocr_result);
  238. if (ret)
  239. {
  240. if (!atof(ocr_result.c_str()))
  241. {
  242. //识别结果不是有效的数字
  243. CLabelUI* valueLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("value")));
  244. valueLabel->SetText(L"0.00");
  245. }
  246. else
  247. {
  248. std::string format_value = CLewaimaiString::DoubleToString(atof(ocr_result.c_str()), 2);
  249. std::wstring ws_ocr_result = CLewaimaiString::UTF8ToUnicode(format_value);
  250. m_watchValue = ws_ocr_result;
  251. CLabelUI* valueLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("value")));
  252. valueLabel->SetText(ws_ocr_result.c_str());
  253. }
  254. }
  255. }
  256. Sleep(200);
  257. }
  258. }
  259. LRESULT CValueWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  260. {
  261. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  262. styleValue &= ~WS_CAPTION;
  263. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  264. // 把自己的窗口句柄与窗口绘制管理器挂接在一起
  265. m_pm.Init(m_hWnd);
  266. m_pm.AddPreMessageFilter(this);
  267. CDialogBuilder builder;
  268. CControlUI* pRoot = builder.Create(_T("value.xml"), (UINT)0, NULL, &m_pm);
  269. ASSERT(pRoot && "Failed to parse XML");
  270. // 把这些控件绘制到本窗口上
  271. m_pm.AttachDialog(pRoot);
  272. // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
  273. m_pm.AddNotifier(this);
  274. //进行一些初始化工作
  275. Init();
  276. return 0;
  277. }
  278. LRESULT CValueWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  279. {
  280. bHandled = FALSE;
  281. return 0;
  282. }
  283. /*
  284. *这个是窗口被销毁的时候调用的
  285. **/
  286. LRESULT CValueWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  287. {
  288. UnregisterHotKey(m_hWnd, WM_MYHOTKEY_SHOUKUAN);
  289. bHandled = FALSE;
  290. return 0;
  291. }
  292. LRESULT CValueWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  293. {
  294. if(::IsIconic(*this))
  295. {
  296. bHandled = FALSE;
  297. }
  298. return (wParam == 0) ? TRUE : FALSE;
  299. }
  300. LRESULT CValueWnd::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  301. {
  302. return 0;
  303. }
  304. LRESULT CValueWnd::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  305. {
  306. return 0;
  307. }
  308. LRESULT CValueWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  309. {
  310. POINT pt;
  311. pt.x = GET_X_LPARAM(lParam);
  312. pt.y = GET_Y_LPARAM(lParam);
  313. ::ScreenToClient(*this, &pt);
  314. RECT rcClient;
  315. ::GetClientRect(*this, &rcClient);
  316. RECT rcCaption = m_pm.GetCaptionRect();
  317. if(pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  318. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom)
  319. {
  320. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  321. if(pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0)
  322. {
  323. return HTCAPTION;
  324. }
  325. }
  326. return HTCLIENT;
  327. }
  328. LRESULT CValueWnd::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  329. {
  330. pMenu->ShowWindow(false);
  331. bHandled = FALSE;
  332. return 0;
  333. }
  334. LRESULT CValueWnd::OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  335. {
  336. RECT rect;
  337. GetWindowRect(m_hWnd, &rect);
  338. ::SetWindowPos(pMenu->GetHWND(), HWND_NOTOPMOST, rect.left, rect.bottom - 3, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  339. pMenu->ShowWindow(true);
  340. //右击后点别地可以清除“右击出来的菜单”
  341. SetForegroundWindow(pMenu->GetHWND());
  342. bHandled = FALSE;
  343. return 0;
  344. }
  345. LRESULT CValueWnd::OnKexianUpdate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  346. {
  347. //如果当前不是串口模式,直接忽略
  348. if(m_nWatchType != 1)
  349. {
  350. bHandled = TRUE;
  351. return true;
  352. }
  353. //收到了客显数据消息,读取客显数据
  354. char data[MAX_DATA_LENGTH] = { 0 };
  355. int data_length = 0;
  356. HookGetData(data, &data_length);
  357. if(data_length == 0)
  358. {
  359. bHandled = TRUE;
  360. return true;
  361. }
  362. UpdateKexian(data, data_length);
  363. bHandled = TRUE;
  364. return true;
  365. }
  366. LRESULT CValueWnd::OnWatchWndUpdate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  367. {
  368. CLabelUI* valueLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("value")));
  369. std::string utf8_watch_value = CLewaimaiString::UnicodeToUTF8(m_watchValue);
  370. std::string money_show = CLewaimaiString::DoubleToString(atof(utf8_watch_value.c_str()), 2);
  371. valueLabel->SetText(CLewaimaiString::UTF8ToUnicode(money_show).c_str());
  372. return TRUE;
  373. }
  374. LRESULT CValueWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  375. {
  376. LRESULT lRes = 0;
  377. BOOL bHandled = TRUE;
  378. switch(uMsg)
  379. {
  380. case WM_CREATE:
  381. lRes = OnCreate(uMsg, wParam, lParam, bHandled);
  382. break;
  383. case WM_CLOSE:
  384. lRes = OnClose(uMsg, wParam, lParam, bHandled);
  385. break;
  386. case WM_DESTROY:
  387. lRes = OnDestroy(uMsg, wParam, lParam, bHandled);
  388. break;
  389. case WM_NCACTIVATE:
  390. lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
  391. break;
  392. case WM_NCLBUTTONDOWN:
  393. lRes = OnLButtonDown(uMsg, wParam, lParam, bHandled);
  394. break;
  395. case WM_NCRBUTTONDOWN:
  396. lRes = OnRButtonDown(uMsg, wParam, lParam, bHandled);
  397. break;
  398. case WM_NCCALCSIZE:
  399. lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
  400. break;
  401. case WM_NCPAINT:
  402. lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
  403. break;
  404. case WM_NCHITTEST:
  405. lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
  406. break;
  407. case WM_SHOWTASK:
  408. lRes = OnTrayIcon(uMsg, wParam, lParam, bHandled);
  409. break;
  410. case WM_HOOK_MESSAGE:
  411. lRes = OnKexianUpdate(uMsg, wParam, lParam, bHandled);
  412. break;
  413. case WM_WATCH_WND_UPDATE:
  414. lRes = OnWatchWndUpdate(uMsg, wParam, lParam, bHandled);
  415. break;
  416. case WM_HOTKEY:
  417. lRes = OnHotKey(uMsg, wParam, lParam, bHandled);
  418. break;
  419. default:
  420. bHandled = FALSE;
  421. }
  422. if(bHandled)
  423. {
  424. return lRes;
  425. }
  426. if(m_pm.MessageHandler(uMsg, wParam, lParam, lRes))
  427. {
  428. return lRes;
  429. }
  430. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  431. }
  432. LRESULT CValueWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  433. {
  434. return false;
  435. }
  436. void CValueWnd::ShowShoukuan()
  437. {
  438. if (m_is_show_shoukuan == true)
  439. {
  440. return;
  441. }
  442. pMenu->ShowWindow(false);
  443. CShoukuanWnd* pShoukuanWnd = new CShoukuanWnd;
  444. if (pShoukuanWnd != NULL)
  445. {
  446. pShoukuanWnd->Create(NULL, _T(""), UI_WNDSTYLE_DIALOG, WS_EX_TOOLWINDOW);
  447. pShoukuanWnd->SetIcon(IDI_ICON_DUILIB);
  448. pShoukuanWnd->CenterWindow();
  449. ::SetWindowPos(pShoukuanWnd->GetHWND(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  450. m_is_show_shoukuan = true;
  451. m_shoukuanWnd = pShoukuanWnd;
  452. m_shoukuanHWND = pShoukuanWnd->GetHWND();
  453. //只要金额不为0,就初始化
  454. std::string utf8_watch_value = CLewaimaiString::UnicodeToUTF8(m_watchValue);
  455. if (atof(utf8_watch_value.c_str()) > 0.001)
  456. {
  457. std::string format_string = CLewaimaiString::DoubleToString(atof(utf8_watch_value.c_str()), 2);
  458. m_watchValue = CLewaimaiString::UTF8ToUnicode(format_string);
  459. pShoukuanWnd->InitMoney(m_watchValue);
  460. }
  461. UINT ret = pShoukuanWnd->ShowModal();
  462. m_is_show_shoukuan = false;
  463. m_shoukuanWnd = NULL;
  464. m_shoukuanHWND = NULL;
  465. if (ret == IDOK)
  466. {
  467. }
  468. else if (ret == IDCANCEL)
  469. {
  470. }
  471. }
  472. }
  473. void CValueWnd::AddTrayIcon()
  474. {
  475. memset(&m_trayIcon, 0, sizeof(NOTIFYICONDATA));
  476. m_trayIcon.cbSize = sizeof(NOTIFYICONDATA);
  477. m_trayIcon.hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_DUILIB));
  478. m_trayIcon.hWnd = m_hWnd;
  479. lstrcpy(m_trayIcon.szTip, _T("智铺子收银插件"));
  480. m_trayIcon.uCallbackMessage = WM_SHOWTASK;
  481. m_trayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  482. Shell_NotifyIcon(NIM_ADD, &m_trayIcon);
  483. }
  484. LRESULT CValueWnd::OnTrayIcon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  485. {
  486. //如果在图标中单击左键则还原
  487. if(lParam == WM_LBUTTONDOWN)
  488. {
  489. ShowTrayMenu();
  490. }
  491. //如果在图标中单击右键则弹出声明式菜单
  492. else if(lParam == WM_RBUTTONDOWN)
  493. {
  494. ShowTrayMenu();
  495. }
  496. bHandled = true;
  497. return 0;
  498. }
  499. LRESULT CValueWnd::OnHotKey(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  500. {
  501. UINT fuModifiers = (UINT)LOWORD(lParam);
  502. UINT uVirtKey = (UINT)HIWORD(lParam);
  503. //判断响应了什么热键
  504. if (wParam == WM_MYHOTKEY_SHOUKUAN)
  505. {
  506. if (m_is_show_setting == false)
  507. {
  508. ShowShoukuan();
  509. }
  510. }
  511. bHandled = TRUE;
  512. return 0;
  513. }
  514. void CValueWnd::ShowSystemSetting()
  515. {
  516. //先隐藏目录
  517. pMenu->ShowWindow(false);
  518. //然后判断和显示系统设置
  519. if (m_is_show_setting == true)
  520. {
  521. return;
  522. }
  523. CSystemSettingWnd* pSystemSettingFrame = new CSystemSettingWnd(this);
  524. m_settingWnd = pSystemSettingFrame;
  525. if (pSystemSettingFrame != NULL)
  526. {
  527. m_is_show_setting = true;
  528. pSystemSettingFrame->Create(NULL, _T(""), UI_WNDSTYLE_DIALOG, WS_EX_TOOLWINDOW);
  529. pSystemSettingFrame->SetIcon(IDI_ICON_DUILIB);
  530. pSystemSettingFrame->CenterWindow();
  531. UINT ret = pSystemSettingFrame->ShowModal();
  532. m_is_show_setting = false;
  533. if (ret == IDOK)
  534. {
  535. }
  536. else if (ret == IDCANCEL)
  537. {
  538. }
  539. }
  540. }
  541. void CValueWnd::UpdateKexian(const char* data, int length)
  542. {
  543. std::string new_data = data;
  544. //清屏命令
  545. std::string clear_command = "\f";
  546. //4个信号灯全灭
  547. std::string quanmie_command = "\x1bs0";
  548. std::string show_command = "\x1bQA";
  549. std::string show_command_2 = "QA";
  550. std::string type_command = "\x1bs";
  551. if(new_data == clear_command || new_data == quanmie_command)
  552. {
  553. //清屏命令,或者是熄灭灯的命令
  554. return;
  555. }
  556. int nShowType;
  557. if(new_data.find(show_command) == new_data.npos)
  558. {
  559. //没有\x1bQA,继续找有没有QA(比如思迅)
  560. if (new_data.find(show_command_2) == new_data.npos)
  561. {
  562. //完全没QA
  563. return;
  564. }
  565. else
  566. {
  567. nShowType = 2;
  568. }
  569. }
  570. else
  571. {
  572. nShowType = 1;
  573. }
  574. std::string money;
  575. int nPos;
  576. if (nShowType == 1)
  577. {
  578. nPos = new_data.find(show_command);;
  579. }
  580. else
  581. {
  582. nPos = new_data.find(show_command_2);
  583. }
  584. int nTypePos = new_data.find(type_command, nPos + 1);
  585. if(nTypePos == new_data.npos)
  586. {
  587. //没有类型信息,有些收银机(比如思迅天店)就是不传类型信息,这种只有传什么显示什么了
  588. int nTabPos = new_data.find("\r", nPos + 1);
  589. if(nTabPos == new_data.npos)
  590. {
  591. //制表符也没有
  592. return;
  593. }
  594. if (nShowType == 1)
  595. {
  596. money = new_data.substr(nPos + 3, nTabPos - nPos - 3);
  597. }
  598. else
  599. {
  600. money = new_data.substr(nPos + 2, nTabPos - nPos - 2);
  601. }
  602. CLabelUI* valueLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("value")));
  603. std::string money_show = CLewaimaiString::DoubleToString(atof(money.c_str()), 2);
  604. m_watchValue = CLewaimaiString::UTF8ToUnicode(money_show);
  605. valueLabel->SetText(CLewaimaiString::UTF8ToUnicode(money_show).c_str());
  606. }
  607. else
  608. {
  609. std::string value_type = new_data.substr(nTypePos + 2, 1);
  610. money = new_data.substr(nPos + 3, nTypePos - nPos - 3 - 1);
  611. if(value_type == "2")
  612. {
  613. //这个表示是总计的,那么就要刷新金额显示
  614. CLabelUI* valueLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("value")));
  615. std::string money_show = CLewaimaiString::DoubleToString(atof(money.c_str()), 2);
  616. m_watchValue = CLewaimaiString::UTF8ToUnicode(money_show);
  617. valueLabel->SetText(CLewaimaiString::UTF8ToUnicode(money_show).c_str());
  618. }
  619. else
  620. {
  621. //这种情况传输的是一些找零、单价之类的信息,不更新客显金额
  622. }
  623. }
  624. return;
  625. }
  626. void CValueWnd::InitWndPos()
  627. {
  628. ASSERT(::IsWindow(m_hWnd));
  629. ASSERT((GetWindowStyle(m_hWnd)&WS_CHILD) == 0);
  630. RECT rcDlg = { 0 };
  631. ::GetWindowRect(m_hWnd, &rcDlg);
  632. RECT rcArea = { 0 };
  633. RECT rcCenter = { 0 };
  634. HWND hWnd = *this;
  635. HWND hWndParent = ::GetParent(m_hWnd);
  636. HWND hWndCenter = ::GetWindowOwner(m_hWnd);
  637. if(hWndCenter != NULL)
  638. {
  639. hWnd = hWndCenter;
  640. }
  641. // 处理多显示器模式下屏幕居中
  642. MONITORINFO oMonitor = {};
  643. oMonitor.cbSize = sizeof(oMonitor);
  644. ::GetMonitorInfo(::MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &oMonitor);
  645. rcArea = oMonitor.rcWork;
  646. if(hWndCenter == NULL)
  647. {
  648. rcCenter = rcArea;
  649. }
  650. else
  651. {
  652. ::GetWindowRect(hWndCenter, &rcCenter);
  653. }
  654. int DlgWidth = rcDlg.right - rcDlg.left;
  655. int DlgHeight = rcDlg.bottom - rcDlg.top;
  656. int xLeft = rcCenter.right - DlgWidth - 60;
  657. int yTop = 100;
  658. if(xLeft < rcArea.left)
  659. {
  660. xLeft = rcArea.left;
  661. }
  662. else if(xLeft + DlgWidth > rcArea.right)
  663. {
  664. xLeft = rcArea.right - DlgWidth;
  665. }
  666. if(yTop < rcArea.top)
  667. {
  668. yTop = rcArea.top;
  669. }
  670. else if(yTop + DlgHeight > rcArea.bottom)
  671. {
  672. yTop = rcArea.bottom - DlgHeight;
  673. }
  674. ::SetWindowPos(m_hWnd, HWND_TOPMOST, xLeft, yTop, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  675. }
  676. void CValueWnd::ShowTrayMenu()
  677. {
  678. //获取鼠标坐标
  679. POINT pt;
  680. GetCursorPos(&pt);
  681. CRect rect;
  682. GetWindowRect(pMenu->GetHWND(), &rect);
  683. int height = rect.bottom - rect.top;
  684. //显示目录
  685. ::SetWindowPos(pMenu->GetHWND(), HWND_NOTOPMOST, pt.x, pt.y - 15 - height, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  686. pMenu->ShowWindow(true);
  687. SetFocus(pMenu->GetHWND());
  688. //右击后点别地可以清除“右击出来的菜单”
  689. SetForegroundWindow(pMenu->GetHWND());
  690. }
  691. void CValueWnd::StartDingweiWnd()
  692. {
  693. m_settingWnd->ShowWindow(false);
  694. m_is_dingwei_wnd = true;
  695. std::thread(&CValueWnd::StartDingweiWork, this).detach();
  696. }
  697. void CValueWnd::ConfirmDingweiWnd(std::wstring value, HWND hWnd)
  698. {
  699. m_is_dingwei_wnd = false;
  700. m_settingWnd->SetDingweiWnd(value, hWnd);
  701. }
  702. void CValueWnd::CancalDingweiWnd()
  703. {
  704. m_is_dingwei_wnd = false;
  705. }
  706. void CValueWnd::StartDingweiWork()
  707. {
  708. while (m_is_dingwei_wnd)
  709. {
  710. //这个时候,是开始定位窗口了
  711. POINT pt;
  712. GetCursorPos(&pt);
  713. //将屏幕坐标保存起来
  714. POINT ptScreen = pt;
  715. //转换成桌面坐标
  716. HWND hDeskHandle = GetDesktopWindow();
  717. ScreenToClient(hDeskHandle, &pt);
  718. HWND hParent = hDeskHandle;
  719. HWND hChild = ChildWindowFromPointEx(hParent, pt, CWP_SKIPINVISIBLE | CWP_SKIPTRANSPARENT | CWP_SKIPDISABLED);
  720. if (hChild == pDingweiInfoWnd->GetHWND())
  721. {
  722. //这里表示是移动信息窗口了,这里就不要执行后面的刷新操作了
  723. }
  724. else
  725. {
  726. while (hChild != NULL && hChild != hParent)
  727. {
  728. //说明还有更下级的子窗口
  729. hParent = hChild;
  730. pt = ptScreen;
  731. ScreenToClient(hParent, &pt);
  732. hChild = ChildWindowFromPointEx(hParent, pt, CWP_SKIPINVISIBLE | CWP_SKIPTRANSPARENT);
  733. }
  734. HWND hWnd = hParent;
  735. RECT rect;
  736. GetWindowRect(hWnd, &rect);
  737. //在这个窗口上,画一个边框
  738. pDingweiWnd->ShowWindow(true);
  739. ::SetWindowPos(pDingweiWnd->GetHWND(), HWND_TOPMOST, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOACTIVATE);
  740. //显示定位信息
  741. TCHAR szTitle[MAX_PATH] = { 0 };
  742. LPWSTR lpWindowName = szTitle;
  743. ::SendMessageW(hWnd, WM_GETTEXT, (WPARAM)(MAX_PATH), (LPARAM)lpWindowName);
  744. //设置信息窗口的内容
  745. pDingweiInfoWnd->UpdateInfo(lpWindowName, hWnd);
  746. pDingweiInfoWnd->ShowWindow(true);
  747. ::SetWindowPos(pDingweiInfoWnd->GetHWND(), HWND_TOPMOST, rect.left, rect.bottom, rect.right - rect.left, 30, SWP_NOACTIVATE);
  748. }
  749. Sleep(100);
  750. }
  751. m_settingWnd->ShowWindow(true);
  752. pDingweiWnd->ShowWindow(false);
  753. pDingweiInfoWnd->ShowWindow(false);
  754. }
  755. void CValueWnd::StartOcrWnd()
  756. {
  757. m_settingWnd->ShowWindow(false);
  758. m_is_orc_wnd = true;
  759. pOcrWnd->ShowWindow(true);
  760. //先默认居中
  761. ::SetWindowPos(pOcrWnd->GetHWND(), HWND_TOPMOST, 0, 0, 320, 80, SWP_NOMOVE | SWP_NOACTIVATE);
  762. pOcrWnd->CenterWindow();
  763. }
  764. void CValueWnd::ConfirmOcrWnd(RECT rect)
  765. {
  766. m_is_orc_wnd = false;
  767. pOcrWnd->ShowWindow(false);
  768. //保存截屏的结果
  769. m_settingWnd->SetOcrWnd(rect);
  770. //显示设置窗口
  771. m_settingWnd->ShowWindow(true);
  772. }
  773. tesseract::TessBaseAPI& CValueWnd::GetTess()
  774. {
  775. return m_tess;
  776. }
  777. bool CValueWnd::UpdateRegisterHotKey()
  778. {
  779. UINT shoukuan_mod = (UINT)atoi(CSetting::GetParam("system_setting_kuaijiejian_shoukuan_mod").c_str());
  780. UINT shoukuan_vk = (UINT)atoi(CSetting::GetParam("system_setting_kuaijiejian_shoukuan_vk").c_str());
  781. //先卸载旧的,再注册新的
  782. UnregisterHotKey(m_hWnd, WM_MYHOTKEY_SHOUKUAN);
  783. int nRet = RegisterHotKey(m_hWnd, WM_MYHOTKEY_SHOUKUAN, shoukuan_mod, shoukuan_vk);
  784. return nRet;
  785. }