First of all, sorry for my English, I'm from Ukraine. :)
The main idea was to make a collapsed groupbox. I found a large number of C# groupboxes I needed, but none in C++. And I had no time to create it from the beginning, so I used the class described here by JackCa. After fixing some bugs and writing several functions, I got a nice variant of collapsed groupbox.
How To Use It
You have to include a class header in your dialog h file:
#include <span class="code-string">"CollapseGroupBox.h"</span>
Then change the base class of your groupbox variables from
If you don't have groupbox's variables, use class wizard to create them. Select "Control" category for them.
Using the Code
First of all, if your groupbox has a caption string and you like caption's right alignment, and your dialog isn't scalable, you have to do nothing at all to get good results. Nonetheless, I'll give you a brief description of how to use this class.
Let's talk about caption.
Caption variants are
My dialog has 4 groupboxes. And groupbox 1 and 3 will have right caption alignment.
You also have the possibility to change box visualization as you want. These functions allow to modify colors:
And these - to modify caption string and its visualization:
SetFontName(const CString& strFont, BYTE byCharSet = ANSI_CHARSET);
Also if your dialog
COLOR_BTNFACE, use function
setBGColor(COLORREF bgColor); for nice background visualization.
Now, let's talk about collapsing.
std::vector<cwnd* /> intoElements;
BOOL IsInGroupBox(CWnd* pCtrl) const;
void ShowElements(bool bShow);
As you see, we have a vector of elements located inside in groupbox. Function
IsInGroupBox tells us about location of one element, and function
GetGroupBoxElements check the location of everything in the dialog and fills our vector. But if you want, you may fill it by your own hands. :)
And now the most interesting thing. In my dialog, box4 is in box3. And, without this code, collapsing works incorrectly:
BOOL CGBdemoDlg::OnCommand(WPARAM wParam, LPARAM lParam)
if (wParam == UM_UPDATEFORM)
if (lParam == (LPARAM)m_st3.m_hWnd)
if (m_st3.m_frameMinimized == false)
if (wParam == UM_MOUSELEAVE)
if (wParam == UM_MOUSEENTRY)
return CDialog::OnCommand(wParam, lParam);
Our groupbox sends three variants of messages:
- mouse leaves groupbox
- mouse comes to groupbox
- groupbox changed its state
So, we can control everything we need. For example, in my application (not in this demo) I use
UM_UPDATEFORM to size dialog vertically for hiding free space.
A few words about dynamic creation... try this:
m_boxMain.Create(_T("Caption"),WS_CHILD | WS_VISIBLE |
About one of the most popular questions "I can't see any controls in groupbox".
The simplest answer is Open dialog in editor, select groupbox, Ctrl+C, Del, Ctrl+V and everything will be OK. :)
Or you can add
private function for changing Z-order or elements in vector
intoElements and put it at the end of
- 8th October, 2009: Initial version