123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- #include "StdAfx.h"
- #include <vector>
- #include "Common_Funcs.h"
- #include "WindNode.h"
- #include "AutoFunc.h"
- #include "Common_def.h"
- #include "XMLLoader.h"
- #include "WindFuncs.h"
- //#include "debug.h"
- using namespace std;
- WindowObject::WindowObject(void)
- {
- m_SubNodes = new map<string, WindowObject*>();
- m_ConfigName = new string();
- m_ID = new string();
- m_Process = new string();
- m_ReqTitle = new string();
- m_Class = new string();
- m_Dict = new string();
- m_DictKey = new string();
- m_ExistTitle = new string();
- ClearObject();
- }
- WindowObject::~WindowObject(void)
- {
- ClearObject();
- delete m_SubNodes;
- m_SubNodes = NULL;
- delete m_ConfigName;
- m_ConfigName = NULL;
- delete m_ID;
- m_ID = NULL;
- delete m_Process;
- m_Process = NULL;
- delete m_ReqTitle;
- m_ReqTitle = NULL;
- delete m_Class;
- m_Class = NULL;
- delete m_Dict;
- m_Dict = NULL;
- delete m_DictKey;
- m_DictKey = NULL;
- delete m_ExistTitle;
- m_ExistTitle = NULL;
- }
- void WindowObject::ClearLocateData()
- {
- m_ResHwnd = 0;
- m_ResDictKey.x = -1;
- m_ResDictKey.y = -1;
- for (map<string, WindowObject*>::iterator iter1 = m_SubNodes->begin(); iter1 != m_SubNodes->end(); iter1++)
- {
- iter1->second->ClearLocateData();
- }
- }
- void WindowObject::ClearObject()
- {
- m_Style = NO_WNDTYPE;
- m_StyleEx = NO_WNDTYPE;
- m_zOrder = ZORDER_ALLSEARCH;
- m_Width = 0;
- m_Height = 0;
- m_ResHwnd = 0;
- (*m_ConfigName) = _T("");
- (*m_ID) = _T("");
- (*m_Process) = _T("");
- (*m_ReqTitle) = _T("");
- (*m_ExistTitle) = _T("");
- (*m_Class) = _T("");
- m_GuiID = NO_GUIID;
- m_pParent = NULL;
- m_DictRgb = 0;
- memset(&m_DictArea,0,sizeof(RECT));
- m_ResDictKey.x = -1;
- m_ResDictKey.y = -1;
- for(map<string,WindowObject*>::iterator iter1 = m_SubNodes->begin();iter1 != m_SubNodes->end();iter1++)
- {
- //iter1->second->ClearObject();
- delete iter1->second;
- }
- m_SubNodes->clear();
- }
- BOOL WindowObject::MakeParentTree()
- {
- BOOL ret = FALSE;
- map<string, WindowObject*>::iterator iter1 = m_SubNodes->begin();
- for(; iter1 != m_SubNodes->end();iter1++)
- {
- iter1->second->m_pParent = this;
- ret = iter1->second->MakeParentTree();
- if(ret == FALSE)
- {
- return ret;
- }
- }
- return TRUE;
- }
- BOOL WindowObject::LoadConfig(const TCHAR * pRFilePath)
- {
- BOOL ret = FALSE;
- XMLLoader loader;
- ClearObject();
- string filepath = pRFilePath;
- if(loader.LoadFile(filepath))
- {
- ret = loader.GetWindowObject(*this);
- if(ret)
- {
- (*m_ConfigName) = filepath;
- //make parent
- if(MakeParentTree() == FALSE)
- {
- ret = FALSE;
- }
- }
- }
- return ret;
- }
- WindowObject& WindowObject::operator = (WindNode &node)
- {
- (*(m_Process)) = node.m_ProcessName;
- (*(m_Class)) = node.m_Class;
- (*m_ReqTitle) = node.m_Name;
- m_Style = node.m_WndType;
- m_StyleEx = node.m_WndTypeEx;
- m_Width = node.m_Width;
- m_Height = node.m_Height;
- m_zOrder = node.m_ResZOrder;
- m_GuiID = node.m_GuiID;
- m_ResHwnd = node.m_ResHwnd;//res hwnd
- (*m_Dict) = node.m_Dict;
- (*m_DictKey) = node.m_DictKey;
- m_DictRgb = node.m_DictRgb;
- m_DictArea = node.m_DictArea;
- m_ResDictKey = node.m_ResDictKey;//res point
- return *this;
- }
- BOOL FilterWindowsByParent(WindowObject *pObj,vector<WindNode> &list1)
- {
- //call in when this obj located
- BOOL ret = FALSE;
- //no parent
- if(pObj->m_pParent == NULL)
- {
- return TRUE;
- }
-
- //parent exist
- for(vector<WindNode>::iterator iter1 = list1.begin();iter1 != list1.end();)
- {
- //zOrder check
- if(WindFuncs::GetZOrder(iter1->m_ResHwnd) == (ZORDER_DESKTOP+1))
- {
- iter1 = list1.erase(iter1);
- continue;
- }
- //window check,
- //realparentHwnd VS parentWindowObject
- HWND parent = WindFuncs::GetTrueParent(iter1->m_ResHwnd);
- if(WindFuncs::CheckWindowMatch(parent,(WindNode)*pObj->m_pParent) == FALSE)
- {
- iter1 = list1.erase(iter1);
- continue;
- }
- //parent is ok,do check upper layer
- vector<WindNode> parentlist;
- pObj->m_pParent->m_ResHwnd = parent;
- parentlist.push_back((WindNode)*pObj->m_pParent);
- if(FilterWindowsByParent(pObj->m_pParent,parentlist) == FALSE)
- {
- iter1 = list1.erase(iter1);
- continue;
- }
- iter1++;
- }
- return (list1.size() > 0);
- }
- INT WindowObject::LocateWindow(BOOL Visible)
- {
- WindNode node = (*this);
- vector<WindNode> list1;
- HWND pParent = NULL;
- if(m_pParent && m_pParent->m_ResHwnd)
- {
- pParent = m_pParent->m_ResHwnd;
- }
- WindFuncs::LocateWindows(pParent,node,list1,Visible);
- if(list1.size() == 0)
- {
- return -1;//none exist
- }
- //do check parent
- if(FilterWindowsByParent(this,list1) == FALSE)
- {
- return -1;//none exist
- }
- //do it again
- if(FilterWindowsByParent(this,list1) == FALSE)
- {
- return -1;//none exist
- }
- //if(m_SubNodes->size() == 0)
- {
- (*this) = list1[list1.size() - 1];
- if(list1.size() > 1)
- {
- //multiple result
- return 0;
- }
- //search one - got one
- return 1;
- }
- //DWORD HitIdx = list1.size() - 1;
- //multiple results && multiple sub nodes
- //if we go dig deeper,the level2 window might not exist.
- //just find the sub windows in the level1.
- //vector<WindNode> list_match;
- //for(DWORD i = 0;i < list1.size();i++)
- //{
- // //
- // BOOL match_one = TRUE;
- // vector<WindNode> list_sub;
- //
- // for(map<wstring,WindowObject>::iterator iter1 = m_SubNodes->begin();iter1 != m_SubNodes->end();iter1++)
- // {
- // list_sub.clear();
- // if(WindFuncs::LocateWindows(list1[HitIdx].m_ResHwnd,(WindNode)iter1->second,list_sub) == FALSE)
- // {
- // match_one = FALSE;
- // }
- // else
- // {
- // //check one level distance
- // for(DWORD j = 0;j < list_sub.size();j++)
- // {
- // if(list1[HitIdx].m_ResZOrder != (list_sub[j].m_ResZOrder - 1))
- // {
- // match_one = FALSE;
- // break;
- // }
- // }
- // }
- // if(match_one == FALSE)
- // {
- // break;
- // }
- // }
- // //got one match cur windowObject
- // if(match_one)
- // {
- // list_match.push_back(list1[HitIdx]);
- // }
- //}
- ////got match
- //if(list_match.size() > 0)
- //{
- // (*this) = list_match[0];//get first one
- // if(list_match.size() == 1)
- // {
- // return 1;
- // }
- // return 0;
- //}
- //(*this) = list1[list1.size() - 1];
- //return 0;
- }
- BOOL WindowObject::DoClick(BOOL real)
- {
- BOOL ret = MouseMove(real);
- ret &= ClickLB(real);
- return ret;
- }
- INT WindowObject::WaitHideWindow(TCHAR *pID, HITMETHOD hm, DWORD tmPeriod, BOOL Exp, DWORD moretry)
- {
- INT ret = 0;
- do
- {
- DWORD usedTmPeriod = 0;
- DWORD ConfirmTime = 2000;
- //Step1:locate Visible window
- if (pID)
- {
- ret = (*this)[pID].WaitLocateWindow(ConfirmTime, TRUE, Exp);
- }
- else
- {
- ret = WaitLocateWindow(ConfirmTime, TRUE, Exp);
- }
- if (ret <= 0)
- {
- return 1;
- }
- //Step2: operation
- if (pID)
- {
- switch (hm)
- {
- case HM_NOTIFY_PARENT:
- (*this)[pID].NotifyParentHitButton();
- break;
- case HM_HIT:
- (*this)[pID].DoClick();
- break;
- case HM_CLICK:
- (*this)[pID].HitMouse(WM_LBUTTONDOWN, 0, 10, 10);
- Sleep(100);
- (*this)[pID].HitMouse(WM_LBUTTONUP, 0, 10, 10);
- break;
- case HM_REALCLICK:
- (*this)[pID].DoClick(TRUE);
- break;
- default:
- if (Exp)
- {
- throw ExpObject("WaitHideWindow HM type is wrong");
- }
- return -1;
- break;
- }
- }
- else
- {
- switch (hm)
- {
- case HM_NOTIFY_PARENT:
- (*this).NotifyParentHitButton();
- break;
- case HM_HIT:
- (*this).ClickLB();
- break;
- case HM_CLICK:
- (*this).HitMouse(WM_LBUTTONDOWN, 0, 10, 10);
- Sleep(100);
- (*this).HitMouse(WM_LBUTTONUP, 0, 10, 10);
- break;
- case HM_REALCLICK:
- (*this)[pID].DoClick(TRUE);
- break;
- default:
- if (Exp)
- {
- throw ExpObject("WaitHideWindow HM type is wrong");
- }
- return -1;
- break;
- }
- }
- //Step3:wait invisible
- do
- {
- Sleep(250);
- usedTmPeriod += 250;
- if (pID)
- {
- ret = (*this)[pID].IsObjectVisible();
- }
- else
- {
- ret = IsObjectVisible();
- }
- if (ret <= 0)
- {
- return 1;
- }
- if (usedTmPeriod > tmPeriod)
- {
- break;
- }
- } while (usedTmPeriod < tmPeriod);
- } while (moretry--);
- if(ret > 0)
- {
- if(Exp)
- {
- string errMsg = _T("timeout.couldn't hide window::ID--");
- errMsg += (*m_ConfigName);
- errMsg += _T(":");
- errMsg += (*m_ID);
- throw ExpObject(errMsg.c_str());
- }
- return 0;
- }
- return 1;
- }
- INT WindowObject::IsObjectVisible()
- {
- if(m_ResHwnd)
- {
- if(IsWindowVisible(m_ResHwnd))
- {
- return 1;
- }
- return 0;
- }
- return -1;
- }
- INT WindowObject::WaitLocateWindow(DWORD tmPeriod,BOOL Visible,BOOL Exp)
- {
- INT ret = 0;
- DWORD usedTmPeriod = 0;
- do
- {
- ret = LocateWindow(Visible);
- if(ret != -1)
- {
- break;
- }
- Sleep(50);
- if(tmPeriod == INFINITE)
- {
- continue;
- }
- usedTmPeriod += 50;
- if(usedTmPeriod > tmPeriod)
- {
- break;
- }
- }
- while(usedTmPeriod < tmPeriod);
- //if(ret < 0)
- //{
- // if(Exp)
- // {
- // wstring errMsg = L"no window Hwnd. window::ID--";
- // errMsg += (*m_ConfigName);
- // errMsg += L":";
- // errMsg += (*m_ID);
- // throw ExpObject(errMsg.c_str());
- // }
- //}
- //
- if(ret < 0)
- {
- if(Exp)
- {
- string errMsg = _T("timeout.couldn't find window::ID--");
- errMsg += (*m_ConfigName);
- errMsg += ":";
- errMsg += (*m_ID);
- throw ExpObject(errMsg.c_str());
- }
- }
- return ret;
- }
- BOOL WindowObject::SendMessageTo(DWORD msg,DWORD wp,DWORD lp,BOOL sendFlag)
- {
- if(sendFlag)
- {
- SendMessage(m_ResHwnd,msg,(WPARAM)wp,(LPARAM)lp);
- }
- else
- {
- if(PostMessage(m_ResHwnd,msg,(WPARAM)wp,(LPARAM)lp) == 0)
- {
- DWORD errCode = GetLastError();
- //DebugPrint(_T("Failed PostMessage To ID:%s,code:%d\r\n"),m_ID.c_str(),errCode);
- return FALSE;
- }
- }
- return TRUE;
- }
- BOOL WindowObject::MakeFocus(BOOL real)
- {
- return InputCmds::MakeFocus(m_ResHwnd,real);
- }
- BOOL WindowObject::SetText(TCHAR *pContext,BOOL real)
- {
- return InputCmds::SetText(m_ResHwnd,pContext,real);
- }
- BOOL WindowObject::NotifyParentHitButton()
- {
- return InputCmds::NotifyParentHitButton(m_ResHwnd);
- }
- BOOL WindowObject::ClickLB(BOOL real)
- {
- return InputCmds::ClickLB(m_ResHwnd, real);
- }
- BOOL WindowObject::HitVKey(DWORD vk,BOOL real)
- {
- return InputCmds::HitKey(m_ResHwnd,vk,real);
- }
- BOOL WindowObject::HitKey(DWORD vk,BOOL real)
- {
- return InputCmds::HitKey(m_ResHwnd,vk,real);
- }
- BOOL WindowObject::MouseMove(BOOL real)
- {
- return InputCmds::MouseMove(m_ResHwnd,real);
- }
- BOOL WindowObject::MouseMove(int x,int y,BOOL real)
- {
- return InputCmds::MouseMove(m_ResHwnd,x,y,real);
- }
- //MsgType including
- //WM_LBUTTONDOWN
- //WM_LBUTTONUP
- //WM_LBUTTONDBLCLK
- //RIGHT
- //...
- //MIDDLE
- //...
- //ExKey including below
- //MK_CONTROL
- //MK_LBUTTON
- //MK_MBUTTON
- //MK_RBUTTON
- //MK_SHIFT
- //normal-----------------
- //InputCmds::HitMouse(curWind,WM_LBUTTONDBLCLK,MK_LBUTTON,10,10);
- //InputCmds::HitMouse(curWind,WM_LBUTTONUP,0,10,10);
- //real-----------------
- //InputCmds::HitMouse(curWind,MOUSEEVENTF_LEFTDOWN,MK_LBUTTON,10,10);
- //InputCmds::HitMouse(curWind,MOUSEEVENTF_LEFTUP,0,10,10);
- BOOL WindowObject::HitMouse(DWORD MsgType,DWORD ExKey,int x,int y,BOOL real)
- {
- return InputCmds::HitMouse(m_ResHwnd,MsgType,ExKey,x,y,real);
- }
- WindowObject &WindowObject::operator [](TCHAR *pID)
- {
- string keystr = pID;
- map<string,WindowObject*>::iterator iter1 = m_SubNodes->find(keystr);
- if(iter1 != m_SubNodes->end())
- {
- return (*(iter1->second));
- }
- string Msgstr = format_string("No such ID:%s", pID);
- MessageBox(NULL,Msgstr.c_str(),"WTH",MB_OK);
- WindowObject *pObj = new WindowObject();
- return (*pObj);
- }
- HWND WindowObject::GetLocatedHwnd()
- {
- return m_ResHwnd;
- }
- POINT WindowObject::GetLocatedTopLeft()
- {
- return m_ResDictKey;
- }
- const TCHAR* WindowObject::GetProcessName()
- {
- return m_Process->c_str();
- }
- const TCHAR* WindowObject::GetTitle()
- {
- TCHAR szTitle[MAX_PATH] = {0};
- if(m_ResHwnd)
- {
- AttachThreadInput(
- ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
- ::GetCurrentThreadId(), //自己的线程ID
- TRUE);
- LRESULT lResult = SendMessage( // returns LRESULT in lResult
- m_ResHwnd, // handle to destination control
- (UINT)WM_GETTEXT, // message ID
- (WPARAM)MAX_PATH, // = (WPARAM) () wParam;
- (LPARAM)szTitle // = 0; not used, must be zero
- );
- }
- (*m_ExistTitle) = szTitle;
- return m_ExistTitle->c_str();
- }
- AUTOFUNC_C_API WindowObject* GetWindowObject(void)
- {
- return new WindowObject;
- }
- AUTOFUNC_C_API void ReleaseWindowObject(WindowObject* &pObj)
- {
- delete pObj;
- pObj = NULL;
- }
|