Showing posts with label MFC. Show all posts
Showing posts with label MFC. 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;
}

Monday, February 8, 2010

MFC Doxygen Compatibility Note

A conflict between the two will cause this code to be misread and mark all your public variables as private. This is really bad because most doxygen outputs hide privates, causing all your functions to vanish, and if they do display, they will be marked as private which is just wrong.

class foo : public CWnd
{
DECLARE_DYNCREATE(foo)

public:
...
};

So, it is very important that you add an semicolon [;] after the dyncreate macro:

class foo : public CWnd
{
DECLARE_DYNCREATE(foo); //Semicolon added here

public:
...
};