Using TrackMouseEvent to find out when the mouse leaves your window | CodeGuru

Using TrackMouseEvent to find out when the mouse leaves your window

Using TrackMouseEvent is pretty simple. When the mouse enters the window you want to track, you call track mouse event telling it to inform you when the mouse leaves. When it does, it will send a WM_MOUSELEAVE message to that window. If you add this code to any of your existing view classes (or any […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Using TrackMouseEvent is pretty simple. When the mouse enters the
window you want to track, you call track mouse event telling it to
inform you when the mouse leaves. When it does, it will send a
WM_MOUSELEAVE message to that window.

If you add this code to any of your existing view classes (or any CWnd
derived class), you will see a drag rectangle that follows the mouse
cursor around. If you move the mouse outside the window, it will
disappear. If you move the mouse back into the window, it will
reappear.

********************************************************************************



// TrackView.h changes
//

class CTrackView : public CView
{
// Add data members
protected:
	CRect m_rectLast;	//** Added line
	BOOL m_bMouseTracking;	//** Added line

// Add message handler prototype
	afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);	//** Added line

};

****************************************************************************************

// TrackView.cpp changes
//

// Add message handler
BEGIN_MESSAGE_MAP(CTrackView, CView)
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)	//** Added line
END_MESSAGE_MAP()

CTrackView::CTrackView()
{
	m_bMouseTracking = FALSE;	//** Added line
}

/////////////////////////////////////////////////////////////////////////////
// CTrackView message handlers

//** Added function
LRESULT CTrackView::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
	// Draw last rect, but no new one (erase old rect)
	CClientDC dc(this);
	dc.DrawDragRect(CRect(0,0,0,0), CSize(0,0), m_rectLast, CSize(2,2));
	m_rectLast = CRect(0,0,0,0);

	m_bMouseTracking = FALSE;
	return TRUE;
}

void CTrackView::OnMouseMove(UINT nFlags, CPoint point)
{
	// Calc new rectangle
	CRect rectNew(point.x-20, point.y-20, point.x+20, point.y+20);
	CClientDC dc(this);

	// WM_MOUSEMOVE + !m_bMouseTracking becomes the equivalent of
	// WM_MOUSEENTER of which there is no such thing.
	if (!m_bMouseTracking)
	{
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(TRACKMOUSEEVENT);
		tme.dwFlags = TME_LEAVE;
		tme.hwndTrack = this->m_hWnd;

		if (::_TrackMouseEvent(&tme))
		{
			m_bMouseTracking = TRUE;

			// Draw new rect, but no last rect as we are starting anew
			dc.DrawDragRect(rectNew, CSize(2,2), NULL, CSize(0,0));
		}
	}
	else
	{
		// Draw new rect and erase old rect
		dc.DrawDragRect(rectNew, CSize(2,2), m_rectLast, CSize(2,2));
	}

	// Remember where we drew this rectangle
	m_rectLast = rectNew;

	CView::OnMouseMove(nFlags, point);
}

****************************************************************************************


Here’s a helper class I use to make it a little simpler:

class CTrackMouseEvent : public tagTRACKMOUSEEVENT
{
public:
	CTrackMouseEvent(CWnd* pWnd, DWORD dwFlags = TME_LEAVE, DWORD dwHoverTime = HOVER_DEFAULT)
	{
		ASSERT_VALID(pWnd);
		ASSERT(::IsWindow(pWnd->m_hWnd));

		this->cbSize = sizeof(TRACKMOUSEEVENT);
		this->dwFlags = dwFlags;
		this->hwndTrack = pWnd->m_hWnd;
		this->dwHoverTime = dwHoverTime;
	}

	BOOL Track()
		{ return _TrackMouseEvent(this); }
};


You can start tracking like this:

if (!m_bMouseTracking)
	m_bMouseTracking = CTrackMouseEvent(this).Track();



CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.