在鼠标下滚动窗口 [英] Scrolling the Window Under the Mouse

查看:182
本文介绍了在鼠标下滚动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果看一下Visual Studio 2012,您会注意到,如果使用鼠标滚轮,则鼠标下方的窗口将滚动,而不是聚焦的窗口.也就是说,如果将光标放在代码编辑器中,然后将鼠标移到解决方案资源管理器窗口上并滚动,则解决方案资源管理器将滚动,而不是代码编辑器.但是,WM_MOUSEWHEEL消息仅被发送到焦点窗口,因此在这种情况下为代码编辑器.我们如何实现程序,使WM_MOUSEWHEEL消息滚动鼠标下的窗口,这是直观的,而不是焦点窗口?

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?

推荐答案

显然,我们可以在程序的核心解决此问题.查看您的代码以了解消息循环,该循环应在WinMain方法中:

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);
}

在这里,我们只需要说,如果该消息是WM_MOUSEWHEEL消息,那么我们希望将其传递给鼠标下方的窗口,而不是焦点窗口:

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.

这篇关于在鼠标下滚动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