3

Hi i have three controls (CButtton) in my application,whenever mouse move over a control,i want to capture when mouse enters on which control in a window and when it leaves and i have to change the caption of a button control.

Thanks in Advance

shiv
  • 31
  • 1
  • 2

2 Answers2

4

There is no windows message/event that indicates 'mouse enter' or 'mouse leave'. However this can be achieved by handling the 'MouseMove' message for your control and capturing the mouse input to check if the point is inside the control area. Release the capture if the point is out of the control area.

for sample code check here.

Hemant
  • 726
  • 6
  • 17
  • 1
    You can also take a look at the TrackMouseEvent API function: http://msdn.microsoft.com/en-us/library/ms646265(VS.85).aspx – humbagumba Jul 20 '10 at 08:38
2

@Hemant:

You are wrong. There are messages for mouse leave and mouse hover, defined in WinUser.h

#if((_WIN32_WINNT >= 0x0400) || (WINVER >= 0x0500))
#define WM_MOUSEHOVER                   0x02A1
#define WM_MOUSELEAVE                   0x02A3
#endif
#if(WINVER >= 0x0500)
#define WM_NCMOUSEHOVER                 0x02A0
#define WM_NCMOUSELEAVE                 0x02A2
#endif /* WINVER >= 0x0500 */

Documentation:

WM_MOUSEHOVER message

WM_MOUSELEAVE message

And you can handle it with a message-mapping like:

ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
sergiol
  • 4,122
  • 4
  • 47
  • 81