如何用获取当前鼠标所在窗口的句柄及窗口标题用c或c++?

2024-12-03 04:53:44
推荐回答(1个)
回答1:

#include
int main()
{
while (1)
{
POINT pNow = {0,0};
if (GetCursorPos(&pNow)) // 获取鼠标当前位置
{
HWND hwndPointNow = NULL;
hwndPointNow = WindowFromPoint(pNow); // 获取鼠标所在窗口的句柄
if (hwndPointNow)
{
//cout << "Success!!" << endl;
char szWindowTitle[50];
::GetWindowTextA(hwndPointNow, szWindowTitle, sizeof(szWindowTitle)); // 获取窗口标题
cout << hex << (int)hwndPointNow << endl; // 鼠标所在窗口的句柄
cout << szWindowTitle << endl; // 鼠标所在窗口的标题
}
else
cout << "Error!!" << endl;
}
else
cout << "Error!!" << endl;
Sleep(500);
}
return 0;
}