Winforms : Intercepting Mouse Event on Main Form first, not on Controls

Subscribe to all controls MouseMove events (consider do it recursively for nested controls)

foreach (Control control in Controls)
    control.MouseMove += RedirectMouseMove;

And raise MouseMove inside this event handler

private void RedirectMouseMove(object sender, MouseEventArgs e)
{
    Control control = (Control)sender;
    Point screenPoint = control.PointToScreen(new Point(e.X, e.Y));
    Point formPoint = PointToClient(screenPoint);
    MouseEventArgs args = new MouseEventArgs(e.Button, e.Clicks, 
        formPoint.X, formPoint.Y, e.Delta);
    OnMouseMove(args);
}

Keep in mind that controls receive MouseEvents with local coordinates of control. So you need to convert it to form coordinates.
There are could be drawbacks with nested controls, but I leave it to you (e.g. call Parent.PointToClient)

UPDATE: You are still will be able to handle events of control – just subscribe to event one more time.

Leave a Comment