A Journey 2 Eternity

Using TrackMouseEvent to find out when the Mouse Leaves from the Window

Posted on: December 5, 2007

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.

Sample code:

// HotEdit.h
class CHotEdit : public CEdit
{
protected:
	BOOL m_bMouseTracking;
	afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
};

// HotEdit.cpp
BEGIN_MESSAGE_MAP(CHotEdit, CEdit)
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

CHotEdit::CHotEdit()
{
	m_bMouseTracking = FALSE;
}

LRESULT CHotEdit::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
	m_bMouseTracking = FALSE;
	return TRUE;
}

void CHotEdit::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!m_bMouseTracking) {
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(TRACKMOUSEEVENT);
		tme.dwFlags = TME_LEAVE;
		tme.hwndTrack = this->m_hWnd;

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

	CEdit::OnMouseMove(nFlags, point);
}
Advertisements
Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Categories

December 2007
M T W T F S S
« Nov   Mar »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Blog Stats

  • 27,176 hits
%d bloggers like this: