How to capture mouse wheel on panel?

旧街凉风 提交于 2020-01-09 03:44:45

问题


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

EDIT:

I try to do it on PictureBox now.

My code:

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?


回答1:


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.

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.


UPDATE: note that this behavior has changed in Windows 10. The "Scroll inactive windows when I hover over them" option is turned 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.




回答2:


If you can't see the "MouseWheel" event on a component, then you need to create it manually. Also, we need to focus that component, otherwise the "MouseWheel" event will not work for that component. I will show you how to create a "MouseWheel" event for "pictureBox1" and how it works.

  1. INSIDE THE CONSTRUCTOR, create a mousewheel event on that component.

    InitializeComponent();
    this.pictureBox1.MouseWheel += pictureBox1_MouseWheel;
    
  2. CREATE THE FUNCTION manually. According to my example, call it "pictureBox1_MouseWheel"

    private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        //you can do anything here
    }
    
  3. CREATE a MouseHover event on that component (Go to properties in PicureBox1, select event, locate "MouseHover" and double-click the "MouseHover" event).

  4. CALL "Focus()"; method inside that MouseHover event.

    pictureBox1.Focus();
    
  5. Now run the program.




回答3:


To wire it up manually...

this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);

private void panel1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
    ///process mouse event
}

Easier method is in visual studio click on panel, goto properties viewpanel, select events, locate and double click the "mousewheel" event.




回答4:


In Winforms, this is achieved using the Control.MouseWheel event




回答5:


Getting mousewheel events is tricky. The easiest way is using

this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);

instead of

this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);

This way the form gets the event instead of control. This way is easy but has one problem: you can use only one mousewheel event in your form.

If you have more than one control to get mousewheel event the best way is This answer by "Shehan Silva - weltZ"



来源:https://stackoverflow.com/questions/4429901/how-to-capture-mouse-wheel-on-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!