虚拟桌面模拟查找点击自绘控件
生活随笔
收集整理的這篇文章主要介紹了
虚拟桌面模拟查找点击自绘控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// VDesktopClick.cpp : 定義控制臺應用程序的入口點。
//#include "stdafx.h" #include <string> #include <Oleacc.h> using namespace std;#define SET_LIST 1 #define SET_BUT 2#pragma comment(lib,"Oleacc.lib")wstring wsTitle = L"title"; wstring wsAppPath = L"adasd\asdasd\asdasd\asd.exe"; DWORD dwX = 60; DWORD dwY = 20;/* 目的:模擬點擊自繪控件,同時不干擾電腦正常工作,也可以理解成不讓使用者察覺到。測試功能:找到title是wsTitle的窗體,將位置移動到用戶看不到的區域,然后以這個窗體為樹根,遍歷窗體上的所有其他小窗體,目標是找到一個自繪的 byutton,尺寸是dwX,dwY。的自繪控件,然后模擬一次點擊。如果界面程序不存在的話就開啟一個虛擬桌面,然后在虛擬桌面上去啟動這個界面程序,然后模擬。假設界面程序的路徑是wsAppPath。 */void GetObjectName(IAccessible* child,VARIANT* varChild,wchar_t* objName,int len) {BSTR strTmp;HRESULT hr = child->get_accName(*varChild,&strTmp);if(S_OK!=hr) {return;}//_bstr_t str = strTmp;//wchar_t* tmp = str;wchar_t* tmp = strTmp;wcscpy_s(objName,MAX_PATH,tmp); } void GetObjectRole(IAccessible* child,VARIANT* varChild,wchar_t* objRole,int len) {VARIANT pvarRole;DWORD roleId;child->get_accRole(*varChild,&pvarRole);if(varChild->vt!=VT_I4) {pvarRole.vt = VT_EMPTY;return /*E_INVALIDARG*/;}roleId = pvarRole.lVal;UINT roleLength;LPTSTR lpszRoleString;// Get the length of the string.roleLength = GetRoleText(roleId,NULL,0);// Allocate memory for the string. Add one character to// the length you got in the previous call to make room// for the null character.lpszRoleString = (LPTSTR)malloc((roleLength+1) * sizeof(TCHAR));if(lpszRoleString!=NULL) {// Get the string.GetRoleText(roleId,lpszRoleString,roleLength+1);}wchar_t* tmp = lpszRoleString;wcscpy_s(objRole,MAX_PATH,tmp);free(lpszRoleString);return /*S_OK*/;}void GetObjectClass(IAccessible* child,wchar_t* objClass,int len) {HWND htmp;LPTSTR strClass;strClass = (LPTSTR)malloc(MAX_PATH);::WindowFromAccessibleObject(child,&htmp);if(0==::GetClassName(htmp,strClass,MAX_PATH)) {free(strClass);return;}wchar_t* tmp = strClass;wcscpy_s(objClass,MAX_PATH,tmp);free(strClass); }BOOL Find(IAccessible* paccParent,IAccessible** paccChild) {HRESULT hr;long numChildren;unsigned long numFetched;VARIANT varChild;int indexCount;IAccessible* pCAcc = NULL;IEnumVARIANT* pEnum = NULL;IDispatch* pDisp = NULL;BOOL found = false;wchar_t szObjName[MAX_PATH],szObjRole[MAX_PATH],szObjClass[MAX_PATH],szObjState[MAX_PATH];//Get the IEnumVARIANT interfacehr = paccParent->QueryInterface(IID_IEnumVARIANT,(PVOID*)&pEnum);if(pEnum){pEnum->Reset();}// Get child countpaccParent->get_accChildCount(&numChildren);for(indexCount = 1; indexCount<=numChildren && !found; indexCount++) {pCAcc = NULL;// Get next childif(pEnum)hr = pEnum->Next(1,&varChild,&numFetched);else {varChild.vt = VT_I4;varChild.lVal = indexCount;}// Get IDispatch interface for the childif(varChild.vt==VT_I4) {pDisp = NULL;hr = paccParent->get_accChild(varChild,&pDisp);}elsepDisp = varChild.pdispVal;// Get IAccessible interface for the childif(pDisp) {hr = pDisp->QueryInterface(IID_IAccessible,(void**)&pCAcc);hr = pDisp->Release();}// Get information about the childif(pCAcc) {VariantInit(&varChild);varChild.vt = VT_I4;varChild.lVal = CHILDID_SELF;*paccChild = pCAcc;}else{*paccChild = paccParent;}ZeroMemory(szObjName,(MAX_PATH<<1));ZeroMemory(szObjRole,(MAX_PATH<<1));ZeroMemory(szObjClass,(MAX_PATH<<1));GetObjectName(*paccChild,&varChild,szObjName,sizeof(szObjName));GetObjectRole(*paccChild,&varChild,szObjRole,sizeof(szObjRole));GetObjectClass(*paccChild,szObjClass,sizeof(szObjClass));LONG px = 0;LONG py = 0;LONG pcx = 0;LONG pcy = 0;(*paccChild)->accLocation(&px,&py,&pcx,&pcy,varChild);if(_wcsicmp(L"Button",szObjClass) == 0){if(pcx==dwX) {if(pcy==dwY) {//wchar_t ccl[1024] = {0};//wsprintf(ccl,L"-----------%s %s %s %d,%d,%d,%d",szObjName,szObjClass,szObjRole,px,py,pcx,pcy);//OutputDebugStringW(ccl);(*paccChild)->accDoDefaultAction(varChild); //模擬點擊運行按鈕Sleep(1500);//模擬點擊,等待按鈕響應。}}}if(!found && pCAcc) {// Go deeperfound = Find(pCAcc,paccChild);if(*paccChild!=pCAcc){pCAcc->Release();}}}// Clean upif(pEnum){pEnum->Release();}return found; }BOOL CALLBACK EnumWindowsProc(HWND hwnd,DWORD lParam){wchar_t temp[200];ZeroMemory(temp,400);GetWindowTextW((HWND)hwnd,temp,200);if(_wcsicmp(wsTitle.c_str(),temp) == 0){OutputDebugStringW(temp);if(lParam == SET_BUT){HWND hwndOneClickBar = (HWND)hwnd;IAccessible* accT = NULL;IAccessible* aaaaaaccT = NULL;HRESULT hr = AccessibleObjectFromWindow(hwndOneClickBar,OBJID_WINDOW,IID_IAccessible,(LPVOID*)&accT);if(FAILED(hr)) {return TRUE;}Find(accT,&aaaaaaccT);}}return TRUE; }void GoClick() { //方案1:直接就能找到界面的情況,界面開著呢HWND hw = ::FindWindow(NULL,wsTitle.c_str());if(hw!=NULL) {::SetWindowPos(hw,0,-100,-100,100,100,SWP_NOZORDER);::SetWindowLong(hw,GWL_HWNDPARENT,WS_EX_TOOLWINDOW);EnumDesktopWindows(0,(WNDENUMPROC)EnumWindowsProc,SET_BUT);CloseHandle(hw);return;}//方案2:需要自己開啟虛擬桌面,然后再啟動界面程序,達到隱藏的目的 #define MAX_B_SIZE 1024wchar_t strS[MAX_B_SIZE] = {0};ZeroMemory(strS,(MAX_B_SIZE<<1));wcscpy_s(strS,MAX_B_SIZE,wsAppPath.c_str());HDESK hDesk = CreateDesktop(L"desktop__vvvv",NULL,NULL,NULL,GENERIC_ALL,NULL);HDESK hDst = GetThreadDesktop(GetCurrentThreadId());SetThreadDesktop(hDesk);STARTUPINFO si = {0};si.cb = sizeof(si);si.lpDesktop = L"desktop__vvvv";si.dwFlags = STARTF_USESHOWWINDOW;si.wShowWindow = SW_SHOW;PROCESS_INFORMATION pi = {0};if(CreateProcess(NULL,strS,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) {Sleep(5000);EnumDesktopWindows(hDesk,(WNDENUMPROC)EnumWindowsProc,SET_BUT);Sleep(10000);}CloseDesktop(hDesk);SetThreadDesktop(hDst);return; }int _tmain(int argc, _TCHAR* argv[]) {GoClick();return 0; }總結
以上是生活随笔為你收集整理的虚拟桌面模拟查找点击自绘控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 导入表编程-枚举导入表
- 下一篇: 安装全局消息钩子实现dll窗体程序注入