Custom MouseLeave event button

I made a custom button with some panels and frames. Using MouseEnter and MouseLeave, I set the corresponding hover images like regular buttons.

The problem is that if I move the mouse too quickly over the control, it sometimes does not raise the MouseLeave event. Thus, the button is "locked" in hover mode.

screenshot problem: http://www.jesconsultancy.nl/images/screens/screen_prblm.png

the button on the right is locked in a freeze state.

How can i solve this?

Thanks.

+4
source share
2 answers

Holy ... This is a mess!
Firstly, UserControl very distorted. I suggest you make your control inherit from Control and draw the image and text yourself.
Secondly, why are you using reflection? Third, why are there so many controls?

This skips the event because it takes too much to update!

Here is an example code for managing examples that will NEVER miss an event:

 using System; using System.Drawing; using System.Windows.Forms; namespace lol { public class BlackWhiteControl : Control { protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); this.BackColor = Color.Black; } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); this.BackColor = Color.White; } } } 
+1
source

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.

0
source

Source: https://habr.com/ru/post/1333243/


All Articles