鼠标滚轮事件 (C#)
我无法在主窗体中获取鼠标滚轮事件。
作为演示,我想出了一个简单的例子:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
Form2 f2 = new Form2();
f2.Show(this);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
Console.Out.WriteLine(e.Delta);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form2_MouseMove);
this.MouseWheel += new MouseEventHandler(Form2_MouseMove);
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
Console.Out.WriteLine(e.Delta);
}
}
我在 Form2 中获得了鼠标滚轮事件,但在 Form1 中没有获得任何想法?
干杯,
詹姆斯
I can't get the Mouse Wheel event in the main form.
As a demo I came up with a simple example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
Form2 f2 = new Form2();
f2.Show(this);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
Console.Out.WriteLine(e.Delta);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form2_MouseMove);
this.MouseWheel += new MouseEventHandler(Form2_MouseMove);
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
Console.Out.WriteLine(e.Delta);
}
}
I get the mouse wheel event in Form2 but not Form1 any ideas?
Cheers,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我怀疑OP想要在鼠标悬停在面板上时获取滚动事件,即使面板没有焦点。
这里解释了实现此行为的方法:
http ://social.msdn.microsoft.com/forums/en-US/winforms/thread/eb922ed2-1036-41ca-bd15-49daed7b637c/
和此处:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6bfb9287 -986d-4c60-bbcc-23486e239384/
从链接论坛获取的代码片段之一:
此代码基本上将拦截所有 wm_mousewheel 事件并将它们重定向到鼠标当前悬停的控件。 面板不再需要获得焦点来接收滚轮事件。
I suspect the OP wants to get scroll events when just the mouse is hovering over the panel even though the panel does not have the focus.
A way to accomplish this behaviour is explained here:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/eb922ed2-1036-41ca-bd15-49daed7b637c/
and here:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6bfb9287-986d-4c60-bbcc-23486e239384/
One of the code snippets taken from the linked forum:
This code will basically intercept all wm_mousewheel events and redirect them to the control the mouse is currently hovering over. The panel doesn't need to have the focus anymore to receive the wheel events.
您的问题源于 form1 具有焦点,而不是 panel1。 ...这当然意味着将触发 form1 的事件,而不是 panel1 的事件。
我通过对 Form1 中的构造函数进行以下更改来重新创建您的场景,并验证它是否会触发滚轮事件。
Your problem arises from the fact that form1 has focus, not panel1. ...which ofcourse means that it is form1's events that will be fired, not panel1's events.
I recreated your scenario with the following changes to the constructor in Form1 and verified that it fires the scroll wheel event.
添加面板
MouseEnter
的另一个事件,并在其回调函数中获取输入焦点:Add another event of panel
MouseEnter
and in its Callback function get the input focus:感谢 @nitrogenycs 的回答,我编写了一个简单的泛型类来轻松解决这个问题:
从那里,您只需要从此类继承您的 Form,而不是为您需要“管理”MouseWheel 的每个表单继承 Form :
希望这对其他人(而不是我)有帮助。
Thanks to @nitrogenycs 's answer, I've wrote a simple generic class to easily address the issue:
From there, you only need to inherit your Form from this class instead of Form for every form you need the MouseWheel to be "managed":
Hope this will help someone else (than me).
面板本身无法获得焦点,只有放置在面板内的项目才能获得焦点。 只有当某个东西被放置在面板中并且该东西具有焦点时,面板才会接收 MouseWheel 事件。 只需将鼠标悬停在面板上并移动鼠标滚轮即可将事件发送到表单,而不是面板。
这就是你的两个例子之间的区别。
The Panel can't have the focus itself, only an item placed inside the panel can have the focus. The panel will only receive the MouseWheel event once something is placed inside it and that thing has the focus. Simply hoevering over the panel and moving the mouse wheel will send the event to the form, not to the panel.
This is the difference between your two examples.
也许这对你有用?
Maybe this will work for you?