there something I am always curious about for example when I create window I always write the size of the window statically and each time keep trying till I find my right size,but I want to know how can I measure what I want before hand ? because in msdn states that size is in device units which according to screen its in pixels,but it doesn't give any information I have read about pixel measurements etc,but still didn't get into the right direction hopefully someone here can point me to right direction... thanks.
A little trick when setting up a non-resizeable window is to determine your Client area (the inside of the window) according to controls you are inserting... Once you know how much space the controls need, you can calculate the window size quite easily. It doesn't actually matter what the units of measurement are, so long as you use them consistently (But, you are correct that for a non-dialog window it is Pixels).
Here's an example:
In the example I've already created all my windows Wind is the main window, Wind is a small message window... Wind and Wind are toolbars. Now I need to wrap my main window around my display and rows of buttons. I know the button sizes so I can easily calculate the width and height I need for my client area and adjust 1, 2, and 3 to suit. From there it's a matter of querying the system for border sizes etc. and then finally adjusting the size of the main window to suit.
// calculate main window size
ww = bs * 8; // width of client area
wh = (bs * 2) + 28; // height of client area
// size display and buttons
// relocate toolbars
MoveWindow(Wind,0,bs + 28,ww,bs,1);
// calculate window rect
wr.right = ww + (GetSystemMetrics(SM_CXFIXEDFRAME) * 2);
wr.bottom = wh + (GetSystemMetrics(SM_CYFIXEDFRAME) *2)
// resize window
A scheme like this gives you the right size even if the user changes the border width, caption size etc. or switches to "classic view" where the borders are just thin lines.
I've attached a screen shot of the result...
Resizeable windows are a whole different animal...
thanks mate thats good way of measuring size.
I use AdjustWindowRect().
It returns the size you need to create a window if a given style, so that your client area will be the input'ed size.
EDIT: and works on the current settings of the OS. So large fonts, resolutions or special viewports (ie for people with impared sight) ect do not throw out the calcs
Have a read about 'twips' if you need to convert pixels to inches/mm.
Yep, that works pretty much like the code sample I posted.
Originally Posted by novacain
Thing is, I found it's sometimes not accurate, especially if you are using a fixed size window with a toolbar, band or menu bar.
I kinda fell into my way of doing things over time... I suppose I should revisit AdjustWindowRect and see if they've fixed the problems.
thanks for your suggestion a swell,also I didn't know abt twips now that I read it...
Originally Posted by novacain
it might come handy sometimes while coding gui stuff.