简体   繁体   中英

WM_MOUSEMOVE GET_X_LPARAM AND GET_Y_LPARAM Catch wrong Coordinates

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION)
    {
        if (wParam == WM_MOUSEMOVE)
        {
            cout << "X : " << GET_X_LPARAM(lParam) << " Y: " << GET_Y_LPARAM(lParam) << "\n";
        }
    }
    return CallNextHookEx(hMSHook, nCode, wParam, lParam);
}

    int _tmain() {
        HMODULE hInstance = GetModuleHandle(NULL);
        hMSHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, NULL);

        MSG Msg;

        while (GetMessage(&Msg, NULL, 0, 0)) { DispatchMessage(&Msg); }
        ::ReleaseDC(0, dc);

        return 0;
    }

result = Always return wrong coordinates,

example = X = -1844, Y = 79,

X = -1556 Y = 271,

X = -1028 Y = 91

...

Value is changing when every launch

somebody can help me?

The lParam parameter of MouseProc is not identical to the lParam parameter for WM_MOUSEMOVE . It is a MOUSEHOOKSTRUCT * .

So, change MouseProc to :

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION)
    {
        auto &ms = * (const MOUSEHOOKSTRUCT *) lParam;
        if (wParam == WM_MOUSEMOVE)
        {
            cout << "X : " << ms.pt.x << " Y: " << ms.pt.y << "\n";
        }
    }
    return CallNextHookEx(hMSHook, nCode, wParam, lParam);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM