Showing posts with label Mouse. Show all posts
Showing posts with label Mouse. Show all posts

Tuesday, February 9, 2010

Mouse Leave Event in MFC

There is several parts to tracking a mouse leaving, first you need to track on mouse move and a new message, WM_MOUSELEAVE

BEGIN_MESSAGE_MAP(CDerived, CWnd)
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

Within the mouse move, you need to setup a new condition to check if we have setup our mouse leave flag:

void
CDerived
::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_isInClient) // first time we enter, we need to setup the callback
{
TRACKMOUSEEVENT tme = { sizeof(tme) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = GetSafeHwnd();
TrackMouseEvent(&tme);
m_isInClient = 1;
}
else if (point == CPoint(-1,-1))
// We are leaving
{
m_isInClient = 0;
}
}

This is the custom message to loop the on mouse leave back to on mouse move:
afx_msg LRESULT OnMouseLeave(WPARAM,LPARAM)
{
OnMouseMove(0,CPoint(-1,-1));
return 0;
}