Posted on 2001-07-19
Last Modified: 2013-12-03
Hi I wan't to make a function that is called when a mouse leaves a window, how is that done. I tryed to look at WM_MOUSELEAVE, how can I do it otherwise ?
Question by:win32
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 6297472
TrackMouseEvent & WM_MOUSELEAVE works fine but only on Win98 or later.

Alternatives are to:
a) use SetCapture()
   NOTE this has side effects for keyboard accelerators, see the remarks for SetCapture in the documentation.
b) periodically poll the cursor position with GetcursorPos()
c) use SetWindowsHookEx(WH_MOUSE) in a dll and intercept all mouse movements and implement your own versions of TrackMouseEvent & WM_MOUSELEAVE.

Which method you choose depends on your circumstances.
Ask if you need more info.

LVL 42

Accepted Solution

sedgwick earned 100 total points
ID: 6297498
u can use TrackMouseEvent for that:

add this class to the app file:
class CTrackMouseEvent : public tagTRACKMOUSEEVENT
     CTrackMouseEvent(CWnd* pWnd, DWORD dwFlags = TME_LEAVE, DWORD dwHoverTime = HOVER_DEFAULT)

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

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

void CMyView::OnMouseMove(UINT nFlags, CPoint point)
             TRACKMOUSEEVENT tme;
          tme.cbSize = sizeof(TRACKMOUSEEVENT);
          tme.dwFlags = TME_LEAVE;
          tme.hwndTrack = m_hWnd;
CView::OnMouseMove(nFlags, point);

in the message map add this line:

LRESULT CMyView::OnMouseLeave(WPARAM wParam, LPARAM lParam)

//do your code here....
    return TRUE;


Author Comment

ID: 6297680
Ok then, thanks..

1. I hold the mousecurser over my window
2. I popup another application ex. MS Paint, over my window. it cuvers my window, and my window can't be seen at all
3. I move the Mouse Curser away from the window, place it in the DownLeft corner of Win2000
4. I press ALT F4 to close paint

Now some Graph erros in my program occures because I use MouseMove to paint some graphics. Anyway so when the mouse leaves from my window the Graphics shall be ReDrawn()

Now that's why I need the MOUSELEAVE, but is this message send, when I move the mouse away in the way I just discribed ?

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Expert Comment

ID: 6297727
   "Now some Graph erros in my program occures"
...could you elaborate on that?

Anyway the best solution really depends upon exactly what you are doing in your mousemove code.
LVL 42

Expert Comment

ID: 6297933
if u wanna redraw your window after leaving the window use Invalidate();

or if u save the last position of the mouse u can use InvalidateRect()

Author Comment

ID: 6298417
sedqwick >> Where should i call the Invalidate function ?, and if I want can i catch it ?
LVL 42

Expert Comment

ID: 6298512
in OnMouseLeave(), if u wanna invalidate the window that u just left.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query performance - SQL Server 20 85
mixing C++ & C# in Vis Studio 2013 7 280
Is COM supported from Apache 1 81
Dialogbox API leak? 18 118
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question