Dialog Resize Helper

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:


#include “DlgResizeHelper.h”

class CMyDialog : public CDialog
{
//…
DlgResizeHelper m_resizeHelper;
//…
};

3. Initialize the resize helper in OnInitDialog():


BOOL CMyDialog::OnInitDialog()
{
//…
m_resizeHelper.Init(m_hWnd);

// 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:
m_resizeHelper.Fix(m_buttonCtrl,
DlgResizeHelper::kLeft,
DlgResizeHelper::kNoVFix);

// its item id:
m_resizeHelper.Fix(IDC_PRESS_ME,
DlgResizeHelper::kWidth,
DlgResizeHelper::kNoVFix);

// or all sub windows with a common class name (especially useful
// for single line edit controls)
m_resizeHelper.Fix(“Edit”,
DlgResizeHelper::kNoHFix,
DlgResizeHelper::kHeight);

//…
}

4. Tell the resize helper when the size has changed:


//…
BEGIN_MESSAGE_MAP(CDlg_resizerDlg, CDialog)
//{{AFX_MSG_MAP(CDlg_resizerDlg)
//…
ON_WM_SIZE()
//…
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CMyDialog::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
m_resizeHelper.OnSize();
}

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.

Downloads

Download demo project – 66 Kb

Download source – 3 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read