Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if cursor is within the bounds of a control

Tags:

I have a user control

public partial class UserControl1 : UserControl, IMessageFilter {     public UserControl1()     {         InitializeComponent();         Application.AddMessageFilter(this);     }      public bool PreFilterMessage(ref Message m)     {         var mouseLocation = Cursor.Position;          if (Bounds.Contains(PointToClient(mouseLocation)))         {             bool aBool = true;//breakpoint             bool two = aBool;//just assignment so compiler doesn't optimize my bool out         }         if (m.Msg != 0x20a) // Scrolling Message         {             return false;//ignore message         }         return false;     } } 

When I float over the user control contained in a parent form, the breakpoint is not hit. The breakpoint is hit in close proximity, but I can be in an actual textbox inside the user control and not get a hit. How can I accurately determine if I am within the bounds of this user control?

FWIW, I have two monitors. It does not appear to make a difference which monitor I am using.

like image 857
P.Brian.Mackey Avatar asked Sep 20 '12 20:09

P.Brian.Mackey


1 Answers

Try your hit testing against Control.ClientRectangle rather than Control.Bounds:

if (ClientRectangle.Contains(PointToClient(Control.MousePosition))) {     bool aBool = true;//breakpoint      bool two = aBool; } 
like image 109
Jay Riggs Avatar answered Oct 03 '22 22:10

Jay Riggs