How to detect mouseclick anywhere

  • Thread starter Thread starter Kubik
  • Start date Start date
K

Kubik

Hello!

How to detect mouseclick anywhere on the form? There is a lot of
controls on the form, so I don't want to add eventhandler for each of them.
How can I do it in another way?

Adam
 
Hello!

How to detect mouseclick anywhere on the form? There is a lot of
controls on the form, so I don't want to add eventhandler for each of them.
How can I do it in another way?

See the IMessageFilter interface.
 
Hi Adam,

The only way I can think of is to create message filter and screen off the
mouse messages.

1. Add IMessageFilter interface to the list of interfaces implemented by the
form
public class MyForm: Form, IMessageFilter
{
.....
}

2. In form constructor add the following line

Application.AddMessageFilter(this);

3. Implement the IMessageFilter's only member in the form's class
Something like

public bool PreFilterMessage(ref Message m)
{
Control ctrl = Control.FromHandle(m.HWnd);

if(ctrl != null && (ctrl == this || this.Contains(ctrl))))
{
switch(m.Msg)
{
case 0x0201: //0x0201 is the constant value for
WM_LBUTTONDOWN. For more constants see winuser.h
///Do something when the left-mouse button is down
break
case .....
break
}
}
return false;
}


HTH
B\rgds
100
 
Back
Top