Jump to content
  • Advertisement
Sign in to follow this  
Rajveer

GetClientRect coordinates question

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

Apologies if this is in the wrong area.

I'm trying to check whether my mouse cursor is within the client rect of my application, so I get the client rect and the mouse coordinates, then set the mouse coordinates relative to the client rect's position, and then test if the mouse coordinates are within the size of the client rect:



//First get OpenGL client from window
RECT clientRect;
GetClientRect(hWnd,&clientRect);
ClientToScreen(hWnd, (LPPOINT)&clientRect.left);

//Then get mouse cursor pos
POINT ptCursorPos;
GetCursorPos(&ptCursorPos);

//Now calculate touch pos relative to OpenGL client position and size, and check whether window has focus or not
ptCursorPos.x -= clientRect.left;
ptCursorPos.y -= clientRect.top;

//Check if window has focus
HWND focusedWindow = GetFocus();
if(hWnd == focusedWindow)
{
//Check if cursor is within the window
if( (((ptCursorPos.x >= 0) && (ptCursorPos.x < screenWidth)) && ((ptCursorPos.y >= 0) && (ptCursorPos.y < screenHeight))) )
{
printf("Inside");
}
}


This works perfectly, however one thing I'm confused about is why I have to use ClientToScreen with clientRect.left but not with clientRect.top (as in this link http://support.microsoft.com/kb/11570 it says that left and top should both be 0, so by my understanding if I translated top and left, that would give the origin of the client rect in screen coordinates, and right and bottom would give the width and height of the rect)?

Share this post


Link to post
Share on other sites
Advertisement
ClientToScreen() accepts a pointer to a POINT struct. When you pass it the address of the left or right member of a RECT you interpret the left and top members as a POINT or right and bottom members as a POINT struct, because those sets of variables follow each other in memory the same way as the members of a POINT struct.

Share this post


Link to post
Share on other sites
See this page for an alternative method: http://blogs.msdn.com/b/oldnewthing/archive/2003/10/13/55279.aspx

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!