winform中鼠标滚轮冒泡?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能已经注意到,UserControl 不在“属性”窗口中显示 MouseWheel 事件。那里有麻烦的迹象。 WM_MOUSEWHEEL 消息冒泡。如果拥有焦点的控件不处理它,则 Windows 会将其传递给其父级。如此反复,直到找到想要处理它的父窗口。您案件中的专家组。
您需要在滑块控件中调用一些黑魔法。传递给 MouseWheel 事件的实际事件参数对象不是事件签名所示的 MouseEventArgs 类型,而是 HandledMouseEventArgs。这可以让你停止冒泡。像这样:
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:
如果您正在动态创建事件,例如
尝试在像这样调用事件函数后取消注册事件
If you are creating event dynamically like
try un-registering the event after the eventfunction is called like this