问题
In my application I need to handle mouse hover event to change the background of a button. Using the MFC class wizard, I couldn't find a mouse hover entry in the list of events for that item. I tried using PreTranslateMessage, but it doesn't work. How can I handle that event?
回答1:
Mouse hover events aren't generated by default. You have to request them by calling TrackMouseEvent with a properly populated TRACKMOUSEEVENT structure:
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof( tme );
tme.dwFlags = TME_HOVER;
tme.hwndTrack = myButton;
tme.dwHoverTime = myHoverTime; // HOVER_DEFAULT, or the hover timeout in milliseconds.
::TrackMouseEvent( &tme );
The system will then generate WM_MOUSEHOVER messages if the mouse hovers over myButton for myHoverTime milliseconds.
Since the WM_MOUSEHOVER message is posted to the window that requested mouse hover messages, you will have to derive a custom button control, with appropriate entries in its message map. In particular, you will have to use the ON_WM_MOUSEHOVER() macro and implement afx_msg void OnMouseHover(UINT, CPoint) (see WM_ Message Handlers: L - M for reference).
来源:https://stackoverflow.com/questions/33189986/how-to-handle-mouse-hover-event-for-specific-control-in-microsoft-visual-c-m