WindFuncs.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #include "StdAfx.h"
  2. #include <Psapi.h>
  3. #include "Ocr_Dict.h"
  4. #include "ocr_pubfuncs.h"
  5. #include "AutoFunc.h"
  6. #include "WindFuncs.h"
  7. #include "Common_Funcs.h"
  8. #pragma comment(lib, "Psapi.lib")
  9. WindFuncs::WindFuncs(void)
  10. {
  11. }
  12. WindFuncs::~WindFuncs(void)
  13. {
  14. }
  15. HWND WindFuncs::GetTrueParent(HWND hwnd)
  16. {
  17. DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
  18. if((dwStyle & WS_CHILD) == WS_CHILD)
  19. return GetParent(hwnd);
  20. else
  21. return GetDesktopWindow();
  22. }
  23. BOOL WindFuncs::CmpNoCase(string &src,string &des)
  24. {
  25. string::const_iterator p = src.begin();
  26. string::const_iterator p2 = des.begin();
  27. if(src.length() != des.length())
  28. {
  29. return FALSE;
  30. }
  31. while(p!=src.end() && p2!=des.end())
  32. {
  33. if(toupper(*p)!=toupper(*p2))
  34. {
  35. return FALSE;
  36. }
  37. ++p;
  38. ++p2;
  39. }
  40. return TRUE;
  41. }
  42. BOOL WindFuncs::LocateWindowsWithRegEx(HWND Parent,WindNode &node,vector<WindNode> &reslist,BOOL Visible,BOOL singleSearch)
  43. {
  44. return NULL;
  45. }
  46. UINT WindFuncs::GetZOrder(HWND Wnd)
  47. {
  48. UINT zOrder = 0;
  49. //locate parent
  50. if(Wnd != NULL)
  51. {
  52. HWND desk = GetDesktopWindow();
  53. while(Wnd && Wnd != desk)
  54. {
  55. Wnd = GetTrueParent(Wnd);
  56. ++zOrder;
  57. }
  58. }
  59. return zOrder;
  60. }
  61. BOOL WindFuncs::CheckTheProcess(HWND Wnd,WindNode &node)
  62. {
  63. DWORD pid = 0;
  64. if(node.m_ProcessName.length() == 0)
  65. {
  66. return TRUE;
  67. }
  68. GetWindowThreadProcessId(Wnd,&pid);
  69. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  70. TCHAR* procName = new TCHAR[MAX_PATH];
  71. memset(procName, 0, sizeof(TCHAR)*MAX_PATH);
  72. if(GetModuleFileNameEx(hProcess,NULL,procName,MAX_PATH) == 0)
  73. {
  74. LPVOID lpMsgBuf;
  75. DWORD dw = GetLastError();
  76. FormatMessage(
  77. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  78. FORMAT_MESSAGE_FROM_SYSTEM |
  79. FORMAT_MESSAGE_IGNORE_INSERTS,
  80. NULL,
  81. dw,
  82. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  83. (LPTSTR) &lpMsgBuf,
  84. 0, NULL );
  85. // Display the error message and exit the process
  86. string msgContent = (TCHAR*)lpMsgBuf;
  87. //MessageBox(NULL, (LPCTSTR)msgContent.GetBuffer(), TEXT("Error"), MB_OK);
  88. LocalFree(lpMsgBuf);
  89. CloseHandle(hProcess);
  90. delete []procName;
  91. return FALSE;
  92. }
  93. string prc = procName;
  94. CloseHandle(hProcess);
  95. delete []procName;
  96. std::size_t found = prc.find_last_of("\\");
  97. string filename = prc.substr(found+1);
  98. if(filename.length() > 0)
  99. {
  100. if(WindFuncs::CmpNoCase(node.m_ProcessName , filename))
  101. {
  102. return TRUE;
  103. }
  104. }
  105. return FALSE;
  106. }
  107. WindNode WindFuncs::LocateOneWindow(HWND Parent,WindNode &node,BOOL Visible)
  108. {
  109. WindNode nodeLocal;
  110. vector<WindNode> list1;
  111. WindFuncs::LocateWindows(Parent,node,list1,Visible,1);
  112. if(list1.size() > 0)
  113. {
  114. nodeLocal = list1[0];
  115. return nodeLocal;
  116. }
  117. return nodeLocal;
  118. }
  119. BOOL WindFuncs::CheckWindowMatch(HWND subhandle,WindNode &node,BOOL Visible)
  120. {
  121. BOOL sizeMatch = FALSE;
  122. BOOL subIDMatch = FALSE;
  123. BOOL subTypeMatch = FALSE;
  124. BOOL subTypeExMatch = FALSE;
  125. TCHAR szTitle[MAX_PATH] = {0};
  126. if(WindFuncs::CheckTheProcess(subhandle,node) == FALSE)
  127. {
  128. //failed
  129. return FALSE;
  130. }
  131. //name compare
  132. if(node.m_Name.size() > 0)
  133. {
  134. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  135. subhandle, // handle to destination control
  136. (UINT)WM_GETTEXT, // message ID
  137. (WPARAM)MAX_PATH, // = (WPARAM) () wParam;
  138. (LPARAM)szTitle // = 0; not used, must be zero
  139. );
  140. if(strcmp(szTitle,node.m_Name.c_str()) != 0)
  141. {
  142. return FALSE;
  143. }
  144. }
  145. //class compare
  146. if(node.m_Class.size() > 0)
  147. {
  148. memset(szTitle, 0, sizeof(TCHAR)*MAX_PATH);
  149. GetClassName(subhandle,szTitle,MAX_PATH);
  150. if (strcmp(szTitle, node.m_Class.c_str()) != 0)
  151. {
  152. return FALSE;
  153. }
  154. }
  155. //do ID check
  156. if((node.m_GuiID == NO_GUIID) || (node.m_GuiID != NO_GUIID && node.m_GuiID == GetWindowLong(subhandle,GWL_ID)))
  157. {
  158. subIDMatch = TRUE;
  159. }
  160. //for test
  161. LONG wndType = GetWindowLong(subhandle,GWL_STYLE);
  162. //do style check
  163. if((node.m_WndType == NO_WNDTYPE) || (node.m_WndType != NO_WNDTYPE && node.m_WndType == GetWindowLong(subhandle,GWL_STYLE)))
  164. {
  165. subTypeMatch = TRUE;
  166. }
  167. if((node.m_WndTypeEx == NO_WNDTYPE) || (node.m_WndTypeEx != NO_WNDTYPE && node.m_WndTypeEx == GetWindowLong(subhandle,GWL_EXSTYLE)))
  168. {
  169. subTypeExMatch = TRUE;
  170. }
  171. //do size check
  172. RECT Rect1;
  173. GetWindowRect(subhandle,&Rect1);
  174. sizeMatch = TRUE;
  175. if(node.m_Width > 0)
  176. {
  177. if(node.m_Width != (Rect1.right - Rect1.left))
  178. {
  179. sizeMatch = FALSE;
  180. }
  181. }
  182. if(node.m_Height > 0)
  183. {
  184. if(node.m_Height != (Rect1.bottom - Rect1.top))
  185. {
  186. sizeMatch = FALSE;
  187. }
  188. }
  189. //for test
  190. //if((Rect1.right - Rect1.left) == 116 && (Rect1.bottom - Rect1.top) == 52)
  191. //{
  192. // _tprintf(_T("Hited:%d,%d,%d\n"),subTypeMatch,subTypeExMatch,sizeMatch);
  193. //}
  194. if(subTypeMatch == TRUE && subTypeExMatch == TRUE && sizeMatch == TRUE && subIDMatch == TRUE)
  195. {
  196. //visible
  197. if(Visible == FALSE || IsWindowVisible(subhandle) == TRUE)
  198. {
  199. if(IsWindowVisible(subhandle) == TRUE && node.m_Dict.size() > 0 && node.m_DictKey.size() > 0)
  200. {
  201. //do ocr check
  202. ULONG X,Y;
  203. Ocr_Dict dict;
  204. OcrScreen screen;
  205. screen.UpdateScreen(subhandle);
  206. //screen.SaveTheAreaToBitMapFileWithName(L"test.bmp");
  207. RECT area = node.m_DictArea;
  208. if(dict.LoadOcrDictFromDirectory(node.m_Dict.c_str()) == TRUE)
  209. {
  210. if(FindMatchPoint(dict,screen,area,COLORREF(node.m_DictRgb),100,node.m_DictKey,X,Y) == TRUE)
  211. {
  212. node.m_ResDictKey.x = (INT)X;
  213. node.m_ResDictKey.y = (INT)Y;
  214. return TRUE;
  215. }
  216. }
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }
  221. }
  222. return FALSE;
  223. }
  224. BOOL WindFuncs::LocateWindows(HWND Parent,WindNode &node,vector<WindNode> &reslist,BOOL Visible,BOOL singleSearch)
  225. {
  226. BOOL subIDMatch = FALSE;
  227. BOOL subTypeMatch = FALSE;
  228. BOOL subTypeExMatch = FALSE;
  229. HWND sshandle = NULL;
  230. TCHAR *pClassName = NULL;
  231. TCHAR *pWindowName = NULL;
  232. if(node.RegExSearch())
  233. {
  234. return WindFuncs::LocateWindowsWithRegEx(Parent,node,reslist,Visible,singleSearch);
  235. }
  236. if(Parent == NULL)
  237. {
  238. Parent = GetDesktopWindow();
  239. }
  240. UINT ParentzOrder = WindFuncs::GetZOrder(Parent);
  241. if(node.DigDeeper(ParentzOrder + 1) == FALSE)
  242. {
  243. return FALSE;
  244. }
  245. if(node.m_Class.length() > 0)
  246. {
  247. pClassName = (TCHAR *)node.m_Class.c_str();
  248. }
  249. if(node.m_Name.length() > 0)
  250. {
  251. pWindowName = (TCHAR *)node.m_Name.c_str();
  252. }
  253. HWND subhandle = FindWindowEx(Parent,NULL,pClassName,pWindowName);
  254. //Step1 : find sub sub windows
  255. if(subhandle == NULL)
  256. {
  257. subhandle = FindWindowEx(Parent,NULL,NULL,NULL);
  258. while(subhandle)
  259. {
  260. if(WindFuncs::CheckTheProcess(subhandle,node) == FALSE)
  261. {
  262. //get next window
  263. subhandle = FindWindowEx(Parent,subhandle,NULL,NULL);
  264. continue;
  265. }
  266. //dig more Z
  267. if(WindFuncs::LocateWindows(subhandle,node,reslist,Visible,singleSearch) == TRUE && singleSearch == TRUE)
  268. {
  269. return TRUE;
  270. }
  271. //get next window
  272. subhandle = FindWindowEx(Parent,subhandle,NULL,NULL);
  273. }
  274. if(reslist.size() > 0)
  275. {
  276. return TRUE;
  277. }
  278. return FALSE;
  279. }
  280. BOOL sizeMatch;
  281. ULONG X,Y;
  282. Ocr_Dict dict;
  283. OcrScreen screen;
  284. //Step2: found some
  285. while(subhandle)
  286. {
  287. //for test
  288. //if(subhandle == (HWND)0x00050A26)
  289. //{
  290. // CRect area;
  291. // GetWindowRect(subhandle,&area);
  292. // subhandle = subhandle;
  293. //}
  294. if(WindFuncs::CheckTheProcess(subhandle,node) == FALSE)
  295. {
  296. //get next window
  297. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  298. continue;
  299. }
  300. //for test
  301. //if(GetWindowLong(subhandle,GWL_ID) == 0x00000053)
  302. //{
  303. // subhandle = subhandle;
  304. //}
  305. //do ID check
  306. if((node.m_GuiID == NO_GUIID) || (node.m_GuiID != NO_GUIID && node.m_GuiID == GetWindowLong(subhandle,GWL_ID)))
  307. {
  308. subIDMatch = TRUE;
  309. }
  310. else
  311. {
  312. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  313. continue;
  314. }
  315. //do style check
  316. if((node.m_WndType == NO_WNDTYPE) || (node.m_WndType != NO_WNDTYPE && node.m_WndType == GetWindowLong(subhandle,GWL_STYLE)))
  317. {
  318. subTypeMatch = TRUE;
  319. }
  320. else
  321. {
  322. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  323. continue;
  324. }
  325. if((node.m_WndTypeEx == NO_WNDTYPE) || (node.m_WndTypeEx != NO_WNDTYPE && node.m_WndTypeEx == GetWindowLong(subhandle,GWL_EXSTYLE)))
  326. {
  327. subTypeExMatch = TRUE;
  328. }
  329. else
  330. {
  331. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  332. continue;
  333. }
  334. //do size check
  335. RECT Rect1;
  336. GetWindowRect(subhandle,&Rect1);
  337. sizeMatch = TRUE;
  338. if(node.m_Width > 0)
  339. {
  340. if(node.m_Width != (Rect1.right - Rect1.left))
  341. {
  342. sizeMatch = FALSE;
  343. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  344. continue;
  345. }
  346. }
  347. if(node.m_Height > 0)
  348. {
  349. if(node.m_Height != (Rect1.bottom - Rect1.top))
  350. {
  351. sizeMatch = FALSE;
  352. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  353. continue;
  354. }
  355. }
  356. BOOL orc_match = TRUE;
  357. if(IsWindowVisible(subhandle) == TRUE && node.m_Dict.size() > 0 && node.m_DictKey.size() > 0)
  358. {
  359. //do ocr check
  360. RECT area = node.m_DictArea;
  361. if (GetRectWidth(area) == 0 || GetRectHeight(area) == 0)
  362. {
  363. GetWindowRect(subhandle,&area);
  364. OffsetRect(&area, -area.left, -area.top);
  365. }
  366. if(dict.LoadOcrDictFromDirectory(node.m_Dict.c_str()) == TRUE)
  367. {
  368. screen.UpdateScreen(subhandle);
  369. //screen.SaveTheAreaToBitMapFileWithName(L"test.bmp");
  370. if(FindMatchPoint(dict,screen,area,COLORREF(node.m_DictRgb),100,node.m_DictKey,X,Y) == TRUE)
  371. {
  372. node.m_ResDictKey.x = (INT)X;
  373. node.m_ResDictKey.y = (INT)Y;
  374. }
  375. else
  376. {
  377. orc_match = FALSE;
  378. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  379. continue;
  380. }
  381. }
  382. else
  383. {
  384. return FALSE;
  385. }
  386. }
  387. //for test
  388. //if((Rect1.right - Rect1.left) == 116 && (Rect1.bottom - Rect1.top) == 52)
  389. //{
  390. // _tprintf(_T("Hited:%d,%d,%d\n"),subTypeMatch,subTypeExMatch,sizeMatch);
  391. //}
  392. if(subTypeMatch == TRUE && subTypeExMatch == TRUE && sizeMatch == TRUE && subIDMatch == TRUE && orc_match == TRUE)
  393. {
  394. //visible
  395. if(Visible == FALSE || IsWindowVisible(subhandle) == TRUE)
  396. {
  397. WindNode nodeTemp = node;
  398. nodeTemp.m_ResZOrder = GetZOrder(subhandle);
  399. nodeTemp.m_ResHwnd = subhandle;
  400. reslist.push_back(nodeTemp);
  401. if(singleSearch == TRUE)
  402. {
  403. return TRUE;
  404. }
  405. }
  406. }
  407. //get next window
  408. subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
  409. }
  410. //Step3: Step2 failed
  411. subhandle = FindWindowEx(Parent,NULL,NULL,NULL);
  412. while(subhandle)
  413. {
  414. //dig more Z
  415. if(WindFuncs::LocateWindows(subhandle,node,reslist,Visible,singleSearch) == TRUE && singleSearch == TRUE)
  416. {
  417. return TRUE;
  418. }
  419. //get next window
  420. subhandle = FindWindowEx(Parent,subhandle,NULL,NULL);
  421. }
  422. if(reslist.size() > 0)
  423. {
  424. return TRUE;
  425. }
  426. return FALSE;
  427. }