查找的是三方软件的窗口句柄
时间: 2024-05-15 21:16:41 浏览: 8
可以使用Windows API函数EnumWindows()和EnumChildWindows()来遍历所有窗口和子窗口,然后根据窗口标题或类名来判断是否是需要查找的窗口,并返回其句柄。示例代码如下:
```c++
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char title[256];
GetWindowText(hwnd, title, sizeof(title));
string strTitle(title);
if (strTitle.find("需要查找的窗口标题") != string::npos)
{
*(HWND*)lParam = hwnd;
return FALSE;
}
return TRUE;
}
BOOL CALLBACK EnumChildWindowsProc(HWND hwnd, LPARAM lParam)
{
char className[256];
GetClassName(hwnd, className, sizeof(className));
string strClassName(className);
if (strClassName.find("需要查找的窗口类名") != string::npos)
{
*(HWND*)lParam = hwnd;
return FALSE;
}
return TRUE;
}
int main()
{
HWND hwnd = NULL;
// 遍历所有窗口,查找标题包含“需要查找的窗口标题”的窗口
EnumWindows(EnumWindowsProc, (LPARAM)&hwnd);
if (hwnd != NULL)
{
cout << "找到窗口句柄:" << hwnd << endl;
}
else
{
cout << "未找到窗口" << endl;
}
HWND hwndChild = NULL;
// 遍历窗口hwnd的所有子窗口,查找类名为“需要查找的窗口类名”的窗口
EnumChildWindows(hwnd, EnumChildWindowsProc, (LPARAM)&hwndChild);
if (hwndChild != NULL)
{
cout << "找到子窗口句柄:" << hwndChild << endl;
}
else
{
cout << "未找到子窗口" << endl;
}
return 0;
}
```