Click here to Skip to main content
15,906,329 members
Articles / Programming Languages / C++

Simple Tab Control for Visual C++: Part 2

Rate me:
Please Sign up or sign in to vote.
2.84/5 (21 votes)
5 Jun 2007CPOL2 min read 107.5K   3.5K   31   37
An easy way to develop interfaces with the tab control.

Sample Image - Simple_Tab_Control.jpg

Introduction

I take this opportunity to thank all my friends who commented and gave ideas about my previous tab control. It made me build this useful and easy tab control for all of you.

About this new tab control

If you are using this particular tab control, you have to do the following steps:

  1. Drag and drop the VC++ tab control on to your dialog.
  2. Add CIbTabCtrl.c and CIbTabCtrl.h to your solution.
  3. Derive the tab control variable from CIbTabCtrl.

You are free to design your tab pages using property pages. Add property pages to your application using the Add Resource option. Then create a property page associated class using CPropertyPage. You can add any number of property pages to your application. To make it visually suitable for a tab control, you can set the following property values:

  • Set Border property to None
  • Set Control property to True (this is very important; if it is false, the tab key function will not have any effect).
  • Set the Disable property to False

Once you create your property pages, you have to link those pages with your tab control.

How to link pages with the tab control

  1. Create property page objects as members of the dialog where your control is (if property page classes are CPPone and CPPTwo):
  2. C++
    CPPOne m_oPPOne;
    CPPTwo m_oPPTwo;
  3. On the OnInitDialog member function:
  4. C++
    m_oPPOne.Create(IDD_PP_ONE);
    m_oPPTwo.Create(IDD_PP_TWO);
    
    // Using the addNewPage() function, user can set tab caption and the
    // property page user wants to attach with it.
    
    // setDefaultPage() will determine which page should have the very first focus.
    
    m_ctrlTabV.addNewPage("My Page 1",&m_oPPOne);
    m_ctrlTabV.addNewPage("My Page 2",&m_oPPTwo);
    m_ctrlTabV.setDefaultPage(0); 

That's it! This is all you have to do to use this control in your solution.

For those interested in the CIbTabCtrl class

This class is derived from the MFC CTabCtrl class. I have added three functions:

  1. AddNewPage(CString strPage, CWnd * pPage) (public)
  2. This will keep track of all the pages added. Here I am using the CArray member variable to keep those pages.

    C++
    InsertItem(this->GetItemCount(),strPage);
    pPage->ShowWindow(SW_HIDE);
    m_oPages.Add(pPage); // store page;
  3. setPage(CWnd* pWnd) (private member function)
  4. This will display and draw a page according to the size of the tab control.

    C++
    CIbTabCtrl::setPage(CWnd* pWnd)
    {
        CWnd * pPage;
        pPage = m_oPages.GetAt(m_iPrevPage);
        pPage->ShowWindow(SW_HIDE);
        CRect oRect,oWRect,oPWRect,oIRect;
        GetItemRect(0,oIRect);
        GetClientRect(oRect);
        GetWindowRect(oWRect);
        GetParent()->GetWindowRect(oPWRect);
        pWnd->SetWindowPos(this,oWRect.left-oPWRect.left,oWRect.top-oPWRect.top,
              oRect.Width()- 5,oRect.Height()-oIRect.Height() - void 6, SWP_SHOWWINDOW);
    }
  5. setDefaultPage(int iIndex) (public)
  6. Sets the default page.

  7. removePage(int iIndex) (public)
  8. This will remove the tab page from the tab control.

Hope this description is enough for you to use this control. You are free to ask any question on this article. I am sure you will enjoy this control very much.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
I am working as a Tech Lead. I love VC++.
I am trying to get into new technologies coming with VC++ and also in other areas too.

Currently I am working in C# .Net as well...

Now I have sound knowledge in C# as well as in VC++.

