Display a rectangle in the win32 child window if the cursor is on it, and erase if the cursor leaves the child window

I need to display some shapes (more precisely, 5 rectangles) in my small child window when my cursor is in the window, and erase them when the cursor leaves the window; ie enters the area of ​​the parent window.

I track the mouse movement in the child window through NCHITTEST, and the rectangles pop up perfectly. But I can’t make them disappear when my cursor leaves the child window, they just stay there in the client area until WM_PAINT is called into the window.

Can someone tell me how to achieve this functionality? I need to use the NCHITTEST case, as the rest of my functionality depends on it. I tried to track the mouse_move and lbuttondown events, but these events are not recorded with nchittest.

+3
source share
1 answer

Look at the function TrackMouseEvent().

This needs to be called when the mouse enters the window ( WM_MOUSEMOVEif it is not already tracked) and will notify your window when the mouse leaves ( WM_MOUSELEAVE).

Here's some sample VB6 code, but it should be easily converted to any other language.

Select Case Msg
    Case WM_MOUSEMOVE
      If Not MouseInWindow Then
        Dim ET As TRACKMOUSEEVENTTYPE
        'Set up the mouse leave notification
        ET.cbSize = Len(ET)
        ET.hwndTrack = Me.hWnd
        ET.dwFlags = TME_HOVER Or TME_LEAVE
        ET.dwHoverTime = 0
        TrackMouseEvent ET

        MouseInWindow = True
        'The mouse has just entered
        Redraw
      End If

    Case WM_MOUSELEAVE
      If MouseInWindow Then
        MouseInWindow = False
        'The mouse has just left
        Redraw
      End If
End Select
+1
source

All Articles