Boris
Boris

Reputation: 8951

Capture all mouse events in user control

I'm trying to capture all mouse events in a user control (even the ones that occur in child controls). For that I use the "override WndProc"-approach:

protected override void WndProc(ref Message m)
{
  System.Diagnostics.Debug.WriteLine(m.Msg.ToString());
  base.WndProc(ref m);
}

I'm especially interested in mouse events, but that does not seem to work. I do get mouse button up/down events, but I don't get mouse move and mouse wheel events.

Any ideas?

Upvotes: 2

Views: 3272

Answers (2)

Tergiver
Tergiver

Reputation: 14517

The correct way to capture all mouse events in a control is to call that control's Control.Capture method.

Typically this is a temporary state like doing drag n drop, user-drawing, and so forth.

Upvotes: 0

crypted
crypted

Reputation: 10306

Best you could do is implement IMessageFilter in your control.

 public class CustomMessageFilter:UserControl,IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        //Process your message here
        throw new NotImplementedException();

    }
}

you can write your message filtering logic in PreFilterMessage Method. Then just install it to the list of Message Filter in Main method.

 Application.AddMessageFilter(new CustomMessageFilter());

This is a Application level hook, that means you can control all the Win32 message within application.

Upvotes: 3

Related Questions