MouseHover/MouseLeave event on the whole entire window

billpg picture billpg · Jan 6, 2010 · Viewed 18.2k times · Source

I have Form subclass with handlers for MouseHover and MouseLeave. When the pointer is on the background of the window, the events work fine, but when the pointer moves onto a control inside the window, it causes a MouseLeave event.

Is there anyway to have an event covering the whole window.

(.NET 2.0, Visual Studio 2005, Windows XP.)

Answer

Ryan picture Ryan · Aug 30, 2012

Ovveride the MouseLeave event to not trigger so long as the mouse enters a child control

    protected override void OnMouseLeave(EventArgs e)
    {
        if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
            return;
        else
        {
            base.OnMouseLeave(e);
        }
    }