Figure 1: Dialog as it appears in Resource Editor. The same dialog holds properties for line, rectangle and circle objects.


Figure 2: Two run-time versions of the same dialog showing controls only relevant for circle object. The second dialog has "Fill" box checked and expanded at run-time.
Introduction
CStackableGroupBox
allows you to create a single dialog resource and a single dialog class that hold several groups of controls and selectively show those groups at run time. Hiding a group automatically shrinks the dialog and brings up all the controls below it. Group can also include a checkbox control that always remains visible.
Background
Sometimes you need to write a dialog for objects that are different but share some of the same properties. The easiest approach is to write a series of similar dialogs using copy/paste, but maintaining and expanding them is a big headache. You can also stick all the controls in one dialog and disable the ones that don't apply. Another alternative is to organize data in groups and only show applicable groups for each object. That's where CStackableGroupBox
comes in handy.
It is also applicable in a situation where you are writing a dialog with a group of advanced controls that need not be shown to the user by default to avoid overwhelming him/her. But when clicked on "Advanced" checkbox or button, the advanced controls should show. See "Fill" group in Figure 2 for an example.
Using the Code
- Include StackableGroupBox.h and StackableGroupBox.cpp files in your project.
- In resource editor, organize controls that will be toggled at runtime into groups. Give each groupbox control unique ID (i.e. not
IDC_STATIC
).
- In the header class for your dialog, add
CStackableGroupBox
instances for each group.
- In the
OnInitDialog
, call init_group
with IDs of the associated checkbox (0 if none) and groupbox control ID.
- In
OnInitDialog
, call show_group_expand_dialog
to set initial visibility state.
- Call
show_group_expand_dialog
anytime a specific group needs to be shown or hidden.
Here's the code from the demo provided (I used a modified version of UndoDemo
from my UndoHistoryPopup article):
class CDlg_AddObject : public CDialog
{
protected:
CStackableGroupBox m_group_line;
CStackableGroupBox m_group_circle;
CStackableGroupBox m_group_rect;
CStackableGroupBox m_group_fill;
};
BOOL CDlg_AddObject::OnInitDialog()
{
m_group_circle.init_group(this, 0, IDC_GROUP_CIRCLE);
m_group_rect.init_group(this, 0, IDC_GROUP_RECTANGLE);
m_group_line.init_group(this, 0, IDC_GROUP_LINE);
m_group_fill.init_group(this, IDC_CHECK_FILL, IDC_GROUP_COLOR);
m_group_circle.show_group_expand_dialog(obj.type == DOT_ELLIPSE, this);
m_group_rect.show_group_expand_dialog(obj.type == DOT_RECTANGLE, this);
m_group_line.show_group_expand_dialog(true, this);
m_group_fill.show_group_expand_dialog
(obj.type != DOT_LINE && obj.do_fill, this);
GetDlgItem(IDC_CHECK_FILL)->EnableWindow(obj.type != DOT_LINE);
return TRUE;
}
void CDlg_AddObject::OnCheckFill()
{
obj.do_fill = (BST_CHECKED == IsDlgButtonChecked(IDC_CHECK_FILL));
m_group_fill.show_group_expand_dialog
(obj.type != DOT_LINE && obj.do_fill, this);
}
How It Works
When you call init_group
, all the controls inside the groupbox are saved in the array. Another array stores all dialog controls below the group. This should be done during the dialog's initialization process to correctly position controls later.
Calling show_group_expand_dialog
hides controls of that group, then cycles through the stored array of controls below the group and moves them all up in the newly opened space. The dialog window is then contracted by the same height difference.
Limitations
- Controls placed to the right or left side of the bottom-most group will not be shifted up when the group is hidden. You will need to do it manually.
- Controls added or moved in and out of the groupbox area after you call
init_group
will not be properly positioned later.
- You can set only one control to always stay visible. So if at the header of your group there's more than one control, you need either modify this class or move them above the groupbox.
History
- 2007-05-16 - Created the class
- 2009-02-01 - Renamed to
CStackableGroupBox
and released for CodeProject