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

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

问题描述

我有WinForms和鼠标滚轮事件的一个小问题。
我有一个表示滑块自定义用户控件。现在,我有滑块的几个组,每个组中包含了一个面板内。所有组,然后包裹在另一个小组(其中有自动滚屏设置为true),这被包裹在一种形式。滑块逻辑被实现为使得该滚轮可用于改变其值。为此,滑块用户控件获得当鼠标移动到滑块焦点。然而,当我滚动,也自动滚屏父面板滚动它。
我已经失去了很多时间在这个问题上。任何人都知道这里发生了什么,我该如何解决呢?我认为该事件是冒泡到父面板但在滑块控制在搬运时我没有找到关于事件处理的特性(这可能与WPF)。



非常感谢


解决方案

我们实现了滑块作为一个完整的自定义用户控件(继承的用户控件类)用自己的外观和感觉。




您可能已经注意到,一个用​​户控件不显示在属性窗口中的鼠标滚轮事件。麻烦有提示。该WM_MOUSEWHEEL消息气泡。如果具有焦点的控件不处理它那么Windows将其传递到其父。反反复复,直到找到要处理它父窗口。你的情况的面板。



您需要调用位的黑魔法在你的滑块控件。即获得通过向MouseWheel事件的实际事件参数对象不是MouseEventArgs类型的作为事件签名所暗示的,它是HandledMouseEventArgs。它可以让你停止冒泡。像这样的:

 保护覆盖无效OnMouseWheel(MouseEventArgs E){
base.OnMouseWheel(E);
//做滑块滚动
// ..
((HandledMouseEventArgs)E).Handled = TRUE;
}


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).

many thanks

解决方案

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

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.

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天全站免登陆