Sign in to follow this  

Getting correct client window size

Recommended Posts

This question involves Win32, so it isn't exactly DirectX, but the line between the two is pretty blurred, so this seemed the appropriate forum. If I call CreateWindow passing it a width and height, the resulting window's non-client rectangle has this width and height, not the client window. To get the appropriately sized client size rectangle, I have to do a pretty hackish, though thus far reliable, trick:
// psuedo code
HWND hwnd = CreateWindow(.., width, height, ...);
RECT almost;
GetClientRect(hwnd, &almost);
SetWindowPos(hwnd, 0, 0, 0, 
             2*width-almost.right, 2*height-almost.bottom, 
The trick basically assumes that the non-client area is bigger than the client area by a constant amount, so you can know how much to overshoot to get the right client size. I know this has to be a common problem, so is there a better way to do this? I tried to find a deterministic way to translate a desired client rectangle into the size of its surrounding non-client window, but I could find none on MSDN or elsewhere. Any ideas? Thanks

Share this post

Link to post
Share on other sites
The function to use to achieve this is AdjustWindowRect.

RECT r = {0, 0, ClientWidth, ClientHeight};
AdjustWindowRect(&r, GetWindowLong(hWnd, GWL_style), GetMenu(hWnd));
int WindowWidth = r.right - r.left;
int WindowHeight = r.bottom -;

Then call MoveWindow or UpdateWindow to update your window size.

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