已鎖定 Mousewheel scroll Event on a Label

  • Tuesday, February 28, 2012 2:42 PM
     
     

    I have a regular WinForm Label, It is possible to determine when the Mousewheel is scrolling up or down. But just when the cursor is exactly above the label.

    Thanks in Advance

All Replies

  • Tuesday, February 28, 2012 2:54 PM
     
     
    The MouseEventArgs class has a Location property which should tell you where the cursor is when the event occurs.

    Check out My Blog. Now updated to actually work!

  • Wednesday, February 29, 2012 8:31 AM
     
     
    but hot to determine MouseScrollUp or MouseScrollDown?
  • Wednesday, February 29, 2012 4:07 PM
     
     Answered Has Code

    I did following and it worked for me perfectly

    1. I put label on form and created MouseHover and MouseLeave events and stored in variable if mouse over the label or not
              bool IsMouseOver = false;
              private void label1_MouseHover(object sender, EventArgs e)
              {
                  IsMouseOver = true;
              }
      
              private void label1_MouseLeave(object sender, EventArgs e)
              {
                  IsMouseOver = false;
              }
    2. after I've created MouseWheel event for form, but not from properties window, from designer.cs
      this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseWheel);
    3. Finally I put logic into Form1_MouseWheel Method

       private void Form1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
              {            
                  if (IsMouseOver)
                  {
                      //do your logic here
                  }
              }

    To put everything together:

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = "This is label1 content";
            }
    
            
            private void Form1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
            {            
                if (IsMouseOver)
                {
                    //do your logic here
                }
            }
    
            bool IsMouseOver = false;
            private void label1_MouseHover(object sender, EventArgs e)
            {
                IsMouseOver = true;
            }
    
            private void label1_MouseLeave(object sender, EventArgs e)
            {
                IsMouseOver = false;
            }
    
            
    
                    
        }



    Please mark as read if helped.
    Also visit my blog http://msguy.net/

  • Wednesday, February 29, 2012 4:30 PM
     
     Answered

    Oh sorry, I misunderstood what you were asking.

    For that, you should use the MouseEventArgs.Delta property. As it says in the documentation, Delta will be positive if the mouse was scrolled up (away from the user). If the mouse was scrolled down (toward the user) it will be a negative value instead.


    Check out My Blog. Now updated to actually work!

  • Friday, March 02, 2012 10:27 PM
     
     Answered
    Michael, I may be misunderstanding, but I believe what you are proposing is overkill. If you capture the MouseWheel event on the label, it will only ever be triggered when the mouse is over the label. You don't really need to worry about handling MouseEnter and MouseLeave - those are only necessary if you are capturing the MouseWheel event at the Form level. Just subscribe to the MouseWheel event of the Label instead, and your event will be triggered correctly.

    Check out My Blog. Now updated to actually work!