Jump to content
  • Advertisement
Sign in to follow this  
FMDGames

Getting the Cursor Position (C++) (WIN32)

This topic is 4508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to get the cursor position and am currently getting it from the windows message pump as follows:
case WM_MOUSEMOVE:
		int X=LOWORD(lParam);
		int Y=HIWORD(lParam);
		break;

However, the X and Y coordinates returned are not accurate to my application. I have a 1024 x 768 window, and if i position the cursor to the bottom right hand corner of my window, the co-ordinates: X:1017 Y:742 are returned. This is quite obviously not what i am expecting. Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
Isn't this because the borders/caption take up some of the room? Are you making the whole client area 1024x768 using AdjustWindowRect()?

Share this post


Link to post
Share on other sites
Smit is correct. WM_MOUSEMOVE gives you cursor coordinates relative to the top left corner of the client area. You need to adjust the size of the window to create a 1024x768 client area, use a WS_POPUP style window (No borders, so the client area size equals the window size), or use GetCursorPos() to get your mouse position in screen coordinates.

Share this post


Link to post
Share on other sites
If i use GetCursorPos() that gives me position of the cursor on the screen, how do i get the position relative to the drawing area?

In other words, how do i get the Left X coordinate and the Top Y coordinate of the window.

Thx

Share this post


Link to post
Share on other sites
Just use the PointToClient function...

EDIT: Ooops.. been using C# too much...

ScreenToClient

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/cordspac_5oxg.asp

Share this post


Link to post
Share on other sites
Quote:
Original post by FMDGames
If i use GetCursorPos() that gives me position of the cursor on the screen, how do i get the position relative to the drawing area?

In other words, how do i get the Left X coordinate and the Top Y coordinate of the window.

Thx


Hey Bud,

There is a function called ScreenToClient() that does this for you.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
You probably still want to use AdjustWindowRect() like others have suggested, otherwise your drawable window area is probably much less than 1024x768, which may cause behavior you don't expect later on.

Share this post


Link to post
Share on other sites
Can you post an example of how to use that function please? I've no idea at all where it would go in my jungle of code!

Share this post


Link to post
Share on other sites
Just before you call CreateWindow or CreateWindowEx to make a window.

Here is the MSDN link for AdjustWindowRect().

You'd use it something like this:


RECT wrect;
DWORD style = /* the same value as the 3rd param to CreateWindow */;

// Define desired client size.
wrect.left = 0;
wrect.top = 0;
wrect.right = 1024;
wrect.bottom = 768;

AdjustWindowRect(&wrect,style,false /* or true if window has a menu */);

/* now call CreateWindow or CreateWindowEx as before, use the width
* and height of wrect for the width and height of the window. Your
* window will have a 1024x768 client area.
*/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!