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.");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…