ON_WM_MOUSEHOVER消息要生效,要配合ON_WM_MOUSEMOVE一起使用.代码如下:
void CYuButton::OnMouseMove(UINT nFlags, CPoint point)
{// TODO: 在此添加消息处理程序代码和/或调用默认值TRACKMOUSEEVENT tme = { 0 };tme.cbSize = sizeof(TRACKMOUSEEVENT);tme.dwFlags = TME_HOVER | TME_LEAVE;tme.dwHoverTime = 50;tme.hwndTrack = this->m_hWnd;if (TrackMouseEvent(&tme)) {}CWnd::OnMouseMove(nFlags, point);
}
void CYuButton::OnMouseHover(UINT nFlags, CPoint point)
{// TODO: 在此添加消息处理程序代码和/或调用默认值m_bIsDown = TRUE;Invalidate();CWnd::OnMouseHover(nFlags, point);
}
以上是鼠标放在控件上时的消息响应.鼠标离开控件直接处理ON_WM_MOUSELEAVE消息,如:
void CYuButton::OnMouseLeave()
{// TODO: 在此添加消息处理程序代码和/或调用默认值m_bIsDown = FALSE;Invalidate();CWnd::OnMouseLeave();
}
也可同时处理失去焦点消息ON_WM_KILLFOCUS,如下:
void CYuButton::OnKillFocus(CWnd* pNewWnd)
{CWnd::OnKillFocus(pNewWnd);m_bIsDown = TRUE;Invalidate(TRUE);
}
转载于:https://blog.51cto.com/9233403/2093243