Search code examples
c++eventsmousewheelmouse-position

Why GET_X_LPARAM does return an absolute position on mouse wheel?


I override the WndProc method of my Window where I handle Windows messages.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

In order to get the position of the mouse when I catch the left button down event (WM_LBUTTONDOWN) I use the following code:

auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);

And this works well. I am able to get the relative position of the mouse on my window. But I have a problem when I catch the mouse wheel event (WM_MOUSEWHEEL). In this case it seems that the above macros return the abousulte position of the mouse within my screen.

How can I get the relative position of the mouse even on the mouse wheel event?


Solution

  • WM_MOUSEWHEEL indeed carries screen-based coordinates in lParam. That's by design.

    To convert into client-based coordinates, you can use ScreenToClient():

    POINT pt;
    pt.x = GET_X_LPARAM(lParam);
    pt.y = GET_Y_LPARAM(lParam);
    ScreenToClient(hwnd, &pt);
    // 'pt' now contains client-based coordinates.