News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Display mouse coordinates

Started by Ksbunker, February 02, 2006, 07:21:29 AM

Previous topic - Next topic

Ksbunker

I'm not sure what the problem is, any help would be appreciated.

When the mouse moves (WM_MOUSEMOVE), I get the coords (GetCursorPos), convert the coord to printable (wsprintf) number and display coord (SetDlgItemText). But it just doesn't seem to work! IDC_STATIC_X_POS, is just a label.

.data
pt POINT <>
Format db "%d",0
OutputStr db 256 dup(?)

.elseif uMsg == WM_MOUSEMOVE
Invoke GetCursorPos, ADDR pt
Invoke wsprintf, addr OutputStr, addr Format, pt.x
Invoke SetDlgItemText, hWnd, IDC_STATIC_X_POS, addr OutputStr

Edit1*: I ok tested again, it does work, but only displays coordinates when the mouse is within the border of the program (also, doesn't display coords when it's over controls) and not the coordinates of the entire screen, which is what is desired! Any advice.. cheers

Edit2*: I got it working, if someone else has this problem the solution is below.

Use WM_TIMER, and put the code in there and not WM_MOUSEMOVE, be sure to have settimer and killtimer as well ;-)

Ksbunker

How do I make it so that only left mouse button clicks coordinates are recorded when those clicks occur within a particluar window. So only left mouse button clicks are recorded in that window. Everywhere other than that window gets ignored. Any thoughts?

MichaelW

I can't really see any good reason to monitor mouse clicks in this way within a window you control, because you could do it more efficiently by the normal methods. And I'm having trouble understanding how monitoring your own mouse clicks in a window that you do not control could be useful.



eschew obfuscation

Ksbunker

It is for a tool im making to aid persons playing a minigolf game.

Effectively, I want only coords from this minigolf window to be recorded.

MickD

If you want to get the mouse coordinates while in a command say and you go out of the client area you have to 'capture' the mouse else the mouse messages will go to other windows outside of your app. I don't have any reference for you at the moment but it may be a start. look up capturing the mouse in your win32 reference or by web search.
hth
Mick.

ramguru

It (SetCapture, hWnd) is usually for text selection in edit control in order to track when left mouse button was released if mouse cursor went outisde the edit window or its parent window.

P1

Lookup the MouseHook but check out the CBT option.

Regards,  P1  :8)

Ksbunker

This is a bit of a bizarre problem. Some people I know asked me to make a program for there favourite game, anyway the game is a flash minigolf. They wanted me to make a program that records mouse coordinates, and then saves the left mouse click coordinate from a particular window to a list (the window being the minigolf game).

Well, my problem is that not all mouse clicks are recorded, basically only a quarter to a third get processed and the rest go un-noticed. I'm comparing the class retrieved from GetClassName to ID_CLASS, which is the class of the minigolf program. Is there anyway to make it work always, and not just everynow and then. My code is as follows;

.elseif uMsg == WM_TIMER

Invoke GetCursorPos, ADDR pt
invoke wsprintf, addr OutputStrX, addr FormatX, pt.x
invoke wsprintf, addr OutputStrY, addr FormatY, pt.y
Invoke lstrcat, addr OutputStrX, ADDR OutputStrY

Invoke WindowFromPoint, pt.x, pt.y
Invoke GetClassName, eax, addr szClass, sizeof szClass
Invoke lstrcmp, addr ID_CLASS, addr szClass

.if eax==0
Invoke GetAsyncKeyState, VK_LBUTTON
.if eax==1
invoke SendMessage,hList,LB_ADDSTRING,0, ADDR OutputStrX
Invoke SendMessage, hList, WM_VSCROLL, SB_BOTTOM, 0
.endif
.endif

Invoke SetDlgItemText, hWnd, 1002, ADDR OutputStrX

QvasiModo

The problem is you're not really recording mouse movement, but periodically polling for it's position. Even if WM_TIMER was accurate (it isn't), you're not catching mouse clicks as they happen, you're simply polling real fast hoping that the mouse button won't get clicked while you weren't looking. :wink

The correct way to do it would be to use a mouse hook, just like P1 suggested. Note though that your program will have to be run with Administrator priviledges.

On windows hooks:
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks.asp

The functions to set and remove the hooks:
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/unhookwindowshookex.asp

A hook to record mouse movement:
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/lowlevelmouseproc.asp

But you'll want to check these out too:
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/journalrecordproc.asp
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/journalplaybackproc.asp

Hope that helps! :U