You should automatically receive a Windows WMMOMSELEAVE message when the mouse leaves the client area, and processing the base class of this message calls the OnMouseLeave method. If this does not really happen, you can get around this. Just intercept WM_MOUSEMOVE directly, and then create a Win32 API call that asks you to be notified when the mouse leaves your control.
Use the following simple redefinition of WndProc ...
private bool _mouseOver = false; protected override void WndProc(ref Message m) { switch (m.Msg) { case PI.WM_MOUSEMOVE: if (!_mouseOver) { PI.TRACKMOUSEEVENTS tme = new PI.TRACKMOUSEEVENTS(); tme.cbSize = (uint)Marshal.SizeOf(typeof(PI.TRACKMOUSEEVENTS)); tme.dwHoverTime = 100; tme.dwFlags = (int)(PI.TME_LEAVE); tme.hWnd = Handle; PI.TrackMouseEvent(ref tme); _mouseOver = true; } base.WndProc(ref m); break; case PI.WM_MOUSELEAVE: _mouseOver = false; base.WndProc(ref m); break; } }
And you will get the necessary information about the platform ...
internal const int WM_MOUSEMOVE = 0x0200; internal const int WM_MOUSELEAVE = 0x02A3; internal const int TME_LEAVE = 0x0002; [StructLayout(LayoutKind.Sequential)] internal struct TRACKMOUSEEVENTS { public uint cbSize; public uint dwFlags; public IntPtr hWnd; public uint dwHoverTime; }
Hope this helps.
source share