Solved

OnMouseLeave

Posted on 2001-07-19
7
253 Views
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 ?
0
Comment
Question by:win32
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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.

Rob
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 100 total points
Comment Utility
u can use TrackMouseEvent for that:

add this class to the app file:
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); }
};

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

in the message map add this line:
     ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)


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

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


g'day
0
 

Author Comment

by:win32
Comment Utility
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 ?

CB.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:robpitt
Comment Utility
RE:
   "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.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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()
0
 

Author Comment

by:win32
Comment Utility
sedqwick >> Where should i call the Invalidate function ?, and if I want can i catch it ?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
in OnMouseLeave(), if u wanna invalidate the window that u just left.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now