Qt with WinAPI MouseProc
Qt with WinAPI MouseProc
我需要跟踪屏幕上的光标位置,为此我使用 WinAPI 中的函数
// code from Qt(!) project
#include <windows.h>
#pragma comment(lib, "user32.lib")
MyClass *myclass;
static HHOOK hHook;
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
switch( wParam )
{
case WM_MOUSEMOVE:
POINT p;
GetCursorPos(&p);
myclass->setState(QPoint(p.x,p.y),myclass->getParent()); // setCursor
break;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
//in the class constructor
hHook = (HHOOK) ::SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)MouseProc, GetModuleHandle(0),0) ;
这有效,但有时程序在退出时崩溃。如果注释掉最后一行,程序永远不会崩溃,但不会跟踪鼠标。我不知道WinApi,所以错误可能很明显
编辑 1:
我知道如何获取坐标,但问题是如果使用 WinAPI 退出后程序崩溃
编辑2: 谢谢Nurav,错误是它,单击后我删除了应用程序,如果移动鼠标,将调用此函数,其中我指的是窗口的子项
您可以使用GetCursorPos()
功能:
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
switch(wParam)
{
case WM_MOUSEMOVE:
POINT p;
GetCursorPos(&p);
sprintf(msgbuf, "My coordinates is (%d,%d)n", p.x,p.y);
OutputDebugString(msgbuf);
break;
}
}
您可以在此处阅读有关它的更多信息。 编辑 1:
如果是自己的win32窗口,这是一个可行的解决方案。但如果是另一个窗口,你就不能使用它!
相关文章:
- WinAPI挂钩鼠标
- 将执行、作业和WinAPI相乘
- 如何在桌面C++上使用 WinApi 画线
- WINAPI 注册应用程序重新启动时不清除打开的套接字
- Winapi:屏幕截图未显示在窗口中
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- Qt with WinAPI MouseProc
- 如何在"SUBSYSTEM:WINDOWS"下显示并输出到Windows(C++ WinApi)上的
- WinAPI 无法创建按钮
- 将 Win32/WinAPI 应用程序移植到 wxWidgets
- WinAPI 检查窗口是否具有常规标题栏
- 如何使用带有矢量的 winapi 读取进程内存从另一个进程读取缓冲区?
- 将Qt集成到现有的VS项目中以取代WinAPI
- __stdcall - WINAPI vs STDMETHODCALLTYPE vs APIENTRY
- 如何将'this'指针传递给C++ WinAPI 线程?
- Winapi-WM_CLOSE的替代方案
- Winapi - SetWindowLongPtr in ShutdownBlockReason创建/销毁JNI本机代码
- WinAPI C++如何从 64 位应用程序加载资源
- winapi 中的哪个功能检查按钮是否正在关闭控制台?
- WinAPI 窗口在使用 PrintWindow 截屏后不再更新