Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

c# - How to capture mouse wheel on panel?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...