鼠标滚轮在winforms中冒泡? [英] Mousewheel bubbling up in winforms?

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

问题描述

我对 winforms 和 mousewheel 事件有点问题.我有一个代表滑块的自定义用户控件.现在,我有几组滑块,其中每组都包裹在一个面板内.然后所有组都包装在另一个面板中(将 AutoScroll 设置为 true),并将其包装在一个表单中.滑块逻辑的实现使得鼠标滚轮可用于更改其值.为此,当鼠标悬停在滑块上时,滑块用户控件会获得焦点.但是,当我滚动时,AutoScroll 父面板也会随之滚动.我已经在这个问题上浪费了很多时间.任何人都知道这里发生了什么以及我如何解决它?我认为该事件正在冒泡到父面板,但在 Slider 控件中处理它时,我没有在该事件上找到 Handled 属性(WPF 可能如此).

I have a little problem with winforms and mousewheel events. I have a custom user control representing a slider. Now, I have a couple groups of sliders in which each group is wrapped inside a panel. All the groups are then wrapped in another panel (which has AutoScroll set to true) and this is wrapped in a form. The slider logic is implemented such that the mousewheel can be used to change its value. For this, the slider user control gets focus when the mouse is over the slider. However, when I scroll, also the AutoScroll parent panel scrolls with it. I've already lost a lot of time on this issue. Anybody knows what is happening here and how I can solve it? I thought the event was bubbling to the parent panel but I don't find a Handled property on the event when handling it in the Slider control (as is possible with WPF).

非常感谢

推荐答案

我们将 Slider 实现为具有自己外观的完整自定义用户控件(继承 UserControl 类).

We implemented the Slider as a complete custom user control (inheriting the UserControl class) with own look-and-feel.

您可能已经注意到 UserControl 没有在属性"窗口中显示 MouseWheel 事件.有麻烦的迹象.WM_MOUSEWHEEL 消息冒泡.如果具有焦点的控件没有处理它,则 Windows 会将其传递给其父级.重复,直到找到想要处理它的父窗口.您的案例中的小组.

You might have noticed that a UserControl doesn't show the MouseWheel event in the Properties window. Hint of trouble there. The WM_MOUSEWHEEL message bubbles. If the control that has the focus doesn't handle it then Windows passes it on to its Parent. Repeatedly, until it finds a parent window that wants to handle it. The Panel in your case.

您需要在滑块控件中调用一些黑魔法.传递给 MouseWheel 事件的实际事件 args 对象不是事件签名所暗示的 MouseEventArgs 类型,而是 HandledMouseEventArgs.这可以让你停止冒泡.像这样:

You'll need to invoke a bit of black magic in your slider control. The actual event args object that get passed to the MouseWheel event is not of the MouseEventArgs type as the event signature suggests, it is HandledMouseEventArgs. Which lets you stop the bubbling. Like this:

    protected override void OnMouseWheel(MouseEventArgs e) {
        base.OnMouseWheel(e);
        // do the slider scrolling
        //..
        ((HandledMouseEventArgs)e).Handled = true;
    }

这篇关于鼠标滚轮在winforms中冒泡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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