5 out of 19 rated this helpful - Rate this topic

Using Tab Controls

This topic contains two examples that use tab controls. The first example demonstrates how to use a tab control to switch between multiple pages of text in an application's main window. The second example demonstrates how to use a tab control to switch between multiple pages of controls in a dialog box.

In this section

TopicDescription

How to Create a Tab Control in the Main Window

The example in this section demonstrates how to create a tab control and display it in the client area of the application's main window. The application displays a third window (a static control) in the display area of the tab control. The parent window positions and sizes the tab control and static control when it processes the WM_SIZE message.

There are seven tabs in this example, one for each day of the week. When the user selects a tab, the application displays the name of the corresponding day in the static control.

How to Create a Tabbed Dialog Box

The example in this section demonstrates how to create a dialog box that uses tabs to provide multiple pages of controls. The main dialog box is a modal dialog box. Each page of controls is defined by a dialog box template that has the WS_CHILD style. When a tab is selected, a modeless dialog box is created for the incoming page and the dialog box for the outgoing page is destroyed.

Note   In many cases, you can implement multiple-page dialog boxes more easily by using property sheets. For more information about property sheets, see About Property Sheets.

The template for the main dialog box simply defines two button controls. When processing the WM_INITDIALOG message, the dialog box procedure creates a tab control and loads the dialog box template resources for each of the child dialog boxes.

 

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Dialog Example Fails if (when) Studio Converts DIALOG to DIALOGEX
$0$0 The dialog sample code fails if (when) Visual Studio uses a DIALOGEX instead of a DIALOG resource in the .rc file.
A complete code example should be provided
I think you should provide a full code example with nested controls in order this topic can be fully understood.
Dialog Font and Base Units

GetDialogBaseUnits() assumes you are using the system font.

If you are using another font, change

DWORD dwDlgBase = GetDialogBaseUnits(); 
int cxMargin = LOWORD(dwDlgBase) / 4;
int cyMargin = HIWORD(dwDlgBase) / 8;

to

RECT rc;
rc.right = 4;
rc.bottom = 8;
MapDialogRect(hwndDlg, &rc);
int baseUnitX = rc.right;
int baseUnitY = rc.bottom;
int cxMargin = baseUnitX / 4;
int cyMargin = baseUnitY / 8;

and change

rcTab.right = rcTab.right * LOWORD(dwDlgBase) / 4; 
rcTab.bottom = rcTab.bottom * HIWORD(dwDlgBase) / 8;

to

rcTab.right = rcTab.right * baseUnitX / 4; 
rcTab.bottom = rcTab.bottom * baseUnitY / 8;
Code assumes the resource exists in the EXE

Dialog Example Warning

The line:

HRSRC hrsrc = FindResource(NULL, lpszResName, RT_DIALOG); 

assumes the resource you are looking for exists in the "main exe". However, this might not be the case, so whatever you pass into "LoadResource" on the next line:

 HGLOBAL hglb = LoadResource(g_hinst, hrsrc); 

You will need to pass that HINSTANCE into FindResource instead of NULL. Just a "gotcha" that you might not be expecting when using sample code snippets. My final code is:

HRSRC hrsrc = FindResource(g_hinst, lpszResName, RT_DIALOG); 
HGLOBAL hglb = LoadResource(g_hinst, hrsrc);

Once I fixed that up, loading in a DLL worked fine. Don't forget error checking!