1

Here is a short summary of the problem:

I move the mouse pointer quickly from control 1 to control 2.

In IDE, the sequence is this:

Control 1: Enter
Control 1: Leave
Control 2: Enter
Control 2: Leave

When compiled, the sequence is this (sometimes, but not always):

Control 1: Enter
Control 2: Enter
Control 1: Leave
Control 2: Leave

Here is the full description of the problem:

I'm subclassing a usercontrol.

I process WM_MOUSEMOVE and WM_MOUSELEAVE.

When WM_MOUSEMOVE occurs, and the mouse pointer hasn't been on the control before, I detect it as "Enter".

Then I track the mouse event using the following code:

'Track the mouse leaving the indicated window
Private Sub TrackMouseLeave(ByVal lng_hWnd As Long)
      
    Dim tme As TRACKMOUSEEVENT_STRUCT
    With tme
        .cbSize = Len(tme)
        .dwFlags = TME_LEAVE
        .hwndTrack = lng_hWnd
    End With

    Call TrackMouseEvent(tme)

End Sub

When WM_MOUSELEAVE occurs, I know that the mouse pointer has left the control.

It works perfectly: fine.

When I move the mouse pointer inside the control, the "Enter" event occurs. When I move the mouse outside the control, the "Leave" enter occurs.

Now I try the same again with very rapid mouse movements where I move the mouse pointer quickly into and out of control. It works perfectly fine, no matter how fast I do it. The Enter-Leave-Enter-Leave sequence is perfectly fine: Enter-Leave-Enter-Leave, etc.

Now I duplicate the control so that I have 2 of them next to each other:

I quickly move the mouse pointer from control 1 to control 2.

The sequence is still perfectly fine: Enter-Leave-Enter-Leave.

Now I compile the project and run the exe:

I do the same: I quickly move the mouse pointer from control 1 to control 2.

Now the following sequence occurs:

Control 1: Enter
Control 2: Enter
Control 1: Leave
Control 2: Leave

I thought that the sequence was fixed and not as "variable" as could be observed here.

What have I not thought of? What baffles me is that it works as expected in IDE, but not predictable when compiled.

To debug what is happening, I have tried Spy++, but it only reports WM_MOUSEMOVE, so I have no idea what else I could try now to find out if the message pumps receives it in the "correct" order, and for some reason, it just gets messed up in my application.

Thank you for any help.

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 1
    I was able to duplicate the issue. I don't know why this happens, but I did find a way where it behaved correctly. Instead of subclassing the UserControl itself, subclass the parent form. When I did this it always worked as expected. – Brian M Stafford Apr 30 '21 at 14:26
  • @BrianMStafford Thank you. I'm glad you could reproduce it! However, I don't want to subclass the parent, I really need to subclass the control. My controls are buttons, and I have like 40 of them on my form. – tmighty Apr 30 '21 at 16:42
  • I understand the concern since you would need to hook up 40 x 2 messages. One option would be to do this inside a loop. It wouldn't matter if you had 2 or 200 controls, the size of the code would remain static (and small). I also realize there could be other considerations but at least with this approach you get the messages in the order you expect. – Brian M Stafford Apr 30 '21 at 17:36
  • Thank you for the creative idea. :-) But I think the behaviour is so weird. In IDE, it works... – tmighty Apr 30 '21 at 18:01

0 Answers0