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.
What you need to know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
Create a Tab Control in the Main Window
The following function creates the tab control and adds a tab for each day of the week. The names of the days are defined as string resources, consecutively numbered starting with IDS_SUNDAY (defined in the application's resource header file). Both the parent window and the tab control must have the WS_CLIPSIBLINGS window style. The application's initialization function calls this function after creating the main window.
#define DAYS_IN_WEEK 7
// Creates a tab control, sized to fit the specified parent window's client
// area, and adds some tabs.
// Returns the handle to the tab control.
// hwndParent - parent window (the application's main window).
//
HWND DoCreateTabControl(HWND hwndParent)
{
RECT rcClient;
INITCOMMONCONTROLSEX icex;
HWND hwndTab;
TCITEM tie;
int i;
TCHAR achTemp[256]; // Temporary buffer for strings.
// Initialize common controls.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_TAB_CLASSES;
InitCommonControlsEx(&icex);
// Get the dimensions of the parent window's client area, and
// create a tab control child window of that size. Note that g_hInst
// is the global instance handle.
GetClientRect(hwndParent, &rcClient);
hwndTab = CreateWindow(WC_TABCONTROL, L"",
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, rcClient.right, rcClient.bottom,
hwndParent, NULL, g_hInst, NULL);
if (hwndTab == NULL)
{
return NULL;
}
// Add tabs for each day of the week.
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
tie.pszText = achTemp;
for (i = 0; i < DAYS_IN_WEEK; i++)
{
// Load the day string from the string resources. Note that
// g_hInst is the global instance handle.
LoadString(g_hInst, IDS_SUNDAY + i,
achTemp, sizeof(achTemp) / sizeof(achTemp[0]));
if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1)
{
DestroyWindow(hwndTab);
return NULL;
}
}
return hwndTab;
}
The following function creates the static control that resides in the tab control's display area. The application's initialization function calls this function after creating the main window and the tab control.
Note that the static control is positioned in the tab control's display area, but it is itself a sibling of the tab control, not a child. This allows the static control to participate in the shared parent window's tab order. This is not significant for a static control, but it is a good practice in case it is replaced with a keyboard-accessible control like a button.
// Creates a child window (a static control) to occupy the tab control's
// display area.
// Returns the handle to the static control.
// hwndTab - handle of the tab control.
//
HWND DoCreateDisplayWindow(HWND hwndTab)
{
HWND hwndStatic = CreateWindow(WC_STATIC, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 100, 100, 100, // Position and dimensions; example only.
GetParent(hwndTab), NULL, g_hInst, // g_hInst is the global instance handle
NULL);
return hwndStatic;
}
The following example functions are called from the application's window procedure. The application calls the OnSize
function when processing the WM_SIZE message to position and size the tab control to fit the main window's client area.
When a tab is selected, the tab control sends a WM_NOTIFY message, specifying the TCN_SELCHANGE notification code. The application's OnNotify
function processes this notification code by setting the text of the static control.
// Handles the WM_SIZE message for the main window by resizing the
// tab control.
// hwndTab - handle of the tab control.
// lParam - the lParam parameter of the WM_SIZE message.
//
HRESULT OnSize(HWND hwndTab, LPARAM lParam)
{
RECT rc;
if (hwndTab == NULL)
return E_INVALIDARG;
// Resize the tab control to fit the client are of main window.
if (!SetWindowPos(hwndTab, HWND_TOP, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), SWP_SHOWWINDOW))
return E_FAIL;
return S_OK;
}
// Handles notifications from the tab control, as follows:
// TCN_SELCHANGING - always returns FALSE to allow the user to select a
// different tab.
// TCN_SELCHANGE - loads a string resource and displays it in a static
// control on the selected tab.
// hwndTab - handle of the tab control.
// hwndDisplay - handle of the static control.
// lParam - the lParam parameter of the WM_NOTIFY message.
//
BOOL OnNotify(HWND hwndTab, HWND hwndDisplay, LPARAM lParam)
{
TCHAR achTemp[256]; // temporary buffer for strings
switch (((LPNMHDR)lParam)->code)
{
case TCN_SELCHANGING:
{
// Return FALSE to allow the selection to change.
return FALSE;
}
case TCN_SELCHANGE:
{
int iPage = TabCtrl_GetCurSel(hwndTab);
// Note that g_hInst is the global instance handle.
LoadString(g_hInst, IDS_SUNDAY + iPage, achTemp,
sizeof(achTemp) / sizeof(achTemp[0]));
LRESULT result = SendMessage(hwndDisplay, WM_SETTEXT, 0,
(LPARAM) achTemp);
break;
}
}
return TRUE;
}
Related topics