Home   Index   About
Ultimate Pack


Custom Search
Creating a Tab Control

The example in this section creates a tab control and displays 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, 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. The following global variables are used in this example.

// Global variables

HINSTANCE g_hinst; // handle of application instance

char g_achTemp[256]; // temporary buffer for strings

HWND g_hwndMain; // main application window

HWND g_hwndTab; // tab control

HWND g_hwndDisplay; // handle of static control in

// tab control's display area

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_FIRSTDAY (defined in the application's 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.

// DoCreateTabControl - creates a tab control, sized to fit the

// specified parent window's client area, and adds some tabs.

// Returns the handle of the tab control.

// hwndParent - parent window (the application's main window)

HWND WINAPI DoCreateTabControl(HWND hwndParent)

{

RECT rcClient;

HWND hwndTab;

TC_ITEM tie;

int i;

// Get the dimensions of the parent window's client area, and

// create a tab control child window of that size.

GetClientRect(hwndParent, &rcClient);

InitCommonControls();

hwndTab = CreateWindow(

WC_TABCONTROL, "",

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 = g_achTemp;

for (i = 0; i < 7; i++) {

LoadString(g_hinst, IDS_FIRSTDAY + i,

g_achTemp, sizeof(g_achTemp));

if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1) {

DestroyWindow(hwndTab);

return NULL;

}

}

return hwndTab;

}

The following function creates the static control that occupies the tab control's display area. The application's initialization function calls this function after creating the main window and the tab control.

// DoCreateDisplayWindow - creates a child window (a static

// control) to occupy the tab control's display area.

// Returns the handle of the static control.

// hwndParent - parent window (the application's main window)

HWND WINAPI DoCreateDisplayWindow(HWND hwndParent)

{

HWND hwndStatic = CreateWindow("STATIC", "",

WS_CHILD | WS_VISIBLE | WS_BORDER,

0, 0, CW_USEDEFAULT, CW_USEDEFAULT,

hwndParent, NULL, g_hinst, NULL);

return hwndStatic;

}

Following are the relevant portions of the application's window procedure. The application processes the WM_SIZE message to position and size the tab control and the static control. To determine the appropriate position and size for the static control, this example sends the tab control a TCM_ADJUSTRECT message (by using the TabCtrl_AdjustRect macro).

When a tab is selected, the tab control sends a WM_NOTIFY message, specifying the TCN_SELCHANGE notification message. The application processes this notification message by setting the text of the static control.

// MainWindowProc - processes the message for the main window class.

// The return value depends on the message.

// hwnd - handle of the window

// uMsg - identifier for the message

// wParam - message-specific parameter

// lParam - message-specific parameter

LRESULT CALLBACK MainWindowProc(

HWND hwnd,

UINT uMsg,

WPARAM wParam,

LPARAM lParam

)

{

switch (uMsg) {

case WM_SIZE: {

HDWP hdwp;

RECT rc;

// Calculate the display rectangle, assuming the

// tab control is the size of the client area.

SetRect(&rc, 0, 0,

LOWORD(lParam), HIWORD(lParam));

TabCtrl_AdjustRect(g_hwndTab, FALSE, &rc);

// Size the tab control to fit the client area.

hdwp = BeginDeferWindowPos(2);

DeferWindowPos(hdwp, g_hwndTab, NULL, 0, 0,

LOWORD(lParam), HIWORD(lParam),

SWP_NOMOVE | SWP_NOZORDER

);

// Position and size the static control to fit the

// tab control's display area, and make sure the

// static control is in front of the tab control.

DeferWindowPos(hdwp,

g_hwndDisplay, HWND_TOP, rc.left, rc.top,

rc.right - rc.left, rc.bottom - rc.top, 0

);

EndDeferWindowPos(hdwp);

}

break;

case WM_NOTIFY:

switch (HIWORD(wParam)) {

case 0:

.

. // menu command processing

.

case TCN_SELCHANGE: {

int iPage = TabCtrl_GetCurSel(g_hwndTab);

LoadString(g_hinst, IDS_FIRSTDAY + iPage,

g_achTemp, sizeof(g_achTemp));

SendMessage(g_hwndDisplay, WM_SETTEXT, 0

(LPARAM) g_achTemp);

}

break;

}

break;

.

. // additional message processing

.

default:

return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

return 0;

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun