WM_MOUSEWHEEL 消息不会传播到父级 [英] WM_MOUSEWHEEL message does not propagate to parent

查看:29
本文介绍了WM_MOUSEWHEEL 消息不会传播到父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ScrollBars 设置为 None 的 RichTextBox.根据 MSDN

I have a RichTextBox with ScrollBars set to None. According to MSDN

DefWindowProc 函数将消息传播到窗口的父母.不应该有消息的内部转发,因为DefWindowProc 将它向上传播到父链,直到它找到一个处理它的窗口.

The DefWindowProc function propagates the message to the window's parent. There should be no internal forwarding of the message, since DefWindowProc propagates it up the parent chain until it finds a window that processes it.

我将此解释为,我不需要挂入消息泵 (IMessageFilter) 并手动将 WM_MOUSEWHEEL 事件转发到包含富文本框的父表单.当我在 RichTextBox 中并执行鼠标滚动时,表单不会滚动.为什么不?如何让表单滚动?

I interpret this as, I should not need to hook into the message pump (IMessageFilter) and manually forward WM_MOUSEWHEEL events to the parent Form containing the richtextbox. When I am inside of RichTextBox and perform a mouse scroll, the Form does not scroll. Why not? How can I get the Form to scroll?

请记住,RichTextBox 的滚动条设置为 none 并为 Form 启用滚动条.那么为什么表单没有获得滚动事件?

Keep in mind that scrollbars are set to none for RichTextBox and enabled for the Form. So why isn't the form getting the scroll event?

public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
        this.AutoScroll = true;
        richTextBox1.ScrollBars = RichTextBoxScrollBars.None;
    }
}

推荐答案

表单不滚动,因为 RichTextBox 处理 WM_MOUSEWHEEL 本身并且不调用 DefWindowProc 将其转发到其父级.显然,禁用滚动条时不会改变.

The form doesn't scroll because the RichTextBox handles the WM_MOUSEWHEEL itself and doesn't call DefWindowProc to forward it to its parent. Apparently that doesn't change when disabling the scroll bars.

所以你要么需要实现一个 IMessageFilter 或者创建一个 RichTextEdit 的子类,它将 WM_MOUSEWHEEL 转发为 此处显示:

So you either need to implement an IMessageFilter or create a subclass of RichTextEdit that will forward the WM_MOUSEWHEEL as shown here:

public class NoScrollRichTextBox : RichTextBox
{
   const int WM_MOUSEWHEEL = 0x020A;

   protected override void WndProc(ref Message m)
   {
      // This will completely ignore the mouse wheel, which will disable zooming as well
      if (m.Msg != WM_MOUSEWHEEL)
         base.WndProc(ref m);
   }
}

这篇关于WM_MOUSEWHEEL 消息不会传播到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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