123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- #include "StdAfx.h"
- #include <Psapi.h>
- #include "Ocr_Dict.h"
- #include "ocr_pubfuncs.h"
- #include "AutoFunc.h"
- #include "WindFuncs.h"
- #include "Common_Funcs.h"
- #pragma comment(lib, "Psapi.lib")
- WindFuncs::WindFuncs(void)
- {
- }
- WindFuncs::~WindFuncs(void)
- {
- }
- HWND WindFuncs::GetTrueParent(HWND hwnd)
- {
- DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
- if((dwStyle & WS_CHILD) == WS_CHILD)
- return GetParent(hwnd);
- else
- return GetDesktopWindow();
- }
- BOOL WindFuncs::CmpNoCase(string &src,string &des)
- {
- string::const_iterator p = src.begin();
- string::const_iterator p2 = des.begin();
- if(src.length() != des.length())
- {
- return FALSE;
- }
- while(p!=src.end() && p2!=des.end())
- {
- if(toupper(*p)!=toupper(*p2))
- {
- return FALSE;
- }
- ++p;
- ++p2;
- }
-
- return TRUE;
- }
- BOOL WindFuncs::LocateWindowsWithRegEx(HWND Parent,WindNode &node,vector<WindNode> &reslist,BOOL Visible,BOOL singleSearch)
- {
- return NULL;
- }
- UINT WindFuncs::GetZOrder(HWND Wnd)
- {
- UINT zOrder = 0;
- //locate parent
- if(Wnd != NULL)
- {
- HWND desk = GetDesktopWindow();
- while(Wnd && Wnd != desk)
- {
- Wnd = GetTrueParent(Wnd);
- ++zOrder;
- }
- }
- return zOrder;
- }
- BOOL WindFuncs::CheckTheProcess(HWND Wnd,WindNode &node)
- {
- DWORD pid = 0;
- if(node.m_ProcessName.length() == 0)
- {
- return TRUE;
- }
- GetWindowThreadProcessId(Wnd,&pid);
- HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
- TCHAR* procName = new TCHAR[MAX_PATH];
- memset(procName, 0, sizeof(TCHAR)*MAX_PATH);
- if(GetModuleFileNameEx(hProcess,NULL,procName,MAX_PATH) == 0)
- {
- LPVOID lpMsgBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0, NULL );
- // Display the error message and exit the process
- string msgContent = (TCHAR*)lpMsgBuf;
- //MessageBox(NULL, (LPCTSTR)msgContent.GetBuffer(), TEXT("Error"), MB_OK);
- LocalFree(lpMsgBuf);
- CloseHandle(hProcess);
- delete []procName;
- return FALSE;
- }
- string prc = procName;
- CloseHandle(hProcess);
- delete []procName;
- std::size_t found = prc.find_last_of("\\");
- string filename = prc.substr(found+1);
- if(filename.length() > 0)
- {
- if(WindFuncs::CmpNoCase(node.m_ProcessName , filename))
- {
- return TRUE;
- }
- }
- return FALSE;
- }
- WindNode WindFuncs::LocateOneWindow(HWND Parent,WindNode &node,BOOL Visible)
- {
- WindNode nodeLocal;
- vector<WindNode> list1;
- WindFuncs::LocateWindows(Parent,node,list1,Visible,1);
- if(list1.size() > 0)
- {
- nodeLocal = list1[0];
- return nodeLocal;
- }
- return nodeLocal;
- }
- BOOL WindFuncs::CheckWindowMatch(HWND subhandle,WindNode &node,BOOL Visible)
- {
- BOOL sizeMatch = FALSE;
- BOOL subIDMatch = FALSE;
- BOOL subTypeMatch = FALSE;
- BOOL subTypeExMatch = FALSE;
- TCHAR szTitle[MAX_PATH] = {0};
- if(WindFuncs::CheckTheProcess(subhandle,node) == FALSE)
- {
- //failed
- return FALSE;
- }
- //name compare
- if(node.m_Name.size() > 0)
- {
- LRESULT lResult = SendMessage( // returns LRESULT in lResult
- subhandle, // handle to destination control
- (UINT)WM_GETTEXT, // message ID
- (WPARAM)MAX_PATH, // = (WPARAM) () wParam;
- (LPARAM)szTitle // = 0; not used, must be zero
- );
- if(strcmp(szTitle,node.m_Name.c_str()) != 0)
- {
- return FALSE;
- }
- }
- //class compare
- if(node.m_Class.size() > 0)
- {
- memset(szTitle, 0, sizeof(TCHAR)*MAX_PATH);
- GetClassName(subhandle,szTitle,MAX_PATH);
- if (strcmp(szTitle, node.m_Class.c_str()) != 0)
- {
- return FALSE;
- }
- }
- //do ID check
- if((node.m_GuiID == NO_GUIID) || (node.m_GuiID != NO_GUIID && node.m_GuiID == GetWindowLong(subhandle,GWL_ID)))
- {
- subIDMatch = TRUE;
- }
- //for test
- LONG wndType = GetWindowLong(subhandle,GWL_STYLE);
- //do style check
- if((node.m_WndType == NO_WNDTYPE) || (node.m_WndType != NO_WNDTYPE && node.m_WndType == GetWindowLong(subhandle,GWL_STYLE)))
- {
- subTypeMatch = TRUE;
- }
- if((node.m_WndTypeEx == NO_WNDTYPE) || (node.m_WndTypeEx != NO_WNDTYPE && node.m_WndTypeEx == GetWindowLong(subhandle,GWL_EXSTYLE)))
- {
- subTypeExMatch = TRUE;
- }
- //do size check
- RECT Rect1;
- GetWindowRect(subhandle,&Rect1);
- sizeMatch = TRUE;
- if(node.m_Width > 0)
- {
- if(node.m_Width != (Rect1.right - Rect1.left))
- {
- sizeMatch = FALSE;
- }
- }
- if(node.m_Height > 0)
- {
- if(node.m_Height != (Rect1.bottom - Rect1.top))
- {
- sizeMatch = FALSE;
- }
- }
- //for test
- //if((Rect1.right - Rect1.left) == 116 && (Rect1.bottom - Rect1.top) == 52)
- //{
- // _tprintf(_T("Hited:%d,%d,%d\n"),subTypeMatch,subTypeExMatch,sizeMatch);
- //}
- if(subTypeMatch == TRUE && subTypeExMatch == TRUE && sizeMatch == TRUE && subIDMatch == TRUE)
- {
- //visible
- if(Visible == FALSE || IsWindowVisible(subhandle) == TRUE)
- {
- if(IsWindowVisible(subhandle) == TRUE && node.m_Dict.size() > 0 && node.m_DictKey.size() > 0)
- {
- //do ocr check
- ULONG X,Y;
- Ocr_Dict dict;
- OcrScreen screen;
- screen.UpdateScreen(subhandle);
- //screen.SaveTheAreaToBitMapFileWithName(L"test.bmp");
- RECT area = node.m_DictArea;
- if(dict.LoadOcrDictFromDirectory(node.m_Dict.c_str()) == TRUE)
- {
- if(FindMatchPoint(dict,screen,area,COLORREF(node.m_DictRgb),100,node.m_DictKey,X,Y) == TRUE)
- {
- node.m_ResDictKey.x = (INT)X;
- node.m_ResDictKey.y = (INT)Y;
- return TRUE;
- }
- }
- return FALSE;
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL WindFuncs::LocateWindows(HWND Parent,WindNode &node,vector<WindNode> &reslist,BOOL Visible,BOOL singleSearch)
- {
- BOOL subIDMatch = FALSE;
- BOOL subTypeMatch = FALSE;
- BOOL subTypeExMatch = FALSE;
- HWND sshandle = NULL;
- TCHAR *pClassName = NULL;
- TCHAR *pWindowName = NULL;
- if(node.RegExSearch())
- {
- return WindFuncs::LocateWindowsWithRegEx(Parent,node,reslist,Visible,singleSearch);
- }
- if(Parent == NULL)
- {
- Parent = GetDesktopWindow();
- }
- UINT ParentzOrder = WindFuncs::GetZOrder(Parent);
- if(node.DigDeeper(ParentzOrder + 1) == FALSE)
- {
- return FALSE;
- }
- if(node.m_Class.length() > 0)
- {
- pClassName = (TCHAR *)node.m_Class.c_str();
- }
- if(node.m_Name.length() > 0)
- {
- pWindowName = (TCHAR *)node.m_Name.c_str();
- }
- HWND subhandle = FindWindowEx(Parent,NULL,pClassName,pWindowName);
- //Step1 : find sub sub windows
- if(subhandle == NULL)
- {
- subhandle = FindWindowEx(Parent,NULL,NULL,NULL);
- while(subhandle)
- {
- if(WindFuncs::CheckTheProcess(subhandle,node) == FALSE)
- {
- //get next window
- subhandle = FindWindowEx(Parent,subhandle,NULL,NULL);
- continue;
- }
- //dig more Z
- if(WindFuncs::LocateWindows(subhandle,node,reslist,Visible,singleSearch) == TRUE && singleSearch == TRUE)
- {
- return TRUE;
- }
- //get next window
- subhandle = FindWindowEx(Parent,subhandle,NULL,NULL);
- }
- if(reslist.size() > 0)
- {
- return TRUE;
- }
- return FALSE;
- }
- BOOL sizeMatch;
- ULONG X,Y;
- Ocr_Dict dict;
- OcrScreen screen;
- //Step2: found some
- while(subhandle)
- {
- //for test
- //if(subhandle == (HWND)0x00050A26)
- //{
- // CRect area;
- // GetWindowRect(subhandle,&area);
- // subhandle = subhandle;
- //}
- if(WindFuncs::CheckTheProcess(subhandle,node) == FALSE)
- {
- //get next window
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- //for test
- //if(GetWindowLong(subhandle,GWL_ID) == 0x00000053)
- //{
- // subhandle = subhandle;
- //}
- //do ID check
- if((node.m_GuiID == NO_GUIID) || (node.m_GuiID != NO_GUIID && node.m_GuiID == GetWindowLong(subhandle,GWL_ID)))
- {
- subIDMatch = TRUE;
- }
- else
- {
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- //do style check
- if((node.m_WndType == NO_WNDTYPE) || (node.m_WndType != NO_WNDTYPE && node.m_WndType == GetWindowLong(subhandle,GWL_STYLE)))
- {
- subTypeMatch = TRUE;
- }
- else
- {
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- if((node.m_WndTypeEx == NO_WNDTYPE) || (node.m_WndTypeEx != NO_WNDTYPE && node.m_WndTypeEx == GetWindowLong(subhandle,GWL_EXSTYLE)))
- {
- subTypeExMatch = TRUE;
- }
- else
- {
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- //do size check
- RECT Rect1;
- GetWindowRect(subhandle,&Rect1);
- sizeMatch = TRUE;
- if(node.m_Width > 0)
- {
- if(node.m_Width != (Rect1.right - Rect1.left))
- {
- sizeMatch = FALSE;
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- }
- if(node.m_Height > 0)
- {
- if(node.m_Height != (Rect1.bottom - Rect1.top))
- {
- sizeMatch = FALSE;
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- }
- BOOL orc_match = TRUE;
- if(IsWindowVisible(subhandle) == TRUE && node.m_Dict.size() > 0 && node.m_DictKey.size() > 0)
- {
- //do ocr check
- RECT area = node.m_DictArea;
- if (GetRectWidth(area) == 0 || GetRectHeight(area) == 0)
- {
- GetWindowRect(subhandle,&area);
- OffsetRect(&area, -area.left, -area.top);
- }
- if(dict.LoadOcrDictFromDirectory(node.m_Dict.c_str()) == TRUE)
- {
- screen.UpdateScreen(subhandle);
- //screen.SaveTheAreaToBitMapFileWithName(L"test.bmp");
- if(FindMatchPoint(dict,screen,area,COLORREF(node.m_DictRgb),100,node.m_DictKey,X,Y) == TRUE)
- {
- node.m_ResDictKey.x = (INT)X;
- node.m_ResDictKey.y = (INT)Y;
- }
- else
- {
- orc_match = FALSE;
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- continue;
- }
- }
- else
- {
- return FALSE;
- }
- }
- //for test
- //if((Rect1.right - Rect1.left) == 116 && (Rect1.bottom - Rect1.top) == 52)
- //{
- // _tprintf(_T("Hited:%d,%d,%d\n"),subTypeMatch,subTypeExMatch,sizeMatch);
- //}
- if(subTypeMatch == TRUE && subTypeExMatch == TRUE && sizeMatch == TRUE && subIDMatch == TRUE && orc_match == TRUE)
- {
- //visible
- if(Visible == FALSE || IsWindowVisible(subhandle) == TRUE)
- {
- WindNode nodeTemp = node;
- nodeTemp.m_ResZOrder = GetZOrder(subhandle);
- nodeTemp.m_ResHwnd = subhandle;
- reslist.push_back(nodeTemp);
- if(singleSearch == TRUE)
- {
- return TRUE;
- }
- }
- }
- //get next window
- subhandle = FindWindowEx(Parent,subhandle,pClassName,pWindowName);
- }
- //Step3: Step2 failed
- subhandle = FindWindowEx(Parent,NULL,NULL,NULL);
- while(subhandle)
- {
- //dig more Z
- if(WindFuncs::LocateWindows(subhandle,node,reslist,Visible,singleSearch) == TRUE && singleSearch == TRUE)
- {
- return TRUE;
- }
- //get next window
- subhandle = FindWindowEx(Parent,subhandle,NULL,NULL);
- }
- if(reslist.size() > 0)
- {
- return TRUE;
- }
- return FALSE;
- }
|