Have you ever wanted to create a "tabbed window" effect, but found the amount of
information on the web lacking? Or, did you find out how to use "property sheets", but thought that method was too complicated?
I was in both of those situations for years, and it seemed like I would never be able to use tabs how I wanted.
That I couldn't "get" property sheets may be a testament of my programming ability. Yet, it may have
been a more mature programmer who was able to create a different, simpler method. I submit this method in
the hope that it's usefulness outweighs any criticisms.
How It Works
Uses normal dialogs, along with some positioning and hiding.
Child windows are placed on top of a tab control in the main window.
There are 3 major steps to getting this to work:
- Prepare the dialogs
- Set up main dialog's header file
- Code main dialog's methods
Prepare the Dialogs
Make a tab control on the main window. This is the style I used, but it shouldn't be important:
Create four standard blank dialogs, one for each tab, with these settings (style is important):
Check "Control parent" in "Extended Styles" if you want to be able to tab in and out from the tab control and the child windows.
Create the classes for each child dialog as you normally would in ClassWizard, and that's all you need to set up for them!
I put a simple, appropriately labeled Static Text control on each one just so I knew that it worked. You can resize the
dialogs to the planned run-time size for easier arrangement of their controls, but that will be handled programmatically
when the child window is shown.
Set Up Main Dialog's Header File
Include child window header files:
Create instances of the child window classes and the method for switching the child windows:
CSettingsDlg(CWnd* pParent = NULL);
void ShowWindowNumber(int number);
CRect structure to hold the position of the child windows:
Using ClassWizard, add a
WM_SHOWWINDOW message to the main dialog, and a
TCN_SELCHANGE message to the tab control.
Code Main Dialog's Methods
Method for showing and hiding child windows:
void CSettingsDlg::ShowWindowNumber(int number)
int windowCount = 4;
if ((number >= 0) && (number < windowCount))
m_dPointer = &m_dGeneralDlg;
m_dPointer = &m_dDisplayDlg;
m_dPointer = &m_dSoundsDlg;
m_dPointer = &m_dAdvancedDlg;
for (int count = 0; count < windowCount; count++)
if (count != number)
else if (count == number)
Whenever the main window is shown, show the first child window:
void CSettingsDlg::OnShowWindow(BOOL bShow, UINT nStatus)
When a tab is selected, show the right child window:
void CSettingsDlg::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult)
pNMHDR = NULL;
pResult = NULL;
m_rSettingsRect.left = 13;
m_rSettingsRect.top = 44;
m_rSettingsRect.right = tabRect.Width() - 7;
m_rSettingsRect.bottom = tabRect.Height() - 38;
tabItem.mask = TCIF_TEXT;
tabItem.pszText = _T(" &General ");
tabItem.pszText = _T(" Display ");
tabItem.pszText = _T(" Sounds ");
tabItem.pszText = _T(" Advanced ");
Now you can build it and see if it works!
Points of Interest
I didn't realize the current code in
ShowWindowNumber right away. It had a lot of
else conditions with essentially the same code in each.
I would not have shared this code if I had not found a better way to switch the windows. Thus,
ShowWindowNumber is the most important and interesting part of this article.
Where Can You Go From Here?
You can use this idea to make a settings dialog in your application.
The members of each tab window can be accessed like this:
CString string = m_dSettingsDlg.m_dGeneralDlg.m_sString;
There is a lot of room for improvement if used in an actual application, but this shows the basic implementation.