Sign in to follow this  
FMDGames

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

Recommended Posts

FMDGames    134
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
Smit    310
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
Evil Steve    2017
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
FMDGames    134
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
e-u-l-o-g-y    196
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
dave    2187
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
jpetrie    13154
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
jpetrie    13154
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
FMDGames    134
I'm really confused. Still having problems getting exact coordinates. The coordinates are spot on when y is near 0 (top), but the greater y is, the bigger the gap is. So basically, the mouse Y increase faster than the direct3d Y.

I've tried the direct input way with the same results. Surely everyone else doesn't have this problem?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this