由于 Tab 键顺序,鼠标光标总是得到错误的 hwnd - MFC 应用程序
Mouse cursor always get the wrong hwnd due to tab order - MFC Application
我试图通过使用鼠标光标获取在 MFC 应用程序中开发的 window 句柄并将其打印出来。
这是我用来获取 window 句柄的代码。
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
POINT pt;
Sleep(5000);
GetCursorPos(&pt);
SetCursorPos(pt.x,pt.y);
Sleep(100);
HWND hPointWnd = WindowFromPoint(pt);
SendMessage(hPointWnd, WM_LBUTTONDOWN, MK_LBUTTON,MAKELONG(pt.x,pt.y));
SendMessage(hPointWnd, WM_LBUTTONUP, 0, MAKELONG(pt.x,pt.y));
char class_name[100];
char title[100];
GetClassNameA(hPointWnd,class_name, sizeof(class_name));
GetWindowTextA(hPointWnd,title,sizeof(title));
cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
cout <<"hwnd : " <<hPointWnd<<endl<<endl;
system("PAUSE");
return 0;
}
我将鼠标光标放在组框内的按钮上,结果总是显示组框的句柄而不是按钮。我发现 Tab 键顺序是导致我无法获取按钮句柄的原因
是否有任何其他方法或其他 windows 函数可用于解决 Tab 键顺序问题?
任何帮助将不胜感激。非常感谢!
首先你需要调用WindowFromPoint
获取嵌套最重的window句柄,然后你需要调用RealChildWindowFromPoint
获取"real"句柄并避免组框。但它也避免了静态文本,因此您需要使用 ChildWindowFromPointEx
和 CWP_ALL
标志继续寻找子 windows。
实现方式如下:
POINT pt;
GetCursorPos(&pt);
// Get the window from point
HWND hWnd = WindowFromPoint(pt);
// map cursor position to window's client coordinates
MapWindowPoints(NULL, hWnd, &pt, 1);
while (true)
{
// Now let's look for real child window
HWND hWndChild = RealChildWindowFromPoint(hWnd, pt);
if (hWndChild == hWnd)
{
// There's no "real" child but we still need to look
// for Disabled/Transparent/Invisible windows
hWndChild = ChildWindowFromPointEx(hWnd, pt, CWP_ALL);
}
if (hWndChild == NULL || hWndChild == hWnd)
break; // we haven't found any child, stop search
// Continue search within child window
MapWindowPoints(hWnd, hWndChild, &pt, 1);
hWnd = hWndChild;
}
// At this point hWnd variable should contain the handle that you're looking for
我试图通过使用鼠标光标获取在 MFC 应用程序中开发的 window 句柄并将其打印出来。
这是我用来获取 window 句柄的代码。
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
POINT pt;
Sleep(5000);
GetCursorPos(&pt);
SetCursorPos(pt.x,pt.y);
Sleep(100);
HWND hPointWnd = WindowFromPoint(pt);
SendMessage(hPointWnd, WM_LBUTTONDOWN, MK_LBUTTON,MAKELONG(pt.x,pt.y));
SendMessage(hPointWnd, WM_LBUTTONUP, 0, MAKELONG(pt.x,pt.y));
char class_name[100];
char title[100];
GetClassNameA(hPointWnd,class_name, sizeof(class_name));
GetWindowTextA(hPointWnd,title,sizeof(title));
cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
cout <<"hwnd : " <<hPointWnd<<endl<<endl;
system("PAUSE");
return 0;
}
我将鼠标光标放在组框内的按钮上,结果总是显示组框的句柄而不是按钮。我发现 Tab 键顺序是导致我无法获取按钮句柄的原因
是否有任何其他方法或其他 windows 函数可用于解决 Tab 键顺序问题?
任何帮助将不胜感激。非常感谢!
首先你需要调用WindowFromPoint
获取嵌套最重的window句柄,然后你需要调用RealChildWindowFromPoint
获取"real"句柄并避免组框。但它也避免了静态文本,因此您需要使用 ChildWindowFromPointEx
和 CWP_ALL
标志继续寻找子 windows。
实现方式如下:
POINT pt;
GetCursorPos(&pt);
// Get the window from point
HWND hWnd = WindowFromPoint(pt);
// map cursor position to window's client coordinates
MapWindowPoints(NULL, hWnd, &pt, 1);
while (true)
{
// Now let's look for real child window
HWND hWndChild = RealChildWindowFromPoint(hWnd, pt);
if (hWndChild == hWnd)
{
// There's no "real" child but we still need to look
// for Disabled/Transparent/Invisible windows
hWndChild = ChildWindowFromPointEx(hWnd, pt, CWP_ALL);
}
if (hWndChild == NULL || hWndChild == hWnd)
break; // we haven't found any child, stop search
// Continue search within child window
MapWindowPoints(hWnd, hWndChild, &pt, 1);
hWnd = hWndChild;
}
// At this point hWnd variable should contain the handle that you're looking for