Skip to content

Instantly share code, notes, and snippets.

@lyf-is-coding
Last active August 17, 2022 07:11
Show Gist options
  • Save lyf-is-coding/82a6fc7eda1a355fa9516fe7c0567c14 to your computer and use it in GitHub Desktop.
Save lyf-is-coding/82a6fc7eda1a355fa9516fe7c0567c14 to your computer and use it in GitHub Desktop.
C++ Getting screen coordinates of window client-area from relative coordinates
// Window client-area is the area without Title bar,...
#include <Windows.h>
RECT rct;
// Getting upper-left and lower-right corners relative coordinates.
if (!GetClientRect(someHWND, &rct))
{
std::cout << "[GetClientRect] Error " << GetLastError() << '\n';
return;
}
// Because client coordinates are relative to the upper-left corner of a window's client area,
// the relative coordinates of the upper-left corner are always (0,0).
POINT top_left;
top_left.x = 0;
top_left.y = 0;
POINT bottom_right;
bottom_right.x = rct.right;
bottom_right.y = rct.bottom;
// Convert 2 corner relative coords to screen coords
if (!ClientToScreen(someHWND, &top_left) || !ClientToScreen(someHWND, &bottom_right))
{
std::cout << "[ClientToScreen] Error " << GetLastError() << '\n';
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment