WM_MOUSELEAVE is not created when left-clicking

In my Win32 application, I do not receive the WM_MOUSELEAVE message when I hold the left mouse button and quickly move the mouse out of the window. But if I, holding the left mouse button, start from the inside of the window and slowly move beyond the edge of the window, it will generate WM_MOUSELEAVE.

If I do not hold the left mouse button, I get WM_MOUSELEAVE messages every time, no matter how quickly the mouse leaves the window.

Who cares? What can I do to handle both cases correctly?

EDIT: If I left a click and hold, go from the window and then release the left mouse button, I will receive the WM_MOUSELEAVE message. But it's too late.

+3
source share
3 answers

WM_MOUSELEAVE is that you can detect that the mouse leaves your window when you do not have a capture. When you have a capture, you are responsible for detecting this (if you want).

so it makes no sense in SetCaptureAND TrackMouseEventat the same time, you would use one or the other.

Now, if it would be more convenient for you to see WM_MOUSELEAVE messages during capture, it is quite simple to do this yourself in your message pump.

You would just add code that looks something like this between calls GetMessage()and DispatchMessage()in your message pump.

  GetMessage(pmsg, ...);

  .....

  if ((IS_WITHIN(pmsg->message, WM_MOUSEFIRST, WM_MOUSELAST) ||
       IS_WITHIN(pmsg->message, WM_NCMOUSEMOVE, WM_NCMBUTTONDBLCLK)) &&
       MyMouseLeaveDetection(pmsg, g_hwndNotifyMouseLeave))
     {
     MSG msg = *pmsg;
     msg.message = WM_MOUSELEAVE;
     msg.hwnd    = g_hwndNotifyMouseLeave; // window that want 
     msg.lParam  = 0xFFFFFFFF;
     g_hwndNotifyMouseLeave = NULL;

     DispatchMessage (&msg);
     }

 .....
 TranslateMessage(pmsg);
 DispatchMessage(pmsg);
+4
source

Windows 7 , . , mouseenter/mouseleave. , TrackMouseEvent, WM_MOUSEMOVE, , . , , , , TrackMouseEvent, , , . , , WM_MOUSELEAVE, , . , WM_MOUSELEAVE .

, , proc WM_LBUTTONDOWN SetCapture . SetCapture - , WM_MOUSELEAVE. , SetCapture, WM_MOUSEMOVE, . , WM_MOUSELEAVE WM_MOUSEMOVE, , , , mousemove, . SetCapture WM_LBUTTONDOWN proc , WM_MOUSELEAVE, ... .

, , , , .

+5

Since waiting for WM_MOUSELEAVE is unreliable, the best solution I have found is to directly view the mouse position during WM_MOUSEMOVE. I compare the position of the mouse with the client area, and if the position is outside, I process this when the mouse leaves.

Also I have to call SetCapture when the mouse position is in the client area and ReleaseCapture when it leaves.

-1
source

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


All Articles