Click here to Skip to main content
13,191,570 members (64,374 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 21 Jun 2002

A WTL Tab Control for Managing Tab Views

, 21 Jun 2002
Rate this:
Please Sign up or sign in to vote.
This article describes a tab control that automates the management of tab windows.

TabViewCtrl Demo Image


If you are looking for general information about tab controls, you are in the wrong place. You can find general information about tab controls elsewhere (MSDN). If you are just looking for an easier way to use tab controls in WTL, read on.


Tab controls are powerful tools, but sometimes it can be a hassle to re-implement tab management every time you need a tab control. This class encapsulates the functionality for managing tabs.

Some similar controls are available for MFC, but I could not find any tab management controls for WTL. This class was originally a port of an MFC class CSizingTabCtrlBar (author unknown), but as work progressed, the implementation changed significantly.

The management of the "View" windows for each tab is loosely based on the pane management for WTL::CSplitterImpl. Child "View" windows are created by the application for each tab and submitted to the CWTLTabViewCtrl for management.


To use this class in an SDI WTL application, derive a class from CWTLTabViewCtrl. (You need to have WTL in your include path to compile.)

class CDemoTabViewCtrl : public CWTLTabViewCtrl

    BOOL PreTranslateMessage(MSG* pMsg)
        return FALSE;


And declare it as a member variable on the main frame.

CDemoTabViewCtrl    m_TabViewDemo;


In a WTL SDI application, create the tab view window as a child of the main frame and assign the window handle to the m_hWndClient member variable.

m_hWndClient = m_TabViewDemo.Create( 
                      m_hWnd, rcDefault, NULL, 
                      WS_CHILD | WS_VISIBLE, WS_EX_STATICEDGE );

Adding a tab

To add a tab to the tab control, simply call CWTLTabViewCtrl::AddTab, passing the tab label, the window to manage and optionally the activation state, the image index, and the param to associate with the tab.

CTabDemoHelp    m_DemoTabHwnd;

// Create the tab as a child of the tab control
m_DemoTabHwnd.Create( m_hWndClient );

// Add the tab to the tab control
m_TabViewDemo.AddTab( "Tab Name", m_DemoTabHwnd );

Removing a tab

To remove a tab from the tab control, call CWTLTabViewCtrl::RemoveTab with the zero based index of the tab to remove..

// Remove the tab by specifying the zero based index of the 
// tab to remove
m_TabViewDemo.RemoveTab( theIndexOfTheTabToRemove );

CWTLTabViewCtrl::RemoveTab( ) does not destroy the window handle passed to CWTLTabViewCtrl::AddTab. This should be done when CWTLTabViewCtrl::RemoveTab() is called or when the window is no longer needed.

You can also override the CWTLTabViewCtrl::OnTabRemoved virtual method in the derived tab control class to destroy the window or any memory allocated for the window.

Usage in other scenarios

Using this class in a WTL MDI or WTL dialog application should be straightforward. Let me know how it goes ;-)

Modifying the tab control styles

To modify the styles of the tab control, call the CWTLTabViewCtrl::ModifyTabStyle( ) method. Using this method to set or remove the tab styles, TCS_BOTTOM, TCS_RIGHT and TCS_VERTICAL will set the font to the appropriate orientation.

Evidently, a tab control can handle tabs of different orientations, though it fails to modify the text used to draw the tab label to the proper orientation. The WM_SETFONT message is used to set the appropriate font.

Image Lists

Image lists can be associated with any tab control. These image lists are used to determine the image that appears on each tab. An optional parameter of the CWTLTabViewCtrl::AddTab() method specifies the zero based index of the image in the image list that should appear next to the tab label.

API Reference

All WTL::CTabCtrl methods are available on CWTLTabViewCtrl since the latter is derived from the former. The Windows Help file included with the source download contains a complete API reference, compliments of DOxygen (


In order for the tab view control to receive the TCN_SELCHANGE message that notifies the control that a new tab has been selected, parent windows must have the REFLECT_NOTIFICATIONS() macro in their message maps.


If the tab view window is a child of another window, such as CSplitterWindow, you can use the FORWARD_NOTIFICATIONS() macro to forward the WM_NOTIFY messages.


Demo Application

The demo application is a simple WTL SDI application that houses the tab view control. It allows you to add and remove tabs and dynamically change the style of the tab control.

This application dynamically allocates a window class and stores it as the param associated with the tab. When the tab is removed, it destroys the window and deletes the window class in the overloaded CWTLTabViewCtrl::OnTabRemoved virtual method. It uses the image index to determine the type of window to be deleted.


For updates to this class, visit Send me an email if you end up using this class.

  • 06/18/2002 - Creation.


This article and all accompanying material is ©2002 Stephen Jones. All rights reserved. The compiled source code may be used at will.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Stephen Jones
Web Developer
United States United States
Stephen Jones is a Lead Software Engineer for Anark Corporation in Boulder, Colorado.

You may also be interested in...


Comments and Discussions

BugV607 Ownerless expression 'm_Views[inTab]'. wtltabviewctrl.h 226 Pin
PPA_PPA4-Apr-13 20:38
memberPPA_PPA4-Apr-13 20:38 
GeneralMy vote of 5 Pin
kevinhan408926-Mar-12 16:22
memberkevinhan408926-Mar-12 16:22 
Generalusing this control in WTL MDI project Pin
deepdiverxx24-Oct-10 19:13
memberdeepdiverxx24-Oct-10 19:13 
thanks the author and very good control to help to use the tab control easily. but when using this control in MDI child frame, when close the mainframe window, an error happen because of the set view focus function in the "RemoveTab()" function. so I have to modify the "RemoveAllTabs()" to remove the active next tab function since there is not necessary to active any tabs while removing all tabs
GeneralCatch Tab key in TabViews Pin
cact20-Nov-08 0:30
membercact20-Nov-08 0:30 
GeneralRe: Catch Tab key in TabViews Pin
zhouyeheng22-Dec-08 3:18
memberzhouyeheng22-Dec-08 3:18 
GeneralChild Windows Pin
anubis_ex30-Oct-07 5:55
memberanubis_ex30-Oct-07 5:55 
GeneralRe: Child Windows Pin
zhouyeheng22-Dec-08 3:19
memberzhouyeheng22-Dec-08 3:19 
GeneralMDI Pin
picazo4-Mar-06 16:32
memberpicazo4-Mar-06 16:32 
GeneralA error found when use TCS_MULTILINE style Pin
Rocom.liu27-Jan-05 16:50
memberRocom.liu27-Jan-05 16:50 
GeneralIncorrect link to demo app. Pin
blippblapp17-Nov-04 21:46
memberblippblapp17-Nov-04 21:46 
GeneralA fix for problems when using in Dialog Pin
mparedes25-Nov-03 12:43
sussmparedes25-Nov-03 12:43 
GeneralButtons in Tab Lock up App Pin
BillisWrong20-Nov-03 6:44
memberBillisWrong20-Nov-03 6:44 
GeneralWonderful Pin
ShawnTassie7-May-03 16:41
memberShawnTassie7-May-03 16:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171017.1 | Last Updated 22 Jun 2002
Article Copyright 2002 by Stephen Jones
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid