由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
Mouse cursor always get the wrong hwnd due to tab order - MFC Application
我试图通过使用鼠标光标获得MFC应用程序中开发的窗口句柄并将其打印出来。
这是我用来获取窗口句柄的代码。
#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;
}
我把鼠标光标放在一个组框中的按钮上,结果总是显示组框的句柄而不是按钮。我发现标签顺序是导致我无法获得按钮的原因
有没有其他方法或其他窗口功能可以用来解决选项卡顺序问题?
任何帮助都将不胜感激。非常感谢!
首先需要调用WindowFromPoint
来获得嵌套最重的窗口句柄,然后需要调用RealChildWindowFromPoint
来获得"真正的"句柄并避免分组框。但它也避免了静态文本,因此您需要使用ChildWindowFromPointEx
和CWP_ALL
标志继续查找子窗口。
实现方式如下:
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
相关文章:
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 在 C# 中更改鼠标光标图标
- 将鼠标悬停在图片框上时,如何显示带有 x-y 坐标的十字准线光标?
- 如何获得鼠标光标连接时按下左键单击和在C++中释放左键单击时?
- 由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
- 如何在C++中模拟鼠标光标的移动
- 如何确定鼠标光标所在的控制台字符
- 如何在QML中设置鼠标光标位置
- 确定鼠标光标所在的主机上的字符
- 用鼠标光标移动圆圈
- MFC GUI 自定义控件:如何绘制光标更新以响应鼠标移动?
- 获取有关 X11 中离鼠标光标最近的显示器的信息
- 防止触摸输入移动的鼠标光标在窗口上
- 如何更改鼠标光标的大小?
- 如何在Qwidget上获取当前的鼠标光标位置
- QT4.8嵌入.使用QCursor :: SetPos()调整鼠标输入会导致一个抽搐的光标
- 当我按Enter或在其区域外单击鼠标时,如何从可编辑的Qcombobox中删除光标
- 在 VC++ MFC 中创建另一个鼠标光标
- 当光标锁定在屏幕中时,如何获得鼠标移动
- 如何将光标(鼠标指针对象)转换为bmp图像的自动序列