.NET: How to check if the mouse is in a control?

后端 未结 3 1716
梦毁少年i
梦毁少年i 2020-12-18 14:34

i want to know if the mouse is in a particular control in .NET

private void panel1_MouseLeave(object sender, EventArgs e)
{
   if (MouseIsInControl((Control)         


        
相关标签:
3条回答
  • 2020-12-18 14:52

    I also use a System.Windows.Forms.Timer for this solution, but I don't use the enter/leave mouse events anymore. They have caused me too much grief. Instead I use the MouseMove event on the component I need to know the mouse is over. I have 2 member variables named in this class.

    bool Hovering = false;
    System.Drawing.Point LastKnownMousePoint = new System.Drawing.Point();
    

    In my case, I wanted to toggle a border around a label. I deal with knowing if the mouse is over the label control I care about as follows:

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            // Mouse Position relative to the form... not the control
            LastKnownMousePoint = Cursor.Position; 
    
            label1.BorderStyle = BorderStyle.Fixed3D;
            timer1.Stop();
            timer1.Interval = 50; // Very fast (Overkill?  :) )
            timer1.Start();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            System.Drawing.Point point = Cursor.Position;
            if (LastKnownMousePoint.Equals(point))
            {
                // If the mouse is somewhere on the label, I'll call that Hovering.  
                // Though not technically the MS definition, it works for me.
                Hovering = true;
            }
            else if (Hovering == false)
            {
                label1.BorderStyle = BorderStyle.None;
                timer1.Stop();
            }
            else
            {
                Hovering = false;
                // Next time the timer ticks, I'll stop the timer and
                // Toggle the border.
            }
        }
    

    This works because I only update the LastKnownMousePoint when the mouse is over the control (a label in this case). So, if the mouse moves outside of the control, I won't update LastKnownMousePoint and I'll know it's time to toggle the border style.

    0 讨论(0)
  • 2020-12-18 14:57

    This Hans Passant Answer can be adapted to do what you want:

    private bool mEntered;
    private Timer timer1;
    
    public Form1() {
      InitializeComponent();
    
      timer1 = new Timer();
      timer1.Interval = 200;
      timer1.Tick += timer1_Tick;
      timer1.Enabled = false;
    }
    
    private void panel1_MouseEnter(object sender, EventArgs e) {
      timer1.Enabled = true;
    }
    
    private void timer1_Tick(object sender, EventArgs e) {
      bool entered = panel1.ClientRectangle.Contains(panel1.PointToClient(Cursor.Position));
      if (entered != mEntered) {
        mEntered = entered;
        if (!entered) {
          timer1.Enabled = false;
          // OK, Do something, the mouse left the parent container
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-18 15:06

    No hooks or subclassing needed.

    private bool MouseIsOverControl(Button btn) => 
        btn.ClientRectangle.Contains(btn.PointToClient(Cursor.Position))
    

    This method also works if the mouse is outside of the form containing the control. It uses a button object but you can use any UI class

    You can test the method easily like so:

    private void button1_Click(object sender, EventArgs e)
    {
        // Sleep to allow you time to move the mouse off the button
        System.Threading.Thread.Sleep(900); 
    
        // Try moving mouse around or keeping it over the button for different results
        if (MouseIsOverControl(button1))
             MessageBox.Show("Mouse is over the button.");
        else MessageBox.Show("Mouse is NOT over the button.");
    }
    
    0 讨论(0)
提交回复
热议问题