简体   繁体   English

如何处理Microsoft Visual C ++(MFC)中特定控件的鼠标悬停事件?

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

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. 使用MFC类向导,我在该项目的事件列表中找不到鼠标悬停条目。 I tried using PreTranslateMessage , but it doesn't work. 我尝试使用PreTranslateMessage ,但是它不起作用。 How can I handle that event? 我该如何处理该事件?

Mouse hover events aren't generated by default. 默认情况下不会生成鼠标悬停事件。 You have to request them by calling TrackMouseEvent with a properly populated TRACKMOUSEEVENT structure : 您必须通过使用适当填充的TRACKMOUSEEVENT结构调用TrackMouseEvent来请求它们:

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. 如果鼠标悬停在myButtonmyHoverTime毫秒,则系统将生成WM_MOUSEHOVER消息。

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. 由于WM_MOUSEHOVER消息已发布到请求鼠标悬停消息的窗口,因此您将必须派生自定义按钮控件,并在其消息映射中添加适当的条目。 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). 特别是,您将必须使用ON_WM_MOUSEHOVER()宏并实现afx_msg void OnMouseHover(UINT,CPoint) (请参阅WM_消息处理程序:L-M以供参考)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM