Creating a CTabCtrl Application

Environment: VC6 SP3, NT4, Windows 2000 Pro, Windows 2000 Server

Firstly add the CMyTabCtrl.cpp and CMyTabCtrl.h to your project.

Using Visual Studio add a CTabCtrl control to your dialog. Add a member variable for this
control as a CMyTabCtrl type not a simple CTabCtrl. CMyTabCtrl is derived from CTabCtrl.

You can simply add a CTabCtrl member variable and manually edit the file to become CMyTabCtrl

Now create dialogs for every page you require. These must be child dialogs with no border.
Any controls here in these dialogs are handled by their own class.

Create a Class for each dialog. In the example these are CTabOne, CTabTwo and CTabThree.

In the CmyTabCtrl constructor you must add a new for each page. Also in the Init
function you must specify all tab dialogs to hide all but one

In this example the OnLButtonDown message handler is used to show and hide the various
dialogs. The page is managed with a variable m_tabCurrent which holds the value of selected

In the main app dialogs OnInitDialog message handler add the tabs you require for your
dialog. In this example these are Tab One, Tab Two and Tab Three.

Functionality for the left and right cursor keys to select the tabs can be added in the
CMyTabCtrl class.

 m_tabPages[0]=new CTabOne;
 m_tabPages[1]=new CTabTwo;
 m_tabPages[2]=new CTabThree;


 for(int nCount=0; nCount < m_nNumberOfPages; nCount++){
  delete m_tabPages[nCount];

void CMyTabCtrl::Init()

 m_tabPages[0]->Create(IDD_TAB_ONE, this);
 m_tabPages[1]->Create(IDD_TAB_TWO, this);
 m_tabPages[2]->Create(IDD_TAB_THREE, this);



void CMyTabCtrl::SetRectangle()
 CRect tabRect, itemRect;
 int nX, nY, nXc, nYc;

 GetItemRect(0, &itemRect);


 m_tabPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);
 for(int nCount=1; nCount < m_nNumberOfPages; nCount++){
  m_tabPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);

// CMyTabCtrl message handlers

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
 CTabCtrl::OnLButtonDown(nFlags, point);

 if(m_tabCurrent != GetCurFocus()){

The Dialog Showing, Hiding and Drawing code is from the C++ Forum
On this site. It was posted by someone whom I don’t have name for.

If it was you then please let me know and I will have your name added

I hope this code is of some use. It has helped me to develop much more
complex dialogs.


Download demo project – 18 Kb
Download source – 2 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read