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

I want to know if the mouse is in a specific control in .NET.

private void panel1_MouseLeave(object sender, EventArgs e) { if (MouseIsInControl((Control)sender) return; //the mouse didn't leave, don't fire a MouseLeave event ... } public Boolean MouseIsInControl(Control control) { //return (control.Bounds.Contains(MousePosition)); return control.Bounds.Contains(control.PointToClient(MousePosition)) } 

But I need someone to play with four different coordinate systems in order to make it work .

Related Questions

+2
source share
3 answers

No hooks or subclasses are required.

 private bool MouseIsOverControl(Button btn) { if (btn.ClientRectangle.Contains(btn.PointToClient(Cursor.Position))) { return true; } return false; } 

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

You can easily test the method as follows:

 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."); } } 
+2
source

This Hans Passant Answer can be adapted to accomplish 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 } } } 
+4
source

I also use System.Windows.Forms.Timer for this solution, but I no longer use mouse I / O events. They caused me too much grief. Instead, I use the MouseMove event for a component that needs to know that the mouse has ended. I have 2 members of variables named in this class.

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

In my case, I wanted to switch the border around the label. I am dealing with the understanding that the mouse is over the label control, which I care about the following:

  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 update LastKnownMousePoint when the mouse is over the control (label in this case). So, if the mouse moves outside the control, I will not update LastKnownMousePoint, and I will find out what time it is to switch the border style.

0
source

Source: https://habr.com/ru/post/1237793/


All Articles