Click here to Skip to main content
14,835,966 members
Articles / Desktop Programming / MFC
Posted 30 Apr 2004


32 bookmarked

A Tabbed MDI Frame Window

Rate me:
Please Sign up or sign in to vote.
2.95/5 (10 votes)
12 Mar 2008CPOL2 min read
A tabbed frame window to show several views of a document in a single frame.
Sample Image - screenshot.png


In a recent project, I wanted to show the document data using different views. I didn't like the default MFC implementation, creating a new frame window for each view.

I headed to Code Project and found several tabbed window articles (Property Sheet View, A Multi Document Tabbed Interface, etc.) but none was exactly what I needed: one frame per document, and one tab per view. With a little help from Google, I found an old article at CodeGuru named "Tabbed Views" by Salvatore Mosaico. I took the original idea, removed the grouping feature, and created this class, adding support for another feature I needed: tool bars and status bars in the child frame.

Using the Code

  • Derive a new class from CTabbedMDIChildWnd. If you do this by hand, remember to define the message map and dynamic creation. You can use the class wizard to do the dirty work for you.
    • Select a new MFC class:

      Image 2

    • Choose CMDIChildWnd as the base class:

      Image 3

    • And replace all occurrences of CMDIChildWnd with our class. Do this both in the *.h and *.cpp files:

      Image 4

    • Don't forget to include the CTabbedMDIChildWnd header in the *.h.
      #include <span class="code-string">"TabbedMDIChildWnd.h"</span>
  • Include the header files of your views:
    #include <span class="code-string">"FirstView.h"</span>
    #include <span class="code-string">"SecondView.h"</span>
  • To tell the frame window which tabs to use, use the helper class CTabbedMDIChildWnd::CTabItem. This class is derived form TCITEM, with the addition of a CRuntimeClass to create the view. You can use the provided constructor as in the next example.
  • Override OnCreateClient. Inside the function, define the tabs, and call the base class:
    BOOL CDemoChildWnd::OnCreateClient(LPCREATESTRUCT lpcs, 
                                       CCreateContext* pContext)
        // this is the heart of the class, each call to DefineView will
        // add a tab of the specified class
        DefineTab(CTabbedMDIChildWnd::CTabItem(_T("First View"),
        DefineTab(CTabbedMDIChildWnd::CTabItem(_T("Second View"),
        return CTabbedMDIChildWnd::OnCreateClient(lpcs,pContext);
  • Your class in now ready. It is a good time to compile the project!
  • The next step is to change the MDI child frame inside the InitInstance function of the application:
    // Register the application's document templates.  Document templates
    //  serve as the connection between documents, frame windows and views
    CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(IDR_TabbedFrameTYPE,
        RUNTIME_CLASS(CDemoChildWnd), // custom MDI child frame

Inside the Class

The class has a CTabCtrl member and creates one tab for each view defined by a call to DefineTab. It handles the TCN_SELCHANGE and TCN_SELCHANGING notify messages to hide and show the views as the tabs are selected.

It uses a std::vector to keep track of the defined tabs using the runtime class of the different views. This implementation imposes a limitation, only one instance of a view class is supported. Calling DefineTab several times using the same runtime class is not a good idea!

If you want to change the appearance of the tab control, you can customize CTabbedMDIChildWnd::OnCreateClient to change the styles of the tab control, or make additional calls to add images, etc.


  • 1.1 Fixed small memory leak (thanks to J.H.Park)
  • 12th March, 2008: Updated downloads


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


About the Author

Ernesto Perales Soto
Web Developer
Mexico Mexico
@Work : Doing UML modelling for food
@Home : Still coding for fun!

Comments and Discussions

GeneralHandly little piece of code -- plus small fix Pin
John Slagel11-Mar-08 9:19
MemberJohn Slagel11-Mar-08 9:19 
GeneralRe: Handly little piece of code -- plus small fix Pin
Ernesto Perales Soto11-Mar-08 12:28
MemberErnesto Perales Soto11-Mar-08 12:28 
QuestionHow to redraw views Pin
Van Binh10-May-06 23:06
MemberVan Binh10-May-06 23:06 
Hi all,

I have a problem when I use this code. Please help me to solve it.

In child frame I create two views and some control bars, these control bars are docked. Now I call some function to dock control bars to other side of child frame. The control bars are docked but views are not redrew.

How can I redraw the views when I change the control bar's position.
Thanks and best regard.

AnswerRe: How to redraw views Pin
Ernesto Perales Soto11-May-06 3:42
MemberErnesto Perales Soto11-May-06 3:42 
GeneralRe: How to redraw views Pin
Van Binh17-May-06 22:51
MemberVan Binh17-May-06 22:51 
AnswerRe: How to redraw views Pin
Ernesto Perales Soto18-May-06 4:23
MemberErnesto Perales Soto18-May-06 4:23 
Generalcan't capture mouse double click events. Pin
Dennis_Hu15-Dec-05 11:31
MemberDennis_Hu15-Dec-05 11:31 
AnswerRe: can't capture mouse double click events. Pin
Ernesto Perales Soto15-Dec-05 12:02
MemberErnesto Perales Soto15-Dec-05 12:02 
GeneralRe: can't capture mouse double click events. Pin
Dennis_Hu16-Dec-05 8:35
MemberDennis_Hu16-Dec-05 8:35 
Generalit makes leaks Pin
sakeani10-Jun-04 22:32
Membersakeani10-Jun-04 22:32 
GeneralAlternate solution Pin
Ernesto Perales Soto12-Jun-04 4:05
MemberErnesto Perales Soto12-Jun-04 4:05 
GeneralRe: Alternate solution Pin
sakeani13-Jun-04 15:51
Membersakeani13-Jun-04 15:51 
GeneralAnd right again! Pin
Ernesto Perales Soto14-Jun-04 19:36
MemberErnesto Perales Soto14-Jun-04 19:36 
GeneralRe: And right again! Pin
Jabes15-Jun-04 1:18
MemberJabes15-Jun-04 1:18 
GeneralI stand corrected! Pin
Ernesto Perales Soto15-Jun-04 4:31
MemberErnesto Perales Soto15-Jun-04 4:31 
GeneralVery cool ;) Pin
Kochise2-May-04 0:08
MemberKochise2-May-04 0:08 
GeneralThanks for sharing your links! Pin
Ernesto Perales Soto2-May-04 4:09
MemberErnesto Perales Soto2-May-04 4:09 
GeneralNo Google skills... Pin
Kochise2-May-04 6:35
MemberKochise2-May-04 6:35 
GeneralRe: No Google skills... Pin
sakeani13-Jun-04 15:48
Membersakeani13-Jun-04 15:48 

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.