We can automate command button click or check box of another running application using the following sample code.
To automate, first we need the window handle of the application that we need to automate. We can get the handle by using FindWindow() API.
To automate, first we need the window handle of the application that we need to automate. We can get the handle by using FindWindow() API.
struct stUserInterFace { HWND handle; LPCTSTR lpCaption; }; BOOL CALLBACK GetAllButtons(HWND handle,LPARAM lpParam) { stUserInterFace *ptrStUI=NULL; ptrStUI=(stUserInterFace*)lpParam; HWND hwndBtn=ptrStUI->handle; TCHAR szClassName[128]; GetClassName(handle,(LPSTR)&szClassName,sizeof(szClassName)); if (_tcscmp(szClassName,_T("ThunderRT6CommandButton")) == 0 ||_tcscmp(szClassName,_T("Button")) == 0 ) { TCHAR szCaption[128]; GetWindowText(handle,(LPSTR)&szCaption,sizeof(szCaption)); if (_tcscmp(szCaption,ptrStUI->lpCaption) == 0) { ptrStUI->handle = handle; return FALSE; // no more child windows } } return TRUE; } BOOL ClickButton(HWND hWnd,LPCTSTR lpButtonCaption) { BOOL bRet=FALSE; HWND hwndBtn=NULL; stUserInterFace *ptrUI=new stUserInterFace; ptrUI->lpCaption=lpButtonCaption; EnumChildWindows(hWnd,GetAllButtons,(LPARAM)ptrUI); if (ptrUI->handle != NULL) { PostMessage(ptrUI->handle,BM_CLICK,0,0); bRet=TRUE; } delete ptrUI; return bRet; } BOOL CheckBox(HWND hWnd,LPCTSTR lpButtonCaption) { BOOL bRet=FALSE; HWND hwndBtn=NULL; stUserInterFace *ptrUI=new stUserInterFace; ptrUI->lpCaption=lpButtonCaption; EnumChildWindows(hWnd,GetAllButtons,(LPARAM)ptrUI); if (ptrUI->handle != NULL) { PostMessage(ptrUI->handle,BM_SETCHECK,1,0); bRet=TRUE; } delete ptrUI; return bRet; }
Usage:
ShellExecute(NULL,NULL,"Test.EXE",NULL,NULL,SW_SHOW); Sleep(2000); HWND hwnd=::FindWindow(NULL,"Test"); if(hwnd!=NULL) { ClickButton(hwnd,"TestButton"); }
No comments:
Post a Comment