Environment: Windows 9x, Windows NT/2000
A DlgResizeHelper object helps you keeping a layout while resizing a dialog or – more generally – a window with child windows.
For this class I was inspired by an article by John Keogh in DDJ 06/2000 (“Layout Management”) and a look at Code Guru revealed
some solutions as well. Nevertheless I find my solution more handy (of course ;-): it’s C++ (in contrast to Keogh’s code) and
the class is not derived from CDialog (as most other solutions are) so it can be used with every window.
In the dialog above you some controls want to tell you the meaning of life, but what is it? Now you can simply resize
the dialog to read the desired info. Note that you can adjust the resize behaviour of the controls: the lists are resized in
height and width, the edit and combo are just resized in width and the buttons stay fixed on the right side where the radio
buttons belong to the lower list.
On initialization a DlgResizeHelper stores handles to all child windows of a parent window. Also you can optionally add other windows
which shall be resized with the parent window. On resize the DlgResizeHelper
resizes the child windows according to the size changes of the parent. By default a child is resized proportional
to the parent’s size but you can optionally fix the horizontal and/or vertical dimension/position of a child window.
E.g. it’s reasonable to fix the height of single line edit controls.
Usage (here e.g. with a dialog class):
1. Make your dialog resizable (give it a resizable border)
2. Add a DlgResizeHelper member to your dialog class:
class CMyDialog : public CDialog
3. Initialize the resize helper in OnInitDialog():
// default behaviour is to resize sub windows proportional to the
// resize of the parent you can fix horizontal and/or vertical
// dimensions for some sub windows by either specifying the
// sub window by its hwnd:
// its item id:
// or all sub windows with a common class name (especially useful
// for single line edit controls)
4. Tell the resize helper when the size has changed:
void CMyDialog::OnSize(UINT nType, int cx, int cy)
CDialog::OnSize(nType, cx, cy);
That’s all. In most cases it is ok if the user enlarges a dialog, but shrinking it would destroy the layout. So you might
choose to enforce a minimal dialog size by handling the WM_GETMINMAXINFO message (see demo software for an example).
The demo application is a dialog with a bunch of buttons. The name of the buttons reveal their fixed dimensions.