October 24, 2016
Hot Topics:

Creating a CTabCtrl Application

  • April 3, 2000
  • By Developer.com Staff
  • Send Email »
  • More Articles »

This article was contributed by Ben Hill.

screen shot

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 pages.

In the main app dialog's 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.

CMyTabCtrl::CMyTabCtrl(){ m_tabPages[0]=new CTabOne; m_tabPages[1]=new CTabTwo; m_tabPages[2]=new CTabThree; m_nNumberOfPages=3;}CMyTabCtrl::~CMyTabCtrl(){ for(int nCount=0; nCount < m_nNumberOfPages; nCount++){  delete m_tabPages[nCount]; }}void CMyTabCtrl::Init(){ m_tabCurrent=0; m_tabPages[0]->Create(IDD_TAB_ONE, this); m_tabPages[1]->Create(IDD_TAB_TWO, this); m_tabPages[2]->Create(IDD_TAB_THREE, this); m_tabPages[0]->ShowWindow(SW_SHOW); m_tabPages[1]->ShowWindow(SW_HIDE); m_tabPages[2]->ShowWindow(SW_HIDE); SetRectangle();}void CMyTabCtrl::SetRectangle(){ CRect tabRect, itemRect; int nX, nY, nXc, nYc; GetClientRect(&tabRect); GetItemRect(0, &itemRect); nX=itemRect.left; nY=itemRect.bottom+1; nXc=tabRect.right-itemRect.left-1; nYc=tabRect.bottom-nY-1; 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 handlersvoid CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point) { CTabCtrl::OnLButtonDown(nFlags, point); if(m_tabCurrent != GetCurFocus()){  m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);  m_tabCurrent=GetCurFocus();  m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);  m_tabPages[m_tabCurrent]->SetFocus(); }}

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

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel