Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I detect if the mouse moves out of a control's client area?

112 views
Skip to first unread message

Kelvin Lim

unread,
Sep 8, 1998, 3:00:00 AM9/8/98
to
Hi,

I'm trying to write a dialog box control derived from CButton that
responds whenever the mouse cursor moves into and out of its client
area.

OnMouseMove() takes care of the first case pretty nicely; however, I'm
having some difficulty figuring out how to call a member function when
the cursor is moved out of the control's client area.

What are some (preferably simple!) ways for me to achieve this?

Thanks! :)

----------------------------------------------------------------------
#include <stdio.h>
int main(){char*j="\xA\x2F%c\0\0005ofczgei*e~*xo~~oh*mdcb~sdk*o|kb*\
\177es*~-deN",*i;for(i=j+*(j+1);i-j-4;)printf(j+2,*i--^*j);return 0;}
----------------------------------------------------------------------

Tomas Restrepo

unread,
Sep 8, 1998, 3:00:00 AM9/8/98
to
A simple way is to use a timer, even though it's not very elegant.

On your OnMouseMove() create a timer (say 100ms), and then, on your
OnTimer(), check if the mouse is still in your client area (by using
GetCursorPos()). If it isn't, kill the timer.

Remember to check if the timer is already set when creating it in
OnMouseMove(), so that you don't try to create multiple timers.

--
Tomas Restrepo
win...@bigfoot.com
http://www.geocities.com/SiliconValley/Heights/3401

Kelvin Lim <kelv...@pacific.net.sg> wrote in message
35f55112...@news.pacific.net.sg...

Pete Shinners

unread,
Sep 8, 1998, 3:00:00 AM9/8/98
to

Kelvin Lim wrote in message <35f55112...@news.pacific.net.sg>...

>I'm trying to write a dialog box control derived from CButton that
>responds whenever the mouse cursor moves into and out of its client
>area.
>
>OnMouseMove() takes care of the first case pretty nicely; however, I'm
>having some difficulty figuring out how to call a member function when
>the cursor is moved out of the control's client area.
>
>What are some (preferably simple!) ways for me to achieve this?


i caught this somewhere on the newgroup, and have been using it constantly
since.
what you need to do is setup a mouse tracker. the tracker will send you a
message
when the mouse leaves the current window. set it up something like this...


class ExampleWindow: public CWnd
{
ExampleWindow() {m_bTracking = FALSE;}
...
BOOL m_bTracking;
afx_msg void OnMouseLeave(WPARAM, LPARAM);
afx_msg void OnMouseMove(UINT, CPoint)
...
};

BEGIN_MESSAGE_MAP(ExampleWindow, CWnd)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_WM_MOUSEMOVE()
...
END_MESSAGE_MAP()

void ExampleWindow::OnMouseLeave(WPARAM, LPARAM)
{
AfxMessageBox("HEY! the mouse has left us.");
m_bTracking = FALSE;
}

void ExampleWindow::OnMouseMove(UINT nFlags, CPoint pt)
{
if(!m_bTracking)
{
TRACKMOUSEVENT track;
track.cbsize = sizeof(TRACKMOUSEEVENT);
track.dwFlags = TME_LEAVE;
track.hwndTrack = GetSafeHwnd();
if(::_TrackMouseEvent(&track))
m_bTracking = TRUE;
}
// ... rest of mouse handler...
}

hope that helps... there's really not much work to do. the ClassWizard does
not support the WM_MOUSELEAVE, so you'll have to wire it in by hand. no
big problem though. then all you have to do is insert the if(!m_bTracking)
block into the very start of your OnMouseMove handler.


Dirk Djuga

unread,
Sep 8, 1998, 3:00:00 AM9/8/98
to
Kelvin Lim wrote in message <35f55112...@news.pacific.net.sg>...
>Hi,

>
>I'm trying to write a dialog box control derived from CButton that
>responds whenever the mouse cursor moves into and out of its client
>area.
>
>OnMouseMove() takes care of the first case pretty nicely; however, I'm
>having some difficulty figuring out how to call a member function when
>the cursor is moved out of the control's client area.
>
>What are some (preferably simple!) ways for me to achieve this?


