[comp.windows.ms.programmer] Tracking the mouse...

jls@hsv3.UUCP (James Seidman) (10/30/90)

I'm trying to create a child window which can tell when the cursor goes in
or out of it.  The state of the window needs to be different depending on
whether or not the cursor is currently inside it.

Now, figuring out when the mouse is inside it is easy, by capturing
WM_MOUSEMOVE.  However, figuring out when the mouse leaves the window
is harder.  I can't just use WM_MOUSEMOVE from the parent window,
because this won't work if the cursor moves to another child window.
Processing WM_SETCURSOR instead will solve that problem, but I still
have a problem if the user moves from my child window to outside my
parent window quickly enough that no WM_SETCURSOR messages are produced.
(The same kind of problem also occurs when the child window is partially
obscured by a different app's window, and the user just moves the mouse
from my child window to the other app without going through any of my
parent window.)

Now, what I really would like (and won't get, of course) is a WM message
which would specify when the cursor has left my window.  Barring that,
I've come up with some inelegant solutions:

1) Capture mouse input whenever my child window gets a WM_MOUSEMOVE.
   Release it when the mouse leaves the window.  I hate this because
   then the user can't use Cntl-Esc, Alt-Tab, etc. to switch apps.

2) Set a mouse clipping rectangle around the child window to ensure that
   the mouse will go through a piece of the parent window before leaving
   the app.  This solution doesn't address what happens when the app
   doesn't have the focus, and doesn't work when the child window is
   partially obscured.

There must be a better way.  Has anyone grappled with this before?
Thanks in advance for any help you might give me.

-- 
Jim Seidman (Drax), the accidental engineer.
"It doesn't have to work... they'll be paralyzed just from laughing at me."
							- Dr. Who, _Shada_
UUCP: ames!vsi1!hsv3!jls	         INTERNET: hsv3.UUCP!jls@apple.com

risto@tuura.UUCP (Risto Lankinen) (10/31/90)

jls@hsv3.UUCP (James Seidman) writes:


>I'm trying to create a child window which can tell when the cursor goes in
>or out of it.  The state of the window needs to be different depending on
>whether or not the cursor is currently inside it.

>Now, figuring out when the mouse is inside it is easy, by capturing
>WM_MOUSEMOVE.  However, figuring out when the mouse leaves the window
>is harder. 

Hi!

You could try polling the mouse with GetCursorPos() within the application's
message loop, and check whether the cursor is inside the desired child window
by calling WindowFromPoint() .  If it were, you could act directly, or, more
elegantly, send that window a self-defined WM_USER message with wParam set to
indicate cursor entering / exiting status when the hWnd returned by Window-
FromPoint() has changed.

This could also be made as a DLL were there a need to cooperate among many
instances of the same application.  The DLL would contain a 'windowless' 
process to poll the mouse position and two functions which the application
uses to register a window being checked + the corresponding event handler,
and to un-register the service before the application terminates.

Terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              2   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 -1 is PRIME!  Now working on 2 +1 *
replies: risto@yj.data.nokia.fi     ***************************************

jls@hsv3.UUCP (James Seidman) (11/01/90)

risto@tuura.UUCP (Risto Lankinen) writes:
>You could try polling the mouse with GetCursorPos() within the application's
>message loop, and check whether the cursor is inside the desired child window
>by calling WindowFromPoint() .  If it were, you could act directly, or, more
>elegantly, send that window a self-defined WM_USER message with wParam set to
>indicate cursor entering / exiting status when the hWnd returned by Window-
>FromPoint() has changed.

I'm already doing something of the sort in the parent on WM_MOUSEMOVE,
WM_SETCURSOR, and WM_KILLFOCUS.  But none of these help when the user
moves the mouse outside of the parent window fast enough that no messages
get generated in the parent area outside of the child window.

Now, I could use a timer to poll periodically, but this seems like a
terribly inelegant solution.  I was hoping that there would be something
a little nicer to solve the problem (but I'm rapidly losing hope...)

brianf@umd5.umd.edu (Brian Farmer) (11/02/90)

In article <5538@hsv3.UUCP> jls@hsv3.UUCP (James Seidman) writes:
>
>I'm trying to create a child window which can tell when the cursor goes in
>or out of it.  The state of the window needs to be different depending on
>whether or not the cursor is currently inside it.
>

Have you thought of setting a system hook?  WH_GETMESSAGE looks like the 
best bet for what you are trying to do.  This would let you monitor mouse 
movements on a system wide basis.


Brian Farmer

davidds@microsoft.UUCP (David D'SOUZA) (11/04/90)

In article <823@tuura.UUCP> risto@tuura.UUCP (Risto Lankinen) writes:
>jls@hsv3.UUCP (James Seidman) writes:
>
>
>>I'm trying to create a child window which can tell when the cursor goes in
>>or out of it.  The state of the window needs to be different depending on
>>whether or not the cursor is currently inside it.
>
>>Now, figuring out when the mouse is inside it is easy, by capturing
>>WM_MOUSEMOVE.  However, figuring out when the mouse leaves the window
>>is harder. 

Seems to me that whenever your child window gets the WM_MOUSEMOVE
message you could capture the mouse (using SetCapture(hwndchild)). 
This would cause all mouse messages to go to your window regardless of
where the mouse is.  Now, when the mouse moves outside your window
rectangle, you simply release the capture.

--Dave

jseidman@jarthur.Claremont.EDU (James Seidman) (11/05/90)

davidds@microsoft.UUCP (David D'SOUZA) writes:
>In article <823@tuura.UUCP> risto@tuura.UUCP (Risto Lankinen) writes:
>Seems to me that whenever your child window gets the WM_MOUSEMOVE
>message you could capture the mouse (using SetCapture(hwndchild)). 
>This would cause all mouse messages to go to your window regardless of
>where the mouse is.  Now, when the mouse moves outside your window
>rectangle, you simply release the capture.

I thought of this too, and actually tried it.  The problem is that then,
whenever the cursor is inside my child window, the user can't switch away
from it using the normal magic keys (alt-enter, control-escape, etc.).
While this is acceptable for the usual uses of SetCapture (which usually
involve the user making a selection, having a mouse button down, or the
like), it's really unacceptable here.

-- 
Jim Seidman, Headland Technology, 46221 Landing Parkway, Fremont CA 94538
The Doctor: It was a terrible babble of inhuman voices.
Prof. Chronotis: Oh, that was just the undergraduates!
							- Doctor Who, "Shada"