[英]Qt with WinAPI MouseProc
我需要在屏幕上跟蹤 cursor position,為此我使用來自 ZA05B798442A282EBA07 的 function
// 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,錯誤是它,點擊后我刪除了應用程序,如果移動鼠標,這個function將被調用,其中我指的是window的孩子
您為此使用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 window,這個是可行的方案。 但是如果是另一個 window 你不能用這個!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.