I've seen and tried several similar controls available in CodeProject and CodeGuru site.
None of them meets my requirement. Some of them are too complicated and some others miss functions I need.
CGroupCheck is a simple class derived from
A groupbox can be associated with this button (actually a checkbox).
It has the following features:
- The checkbox is moved to the top left corner of the groupbox and resized
- All controls within the groupbox are enabled when checkbox is checked, and
disabled when checkbox is unchecked.
- You can optionally hide the controls inside the groupbox when unchecked.
How to use it
CGroupCheck class is quite straightforward, as illustrated in the demo project.
- Create your controls on the dialog template as usual.
For this control to work, you need a checkbox, and a groupbox.
Inside the groupbox you can put any control.
It doesn't matter where the checkbox is. Also the tab order of the checkbox is unimportant.
The groupbox can have a caption, but it will be removed at run-time.
- Create a data member of
CGroupCheck in your dialog class. You can map this member to the checkbox control using DDX,
or subclass the checkbox in
OnInitDialog messege handler.
- Associate the groupbox with the checkbox by calling its
How does it work
CGroupCheck captures checkbox's click event and
SetCheck function to enable or
disable controls inside the associated groupbox.
At its core is the function
int nCheck = GetCheck();
CWnd* pWnd = GetParent()->GetWindow(GW_CHILD);
CRect rcWnd, rcTest;
if (rcTest.IntersectRect(rcGroupbox, rcWnd) &&
pWnd != this && pWnd != m_pGroupbox)
pWnd->EnableWindow(nCheck == 1);
pWnd->ShowWindow(nCheck ? SW_SHOW : SW_HIDE);
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
m_pGroupbox->ShowWindow(nCheck ? SW_SHOW : SW_HIDE);
When the checkbox is moved to the top of the groupbox, one problem is that the
checkbox window is normally longer than the caption.
That would make the underneath groupbox border invisible.
Adjusting the checkbox's size to fit the caption length is tedious.
Also, sometimes, when checkbox is moved on top of the groupbox, it's actually hiding behind the groupbox
(it looks once I got this problem, changing the tab order doesn't help).
All these problems are now taken care for you, when you associate the groupbox by calling
UINT nGroupboxID, BOOL bHideDisabled )
CButton* pGroupbox, BOOL bHideDisabled )
m_bHideDisabled = bHideDisabled;
m_pGroupbox = pGroupbox;
CFont* pOldFont = dc.SelectObject(GetFont());
CSize czText = dc.GetTextExtent(strText);
czText.cx += 25;
rc.top, czText.cx, czText.cy, 0);
How to associate one checkbox with multiple groupboxes?
You can create an invisible groupbox which includes these multiple groupboxes and associate the invisible groupbox
with the checkbox. In this case, my code doesn't work well if the flag
m_bHideDisabled is set
to true - it will make the invisible groupbox visible when checked.
I didn't fix it because it would require another data member to remember its original state, and in particular,
I don't need it at all. Of course, another limitation is that these groupboxes must be located together to fit
within that big groupbox. If you really look for such features, you may try
Enable/Disable a group of controls with one click,
or Paul S. Vickery's GroupControl.