c++實現按鈕HOVER效果
1.在類中定義成員變數:
//滑鼠事件跟蹤
TRACKMOUSEEVENT * TME;
BOOL m_IsCanTrack;
2.建構函式重初始化:
m_IsCanTrack = TRUE;
TME = new TRACKMOUSEEVENT;
3.新增訊息函式:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
ON_WM_MOUSEMOVE()
ON_WM_MOUSEHOVER()
ON_WM_MOUSELEAVE()
4.函式實現:
void CVMButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值
CRect rc;
GetClientRect(rc);
if (m_IsCanTrack)
{
TME->cbSize = sizeof(TRACKMOUSEEVENT);
TME->dwFlags = TME_LEAVE | TME_HOVER;
TME->dwHoverTime = 10;
TME->hwndTrack = m_hWnd;
if (::_TrackMouseEvent(TME))
{
m_IsCanTrack = FALSE;
}
}
CButton::OnMouseMove(nFlags, point);
}
void CVMButton::OnMouseHover(UINT nFlags, CPoint point)
{
m_IsCanTrack = FALSE;
ButtonStateMov();
CButton::OnMouseHover(nFlags, point);
}
void CVMButton::OnMouseLeave()
{
// TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值
m_IsCanTrack = TRUE;
ButtonStateSet(m_state);
CButton::OnMouseLeave();
}