How to get a perfectly sized client area

I’ve been fighting for a while trying to figure out how to make the size of a window’s client area, exactly a specified size. There is some information out there on the internet, but it’s not that great. Even the MSDN documentation is a little confusing. So i set out on a quest.

Lets say you want your window client size to be exactly 800 x 600. This means 800 x 600 of viewable space and does not include scroll bars or menu bars. Here is how I accomplished it. I know it is correct because I measured it with a paint program.

For this little snippet, I have made the first two elements the top left corner of the window positioned on the screen. Not the client area position, but the window overall. The second two are the width and height in pixels of the client area. Now lets adjust our window and create it.

We just need another rectangle. This rectangle is going to represent the size of our client area as a whole.

So the contents of r2 = { 0, 0, 800, 600 }; Now we pass it to AdjustWindowRect to have it automatically adjust the r2 structure to take into account the style of the window. In other words, we give it the size of the client area, and it gives us back the size of the entire window. Note, it does not take into account scroll bars and multileveled menu bars. See the MSDN documentation for more info. But overall, this should work.

Now that we have our adjusted rectangle, we need to create our window with the same style (WS_OVERLAPPEDWINDOW), and our new dimensions.

The important arguments are: r.left,, r2.right – r2.left, and r2.bottom –

CreateWindow() wants the top left position of the window (which was supplied in r), and it wants the width and height of the entire window. Well, “AdjustWindowRect” doesn’t give us that, it gives us the new dimensions of the entire window. So we do some simple math to calculate the width and height of the entire window noting that “right” is always greater than “left” and “bottom” is always greater than “top.” This is do to window’s coordinate system.

Leave a Reply

You must be logged in to post a comment.