You might SetCapture() when the mouse is inside the control, and then in
OnMouseMove() check whether it still is (w. GetClientRect &
CPoint::PtInRect()), if it isn't ReleaseCapture().

cU
--
Dirk Djuga mailto:di...@solaris.stuttgart.netsurf.de
http://djuga.home.ml.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Retriever 2.0 is out: http://retriever.home.ml.org

Shareware image viewer, thumbnailer & database
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Mika Kurki

unread,
Sep 9, 1998, 3:00:00 AM9/9/98
to
In article <#Z0ydw12...@uppssnewspub05.moswest.msn.net>, "Tomas Restrepo"
<win...@bigfoot.com> wrote:
You can also try SetCapture. This directs all MouseMove event to your window
until ReleaseCapture is called. So in First MouseMove do SetCapture. Then in
MouseMove check if mouse is still above your window, if not call
ReleaseCapture.

>A simple way is to use a timer, even though it's not very elegant.
>
>On your OnMouseMove() create a timer (say 100ms), and then, on your
>OnTimer(), check if the mouse is still in your client area (by using
>GetCursorPos()). If it isn't, kill the timer.
>
>Remember to check if the timer is already set when creating it in
>OnMouseMove(), so that you don't try to create multiple timers.
>

-------------------------------------------------------------
Mika Kurki - System Analyst, ICL Financial Services

Mail: zzzmik...@icl.fi
to respond remove zzz from start of the address

Kelvin Lim

unread,
Sep 9, 1998, 3:00:00 AM9/9/98
to
zzzmik...@icl.fi (Mika Kurki) wrote:

>In article <#Z0ydw12...@uppssnewspub05.moswest.msn.net>, "Tomas Restrepo"
><win...@bigfoot.com> wrote:
>You can also try SetCapture. This directs all MouseMove event to your window
>until ReleaseCapture is called. So in First MouseMove do SetCapture. Then in
>MouseMove check if mouse is still above your window, if not call
>ReleaseCapture.
>
>>A simple way is to use a timer, even though it's not very elegant.
>>
>>On your OnMouseMove() create a timer (say 100ms), and then, on your
>>OnTimer(), check if the mouse is still in your client area (by using
>>GetCursorPos()). If it isn't, kill the timer.
>>
>>Remember to check if the timer is already set when creating it in
>>OnMouseMove(), so that you don't try to create multiple timers.

Thanks for the suggestions. I'll give them a try.

J.T. Anderson

unread,
Sep 9, 1998, 3:00:00 AM9/9/98
to
The cleanest way to do this is to use TrackMouseEvent. Unfortunately, it
wasn't available in Windows 95, so if you use it directly, your program will
require Windows 98 or NT 4.0.

COMCTL32.DLL contains a function named _TrackMouseEvent which works just
like TrackMouseEvent. I don't know if this function was in the first
releases of COMCTL32, but it is certainly in the 4.7x releases. It will
call TrackMouseEvent on systems that support it and emulate it on those that
don't. You may need an updated COMMCTRL.H and/or COMCTL32.LIB to use this
function. These files are in the Platform SDK. If you don't have an MSDN
professional subscription, I think you can download the SDK from the MSDN
web site.

A redistributable update to COMCTL32 version 4.72 can be found at:

http://msdn.microsoft.com/developer/downloads/files/40Comupd.htm

Kelvin Lim wrote in message <35f55112...@news.pacific.net.sg>...
>Hi,
>
>I'm trying to write a dialog box control derived from CButton that
>responds whenever the mouse cursor moves into and out of its client
>area.
>
>OnMouseMove() takes care of the first case pretty nicely; however, I'm
>having some difficulty figuring out how to call a member function when
>the cursor is moved out of the control's client area.
>
>What are some (preferably simple!) ways for me to achieve this?
>

>Thanks! :)

0 new messages