Archived

This topic is now archived and is closed to further replies.

Geting the cursors X Y coordinates

This topic is 6015 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

Hey, Could anyone quickly copy and paste the function that will give me the x,y coordinates of the cursor on the screen (a function, rather than the windows messages). Normally I''d look this kinda thing up quickly at MSDN but I''ve been there for ages and found nothing, laregly because now they have re designed the site, not all the information is up there yet. If you have this information i would be grateful if you could give it to me, thanks. Dachande btw: I''m not using mfc or direct input, so a straight windows function is what I''m really looking for. =)

Share this post


Link to post
Share on other sites
The function you are looking for is:

  
GetCursorPos( &ptMouseLoc );

You pass this function a pointer to a POINT structure, and it will fill ''ptMouseLoc.x'' with the mouse''s X location and ''ptMouseLoc.y'' with the mouse''s Y location.

Note, the coordinate returned by this function is relative to your window, and is in the coordinates you are currently mapping to (if you didn''t mess with anything, it should be in pixel coordinates, with positive Y going downwards, so don''t worry about that).

If you want to get the cursor''s position relative to the screen (where 0,0 is the top left corner of the monitor, not your window), then use the following code:

  
POINT ptMouseLoc; // At the end of this code, this will hold

// the mouse coordinate relative to the monitor

RECT rcWindow; // Holds the coordinates of your window


// Get mouse coordinate

GetCursorPos( &ptMouseLoc );

// Figure out where the window is at

GetClientRect( hWnd, &rcWindow );
ClientToScreen( hWnd, (POINT*)&rcWindow );
ClientToScreen( hWnd, (POINT*)&rcWindow+1 );

// Translate the mouse coordinate

ptMouseLoc.x -= rcWindow.left;
ptMouseLoc.y -= rcWindow.top;

If you are running at full screen, you don''t really need to worry about that.

If you want to test if a mouse button is down, you can use the following function:
  
if( GetAsyncKeyState( keyState ) )
{
// button is pressed

}
// Where keyState is either:

// VK_LBUTTON for left button

// VK_RBUTTON for right button


There may be more keystates, so you may want to look up the documentation. But for just left and right, those are enough.

Hope that helps. I remember when I was starting out, i couldn''t easily find information like this, but know I know how to do it, so, there it is.

Share this post


Link to post
Share on other sites
the function you''re looking for is BOOL GetCursorPos(LPPOINT lpPoint); which fills a point-structure with the current cursor position on the screen.

Share this post


Link to post
Share on other sites
Thanks, thats exactly what I needed. I''m not really starting out - I''ve been doing this stuff for ages, though I''ve never had a use for such a function before!! MSDN is half down right now, which makes things kinda difficult unfortunately.

Thanks again,

Dachande

Share this post


Link to post
Share on other sites
Actually, the code I wrote for polling the mouse-button state isn''t quite correct. It should be:

  
if( GetAsyncKeyState( keyState ) & 0x80 )
{
// button is pressed

}


MSDN says the following about the function:
quote:
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

Share this post


Link to post
Share on other sites
While I''m here, might someone also tell me how to set the mouse speed?

I must admit, the mouse is new territory for me =) I''ve always found that the keyboard is enough.

Thanks,

Dachande

Share this post


Link to post
Share on other sites
To set the mouse speed, you can use the following code (copied from MSDN):

  
int aMouseInfo[3]; // array for mouse information


// Get the current mouse speed.

SystemParametersInfo(SPI_GETMOUSE, // get mouse information

NULL, // not used

&aMouseInfo, // holds mouse information

NULL); // not used


// Modify the mouse speed (double it in this case)

aMouseInfo[2] = 2 * aMouseInfo[2];

// Change the mouse speed to the new value and update WIN.INI.

SystemParametersInfo(SPI_SETMOUSE, // set mouse information

NULL, // not used

aMouseInfo, // mouse information

SPIF_UPDATEINIFILE); // update win.ini



The mouse speed (aMouseInfo[2]) is an integer that ranges from 1 (slowest) to 20 (fastest), and has a default value of 10. By using SPI_GETMOUSE, you also can read the threshold values, so you could change the acceleration stuff. I would have used simpler code (usinig SPI_GETMOUSESPEED), but for some reason, it wouldnt compile.

Note, this function changes system wide mouse speed (it is the exact same thing as if you were to open up the mouse control panel). If you just want to change the mouse speed in your application, you should do it differently (note, you could still do this, but make sure to reset the original value afterwards).

Share this post


Link to post
Share on other sites
Thanks again.

Though changing the value of aMouseInfo[2] just seems to make the mouse speed go t minimum, or if set to 10, the speed stays as it is. Strange.

Thanks,

Dachande

Share this post


Link to post
Share on other sites