How can i easily catch the "mouse down" events of all the controls in a form, without manually subscribing to each and every event? (C#) Something like the "KeyPreview" feature, but for the mouse events.
This event occurs when the mouse pointer moves while it is over a control. The handler for this event receives an argument of type MouseEventArgs. This event occurs when the mouse pointer is over the control and the user releases a mouse button. The handler for this event receives an argument of type MouseEventArgs.
VB.Net Mouse Events MouseDown – occurs when a mouse button is pressed. MouseEnter – occurs when the mouse pointer enters the control. MouseLeave – occurs when the mouse pointer leaves the control. MouseMove – occurs when the mouse pointer moves over the control.
The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse). Common events using this interface include click , dblclick , mouseup , mousedown .
I found this to be the best solution for my purposes.
Create a new class derived from IMessageFilter
:
public class GlobalMouseHandler : IMessageFilter
{
private const int WM_LBUTTONDOWN = 0x201;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
{
// do something
((YourMainForm)Form.ActiveForm).YourMainForm_Click(null, null);
}
return false;
}
}
Then in your main form add this to register the message filter:
GlobalMouseHandler globalClick = new GlobalMouseHandler();
Application.AddMessageFilter(globalClick);
And add this function to do whatever you have to, in your form:
public void YourMainForm_Click(object sender, EventArgs e)
{
// do anything here...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With