winform中鼠标滚轮冒泡?

发布于 2024-11-28 14:41:02 字数 329 浏览 0 评论 0原文

我对 winforms 和鼠标滚轮事件有一个小问题。 我有一个代表滑块的自定义用户控件。现在,我有几组滑块,其中每组都包裹在面板内。然后,所有组都被包装在另一个面板中(该面板的 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).

many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-12-05 14:41:02

我们将滑块实现为具有自己的外观和感觉的完整自定义用户控件(继承 UserControl 类)。

您可能已经注意到,UserControl 不在“属性”窗口中显示 MouseWheel 事件。那里有麻烦的迹象。 WM_MOUSEWHEEL 消息冒泡。如果拥有焦点的控件不处理它,则 Windows 会将其传递给其父级。如此反复,直到找到想要处理它的父窗口。您案件中的专家组。

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

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

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;
    }
我ぃ本無心為│何有愛 2024-12-05 14:41:02

如果您正在动态创建事件,例如

object.event += new EventHandler<EventArgs>(eventfunction);

尝试在像这样调用事件函数后取消注册事件

object.event -= new EventHandler<EventArgs>(eventfunction);

If you are creating event dynamically like

object.event += new EventHandler<EventArgs>(eventfunction);

try un-registering the event after the eventfunction is called like this

object.event -= new EventHandler<EventArgs>(eventfunction);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文