1.得到窗口名
CString strWindowName;
strWindowName="xxx";
//如果搜索word窗口名,则为文件名+“_Microsoft Word”,而且还要注意文件名是否带扩展名
2.查找窗口句柄
HWND hwnd = ::FindWindow(NULL, strWindowName);
3.设置窗口位置
::SetWindowPos(hwnd, //窗口句柄
HWND_TOPMOST, //窗口排序
x, //左边界坐标
y, //上边界坐标
cx, //宽
cy, //高
SWP_SHOWWINDOW ); //窗口显示方式
4.激活窗口
::SetActveWindow(hwnd);
5.遍历窗口内的控件
::EnumChildWindows(hwnd, //窗口句柄
(WNDENUMPROC)lpEnumFunc, //遍历控件回调函数,将窗口内控件的句柄传入lpEnumFunc
NULL);
范例:
BOOL CALLBACK lpEnumProc(HWND hwnd /*窗口内控件句柄*/, LPARAM lParam)
{
char* buffer[501]={0};
LRESULT nRecvNum=0;
if (hwnd == NULL)
return FALSE;
if (::IsWindow(hwnd) && ::IsWindowVisible(hwnd) )
{
::GetWindowTextA(hwnd, buffer, 500); //获得控件上的内容
if(::GetWindowTextLengthA(hwnd)==0) //如果控件是editBox,则需要通过发送消息获得
{
//length=::SendMessageA(hwnd, WM_GETTEXTLENGTH, 0, 0);
nRecvNum=::SendMessageA(hwnd,WM_GETTEXT,(WPARAM)500, (LPARAM)buffer);
if(nRecvNum==0)
return FALSE;
}
}
TRACE(buffer);
return TRUE;
}
void GetText()
{
CString strWindowName;
strWindowName = L"计算器";
HWND hwnd = NULL;
if( (hwnd = ::FindWindowW(NULL, strWindowName )) == NULL )
{
printf("Can not find the window");
return;
}
::SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW); //将窗口置顶
CenterWindow(hwnd); //将窗口居中
::EnumChildWindows(hwnd,(WNDENUMPROC)lpEnumProc, 0); //遍历窗口内的控件
}