Reputation: 27
How do I get the client size and position with window rect? Is this possible?
Upvotes: 1
Views: 1958
Reputation: 27
I found a solution:
RECT GetClientRectFromWindowRect(HWND hWnd, RECT rect)
{
RECT v = { 0 };
AdjustWindowRectEx(&v,
GetWindowLong(hWnd, GWL_STYLE),
!!GetMenu(hWnd),
GetWindowLong(hWnd, GWL_EXSTYLE)
);
RECT ret = { 0 };
ret.bottom = rect.bottom - v.bottom;
ret.left = rect.left - v.left;
ret.right = rect.right - v.right;
ret.top = rect.top - v.top;
return ret;
}
Upvotes: 0
Reputation: 6240
Not sure what exactly you are trying to find out. Maybe try something like this:
#include <iostream>
#include <windows.h>
int main()
{
RECT r;
HWND h = GetConsoleWindow(); // or whatever window needed
GetWindowRect(h, &r);
std::cout << "Relative Client X,Y: " << r.left << "," << r.top << std::endl;
std::cout << "Relative Client W,H: " << r.right - r.left << "," << r.bottom - r.top << std::endl;
GetClientRect(h, &r);
std::cout << "Client X,Y: " << r.left << "," << r.top << std::endl;
std::cout << "Client W,H: " << r.right - r.left << "," << r.bottom - r.top << std::endl;
}
For example:
Relative Client X,Y: 100,100
Relative Client W,H: 947,594
Client X,Y: 0,0
Client W,H: 910,552
And/or if you want to get client area position relative to the screen, you can use ClientToScreen function. For example:
#include <windows.h>
int main()
{
HWND h = GetConsoleWindow(); // or provided HWND
POINT p{}; // defaulted to 0,0 which is always left and top of client area
ClientToScreen(h, &p);
SetCursorPos(p.x, p.y); // places cursor to the 0,0 of the client
}
Upvotes: 1
Reputation: 25
If you have a window RECT, the upper left corner is the window position and also you can calculate the size using upper left and lower-right corners of the window.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633519(v=vs.85).aspx
Upvotes: 0