How to handle mouse hover event for specific control in Microsoft visual C++ ( MFC )?

谁都会走 提交于 2019-12-08 10:38:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!