Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:


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 ?
  • 3
  • 2
  • 2
1 Solution
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.

Meir RivkinFull stack Software EngineerCommented:
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;

win32Author Commented:
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 ?

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

   "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.
Meir RivkinFull stack Software EngineerCommented:
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()
win32Author Commented:
sedqwick >> Where should i call the Invalidate function ?, and if I want can i catch it ?
Meir RivkinFull stack Software EngineerCommented:
in OnMouseLeave(), if u wanna invalidate the window that u just left.

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now