Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling the Window Under the Mouse

If you take a look at Visual Studio 2012, you'll notice that if you use the mouse wheel, the window under your mouse will scroll, and not the focused window. That is, if you have your cursor in the code editor, and move your mouse over the Solution Explorer window and scroll, the Solution Explorer will scroll, and not the code editor. The WM_MOUSEWHEEL message, though, only gets sent to the focused window, so in this case, the code editor. How can we implement our program such that the WM_MOUSEWHEEL messages scroll the window under the mouse, which is intuitive, and not the focused window?

like image 272
GILGAMESH Avatar asked Dec 19 '22 21:12

GILGAMESH


1 Answers

Apparently we can address this issue at the heart of the program. Look at your code for the message loop, which should be in your WinMain method:

while (GetMessage (&msg, NULL, 0, 0) > 0)
{
    TranslateMessage (&msg);
    DispatchMessage (&msg);
}

Here, we just need to say that if the message is a WM_MOUSEWHEEL message, that we want to pass it to the window under the mouse, and not the focus window:

POINT mouse;

while (GetMessage (&msg, NULL, 0, 0) > 0)
{
    //Any other message.
    if (msg.message != WM_MOUSEWHEEL)
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    //Send the message to the window over which the mouse is hovering.
    else
    {
        GetCursorPos (&mouse);
        msg.hwnd = WindowFromPoint (mouse);
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
}

And now, the window under your mouse will always get scroll messages, and not the focused window.

like image 125
GILGAMESH Avatar answered Jan 03 '23 09:01

GILGAMESH