Comments and Discussions

 
QuestionComplete source code? Pin
Member 1087179312-Dec-16 21:49
Member 1087179312-Dec-16 21:49 
AnswerRe: Complete source code? Pin
venura c.p.w. goonatillake19-Jun-17 19:16
venura c.p.w. goonatillake19-Jun-17 19:16 
QuestionMouse event on ListControl Pin
Sharad142835-Jul-13 23:09
Sharad142835-Jul-13 23:09 
GeneralSome keyboard (navigation) keys are not working as expected Pin
User 2694225-Aug-10 2:18
professionalUser 2694225-Aug-10 2:18 
QuestionWhat is the meaning of the constants (5 and 6) passed to SetWindowPos in fucntion setPage [modified] Pin
User 2694218-Aug-10 23:22
professionalUser 2694218-Aug-10 23:22 
AnswerRe: What is the meaning of the constants (5 and 6) passed to SetWindowPos in fucntion setPage Pin
venura c.p.w. goonatillake21-Aug-10 23:36
venura c.p.w. goonatillake21-Aug-10 23:36 
GeneralRe: What is the meaning of the constants (5 and 6) passed to SetWindowPos in fucntion setPage Pin
User 2694225-Aug-10 2:08
professionalUser 2694225-Aug-10 2:08 
GeneralRe: What is the meaning of the constants (5 and 6) passed to SetWindowPos in fucntion setPage Pin
venura c.p.w. goonatillake29-Aug-10 17:34
venura c.p.w. goonatillake29-Aug-10 17:34 
GeneralRe: What is the meaning of the constants (5 and 6) passed to SetWindowPos in fucntion setPage Pin
User 2694229-Aug-10 21:37
professionalUser 2694229-Aug-10 21:37 
GeneralHIde/show tab pages Pin
mcory200219-Jul-10 22:56
mcory200219-Jul-10 22:56 
GeneralRe: HIde/show tab pages Pin
venura c.p.w. goonatillake20-Jul-10 1:11
venura c.p.w. goonatillake20-Jul-10 1:11 
GeneralOnTcnSelchange Pin
Rangarajan Varadan23-Feb-09 5:07
Rangarajan Varadan23-Feb-09 5:07 
GeneralTabs are not visible. Pin
Member 205869725-Nov-08 3:15
Member 205869725-Nov-08 3:15 
GeneralTabs within the main Tab aren't visible Pin
adamatschka4-Nov-08 3:27
adamatschka4-Nov-08 3:27 
QuestionHow to avoid UpdateData or overcome this problem Pin
Rangarajan Varadan27-Sep-08 14:35
Rangarajan Varadan27-Sep-08 14:35 
AnswerRe: How to avoid UpdateData or overcome this problem Pin
venura c.p.w. goonatillake28-Sep-08 6:05
venura c.p.w. goonatillake28-Sep-08 6:05 
Please send me your code, I can look at it. Coz it is difficult
to think what the problem is.

Thank You
Venura

VENURA GOONATILLAKE, SRI LANKA

QuestionHow to convert my Dialogue into a Tab Pin
Rangarajan Varadan27-Aug-08 6:38
Rangarajan Varadan27-Aug-08 6:38 
AnswerRe: How to convert my Dialogue into a Tab Pin
venura c.p.w. goonatillake27-Aug-08 18:58
venura c.p.w. goonatillake27-Aug-08 18:58 
GeneralYou got my 5! Pin
flippydeflippydebop1-Jun-07 20:09
flippydeflippydebop1-Jun-07 20:09 
GeneralRe: You got my 5! Pin
venura c.p.w. goonatillake3-Jun-07 17:32
venura c.p.w. goonatillake3-Jun-07 17:32 
GeneralRe: You got my 5! Pin
Sam_c26-Jul-07 4:08
Sam_c26-Jul-07 4:08 
QuestionNo content in tab panes Pin
jalstadt24-Nov-06 3:48
jalstadt24-Nov-06 3:48 
AnswerRe: No content in tab panes Pin
venura c.p.w. goonatillake26-Nov-06 15:45
venura c.p.w. goonatillake26-Nov-06 15:45 
QuestionRe: No content in tab panes Pin
jalstadt26-Nov-06 21:03
jalstadt26-Nov-06 21:03 
AnswerRe: No content in tab panes Pin
jalstadt27-Nov-06 4:06
jalstadt27-Nov-06 4:06 

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.