Getting Cursor Postion? (when clicked)

Hey guys,

I'm new here, so I apologize if I'm not doing this correctly. I need some help in a game I'm making where I need to be able to see where on screen that someone clicked. I understand that the GetCursorPos function would help, but I'm still not really sure how to implement this in my code...at all. Do you think someone could help with a very basic, sample code that would be able to give the cursor position when clicked?

Thanks!

-Pat
What framework or library are you using for your game? Win API?
Yeah, I'm pretty sure
"Spoons go in the spoon drawer. Forks go in the fork drawer and the Whiskernugen goes in the whiskernugen drawer." - Chowder

Following the logic of my cartoon hero; the the Windows questions are more accurately answered in the Windows section of the forum. The WinAPI function "GetCursorPos()" fills out a 'POINT' struct that describes the X and the Y 'coords' of the cursor at the moment it is called. You should put this in the event handler for the application, or you could provide some code if you need more specifics.

- GetCursorPos(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms648390(v=vs.85).aspx

- POINT: http://msdn.microsoft.com/en-us/library/windows/desktop/dd162805(v=vs.85).aspx
The next question I have would be... is this an actual window? Or is this in the console?

If you are using an actual window, Windows will send you a WM_LBUTTONDOWN event when the user clicks. This event will have X,Y coords in the lparam:

1
2
3
4
// in your event handler:
case WM_LBUTTONDOWN:
    x = LOWORD(lparam);
    y = HIWORD(lparam);


http://msdn.microsoft.com/en-us/library/windows/desktop/ms645607%28v=vs.85%29.aspx


EDIT:

The advantage to this over GetCursorPos is not only that you are notified when the user clicks... but the position you get back is in client coords (ie: 0,0 is the upper left corner of your game's drawing space).

Whereas GetCursorPos() will give you the position in screen coords (ie: 0,0 is the upper left corner of the user's monitor)... so you will have to manually convert to client coords.
Last edited on
I still think OP ought to have a better idea about how he is interfacing with the display. Is this console? Win32? Qt? SFML? FLTK?
Thanks, Disch, that helps a lot. It is in a window, so this would definitely be good to use. May I just ask, though, and I'm very sorry because I'm pretty new at this, what the LOWORD(lparam) AND HIWORD(lparam) things are? Thanks so much for all your help.
Topic archived. No new replies allowed.