How do I get the mouse position over the Desktop when the mouse is moving out of the program window?
Thanks in advance,
Posted on 2004-01-30 20:13:01 by Rudy Penteado
Im no expert but.. Have you tried invoking GetCursorPos inside of a timer?
Posted on 2004-01-30 20:20:36 by DevSpartacus
Hi Rudy,

Haven't really thougth about it but you might use the _TrackMouseEvent function and wait for a WM_MOUSELEAVE message then use the GetCursorPos function to obtain the current position of the cursor...


mov D[tme.dwFlags],TME_LEAVE
mov eax,[hWin]
mov [tme.hwndTrack],eax
mov D[tme.dwHoverTime],HOVER_DEFAULT
invoke _TrackMouseEvent,ADDR tme

pt POINT <>

; on receving a WM_MOUSELEAVE
invoke GetCursorPos,ADDR pt
Posted on 2004-01-30 20:31:12 by donkey

Thanks for the answer but AFAIK, GetCursorPos does not report the movements of the mouse over another application or the Desktop. It reports the position of the mouse ONLY when the mouse is over your program window. There are 3 exceptions to this rule:
1) When you capture the mouse with any button down and drag it around.
2) When the focus is passed to your program the API will report the place where the cursor was WHEN the change of focus occurred.
3) When your program looses the focus, the API reports the place where the mouse was when the focus was removed.
In some circumstances you can do some tricks with this.
What I need is to be able to know where the cursor is, all the time, without disturbing the other applications.



I will investigate that and let youknow soon.
Thanks also.
Posted on 2004-01-30 20:51:38 by Rudy Penteado
Yea, I wasnt to sure on it I know I have used it when making games though so :/ didnt test it >.<
Posted on 2004-01-30 20:57:18 by DevSpartacus
Hi Rudy,

GetCursorPos will report the mouse position any on the screen regardless of focus. You are probably thinking about the WM_MOUSEMOVE message which is only sent to the Window under the cursor.
Posted on 2004-01-30 21:20:19 by donkey

The description I gave to DevSpartacus is correct for the GetCursorPos API (I was not referring to the WM_MOUSEMOVE messages). As DevSpartacus guessed correctly, I was using it inside the timer routine.
I found that if you fill the POINT <> structure with zeros and call the API when the cursor is out of your screen, the API will report the last position where the cursor was (inside you window), before it left the window. Again, the exceptions are the 3 ones mentioned in the earlier answer.
I tried following the suggestion you gave and found a curious behavior that I can't explain: I do get a WM_MOUSELEAVE message but when the cursor enters my window and not when it leaves.
It's odd, isn't it? Can't explain that.

Many thanks,
Posted on 2004-01-31 06:02:10 by Rudy Penteado
donkey & DevSpartacus,

I fully apologize.
My mistake. The program I wrote to test the numbers reported from GetCursorPos was not refreshing the screen in all cases so, it induced me on taking those conclusions.
GetCursorPos DOES report the position of the mouse all over the screen, all the time, have you the focus or not.
Many thanks and I hope you guys excuse me for the mistake.

Best regards,
Posted on 2004-01-31 08:02:21 by Rudy Penteado
Hi :)
My 0.02 to this topic: global hooks.
A warning though, using hooks tends to degrade performace, so it could be a bit of an overkill. IMHO donkey's solution is the best.
Posted on 2004-01-31 16:10:10 by QvasiModo
Rudy Penteado,
Keep in mind that GetCursorPos only gives you a position only if you ask for it. Whereas WM_MOUSEMOVE continually gives a position whenever the mouse moves. If you want mouse messages when the mouse if moved outside of the window of interest, you need to capture the mouse. If you don't, you can depress one of the mouse keys in the client area and Windows will not know if the key is released, or the position if the cursor is outside the client area. To avoid problems, you should capture the mouse only when the button is depressed in your client area. You should release the capture when the button is released. GetCursorPos gives a position in screen coordinates, WM_MOUSEMOVE gives the same position in client coordinates. Ratch
Posted on 2004-01-31 16:25:21 by Ratch