如何在面板上捕获鼠标滚轮? [英] How to capture mouse wheel on panel?

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

问题描述

如何在 C# 中捕获面板上的鼠标滚轮?我正在使用 WinForms

How to capture mouse wheel on panel in C#? I'm using WinForms

我现在尝试在 PictureBox 上做.

I try to do it on PictureBox now.

我的代码:

this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);    
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)  
  {  
    MessageBox.Show("Click");  
  }

点击有效.惠灵没有.为什么?

Clicking works. Wheelling doesn't. Why?

推荐答案

Windows 将 WM_MOUSEWHEEL 消息发送到具有焦点的控件.那不会是Panel,不是可以获取焦点的控件.只要你在面板上放置一个控件,说一个按钮,然后按钮就会获得焦点和消息.

Windows sends the WM_MOUSEWHEEL message to the control that has the focus. That won't be Panel, it is not a control that can get the focus. As soon as you put a control on the panel, say a button, then the button gets the focus and the message.

但是按钮对消息没有用,它没有什么可滚动的.Windows 注意到这一点并将消息发送给父级.那是面板,现在它会滚动.

The button however has no use for the message, it's got nothing to scroll. Windows notices this and sends the message to the parent. That's the panel, now it will scroll.

您会在这个答案.

You'll find code for a custom panel that can get the focus in this answer.

更新:请注意,此行为在 Windows 10 中发生了变化.默认情况下,新的将鼠标悬停在非活动窗口上时滚动"选项处于打开状态.这使得鼠标滚轮的行为与其在浏览器或 Office 程序中的工作方式更加一致.在这种特定情况下,图片框现在获取事件.请注意这一点.

UPDATE: note that this behavior has changed in Windows 10. The new "Scroll inactive windows when I hover over them" option is turned on by default. The makes the mouse wheel behavior more consistent with the way it works in a browser or, say, an Office program. In this specific case the picturebox now will get the event. Watch out for this.

这篇关于如何在面板上捕获鼠标滚轮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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