5,665,355 members and growing! (15,641 online)
Email Password   helpLost your password?
Desktop Development » Document / View » General     Intermediate License: The Code Project Open License (CPOL)

A Tabbed MDI Frame Window

By Ernesto Perales Soto

A tabbed frame window to show several views of a document in a single frame.
VC7, C++Windows, WinXP, MFC, VS.NET2002, Visual Studio, Dev

Posted: 30 Apr 2004
Updated: 12 Mar 2008
Views: 57,146
Bookmarked: 24 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.05 Rating: 3.05 out of 5
0 votes, 0.0%
1
2 votes, 20.0%
2
5 votes, 50.0%
3
2 votes, 20.0%
4
1 vote, 10.0%
5
Sample Image - screenshot.png

Introduction

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:

    • Choose CMDIChildWnd as the base class:

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

    • Don't forget to include the CTabbedMDIChildWnd header in the *.h.
      #include "TabbedMDIChildWnd.h"
  • Include the header files of your views:
    #include "FirstView.h"
    #include "SecondView.h"
  • 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"),
                                       RUNTIME_CLASS(CFirstView)));
        DefineTab(CTabbedMDIChildWnd::CTabItem(_T("Second View"),
                                      RUNTIME_CLASS(CSecondView)));
        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(CTabbedFrameDoc),
            RUNTIME_CLASS(CDemoChildWnd), // custom MDI child frame
            RUNTIME_CLASS(CTabbedFrameView));
        AddDocTemplate(pDocTemplate);

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.

History

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

License

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


@Work : Doing UML modelling for food
@Home : Still coding for fun!
Occupation: Web Developer
Location: Mexico Mexico

Other popular Document / View articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralHandly little piece of code -- plus small fixmemberJohn Slagel10:19 11 Mar '08  
GeneralRe: Handly little piece of code -- plus small fixmemberErnesto Perales Soto13:28 11 Mar '08  
GeneralHow to redraw viewsmemberVan Binh0:06 11 May '06  
AnswerRe: How to redraw viewsmemberErnesto Perales Soto4:42 11 May '06  
GeneralRe: How to redraw viewsmemberVan Binh23:51 17 May '06  
AnswerRe: How to redraw viewsmemberErnesto Perales Soto5:23 18 May '06  
Generalcan't capture mouse double click events.memberDennis_Hu12:31 15 Dec '05  
AnswerRe: can't capture mouse double click events.memberErnesto Perales Soto13:02 15 Dec '05  
GeneralRe: can't capture mouse double click events.memberDennis_Hu9:35 16 Dec '05  
Generalit makes leaksmembersakeani23:32 10 Jun '04  
GeneralAlternate solutionmemberErnesto Perales Soto5:05 12 Jun '04  
GeneralRe: Alternate solutionmembersakeani16:51 13 Jun '04  
GeneralAnd right again!memberErnesto Perales Soto20:36 14 Jun '04  
GeneralRe: And right again!memberJabes2:18 15 Jun '04  
GeneralI stand corrected!memberErnesto Perales Soto5:31 15 Jun '04  
GeneralVery cool ;)memberKochise1:08 2 May '04  
GeneralThanks for sharing your links!memberErnesto Perales Soto5:09 2 May '04  
GeneralNo Google skills...memberKochise7:35 2 May '04  
GeneralRe: No Google skills...membersakeani16:48 13 Jun '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Mar 2008
Editor: Deeksha Shenoy
Copyright 2004 by Ernesto Perales Soto
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project