Click here to Skip to main content
15,036,441 members
Articles / Desktop Programming / MFC
Article
Posted 6 Oct 2001

Stats

362.9K views
12K downloads
125 bookmarked

CMDITabs

Rate me:
Please Sign up or sign in to vote.
4.88/5 (43 votes)
19 Oct 2001CPOL4 min read
A CTabCtrl control to switch comfortably between MDI views

Image 1

Overview

A feature I always missed in most MDI products (like Microsoft Word or Microsoft FrontPage) was a simple means to see which views/docs are currently open and to switch easily between them. Using the 'Window' menu can be quite cumbersome. I wanted something like Oz Solomonovich's Window Tabs Add-In. In my current project I have to do an MDI application with several different views and I don't want my users to become as frustrated as me about this topic. So I decided to make my own CTabCtrl derivation. As it turns out, it was very easy to do so.

CMDITabs is a little control that adds 'tabs' to an MFC MDI application with which the user can switch between views. The appearance can also be customized to display the view icons. This is useful if you have different types of views, otherwise I would suggest to turn the icons off. The tabs can be placed at the bottom or at the top of the views. The control is smart enough to reflect all changes in the views (open, close, new, change title/icon).

After writing this article I discovered, that there is a similar solution for tabbing between MDI views in the doc/view section of Code Project. First I was a little bit frustrated, thinking I had reinvented the wheel again ;-). But on a closer look I liked my MDITabs better because of three reasons:

  1. it looks nicer and hasn't got a space wasting double border frame
  2. it is more stable due to its simpler implementation ;-) (read the bug list in the comments)
  3. it is smaller, you have only one class to use, not three

So I sent this article int to the Code Project.

How to use

Using CMDITabs in your code is extremely simple. Add MDITabs.h and MDITabs.cpp to your project. In your CMainFrame class add a new member m_wndMDITabs of class CMDITabs (don't forget to include 'MDITabs.h'). Insert m_mdiTabs.Create(this); in CMainFrame::OnCreate() after all toolbars and status bars have been set up created. It is important for proper layout that the MDI tabctrl is created last. To synchronize view operations with the MDI Tabs, it is necessary to override OnUpdateFrameTitle from the base class CMDIFrameWnd of our CMainFrame class. After calling the base class implementation you have to call the Update() function of m_wndMDITabs

// MainFrm.h
class CMainFrame : public CMDIFrameWnd
{
  [...]
  CMDITabs m_wndMDITabs;
  
  virtual void OnUpdateFrameTitle(BOOL bAddToTitle); 
  [...]  
};

// MainFrm.cpp
void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
  CMDIFrameWnd::OnUpdateFrameTitle(bAddToTitle);
  m_wndMDITabs.Update(); // sync the mditabctrl with all views
}

That's it! Build and start your application, open some views and enjoy switching between them ;-)

Other features

  • Double clicking on a tab maximizes the view.
  • Right clicking on a tab displays the view's system menu. You can change the menu by simply changing the views system menu, if you like (see demo project CChildFrame::OnCreate()). If you need to supply a total different (independant) menu, your view can answer the WM_GETTABSYSMENU message.
  • The tabs are hidden when there are less than one (two) view(s) open. Use SetMinViews() or the style MT_HIDEWLT2VIEWS to change this behaviour.
  • In the Create() function you can supply some styles to customize the appearance of the control:
    MT_BOTTOMtabs appear at bottom
    MT_TOPtabs appear at top
    MT_IMAGESuse view icons as images
    MT_HIDEWLT2VIEWShide tabs when there are less than two views (default is one view)
m_wndMDITabs.Create(this, MT_TOP|MT_IMAGES|MT_HIDEWLT2VIEWS);

Internals

Layout

The private MFC message WM_SIZEPARENT provides a way to attach windows to the MDI client area of a doc/view app. CMDITabs implements OnSizeParent to attach itself at the bottom of the MDI client area. If you want another layout you need to change this function. The Z-order of the MDIClient siblings is important for the layout algorithm. Siblings are asked to position themselves in Z-order (search for WM_SIZEPARENT on MSDN to get more info). That's the reason why CMDITabs must be created after all other control and status bars have been done in CMainFrame::OnCreate(). Otherwise the status bar would appear above the tabs, destroying proper layout.

Synchronizing Views and Tabs

The tab control must always reflect the list of views. Instead of monitoring all possible view changing events (close, open, new, changing titles/icons) I hooked into the CMainFrame::OnUpdateFrameTitle() function. I discovered, that this function gets called when something view-related happens. Here you have to call CMDITabs::Update() which in response completely rebuilds its tab list. It does so by querying the child windows of the MDIClient window, circumventing the complex doc/view organization of MFC! The simpler the solution the robuster it works!

License

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

Share

About the Author

Christian Rodemeyer
Software Developer
Germany Germany
I'm developing for fun since 1985, starting with UCSD Pascal on some old machines (no hard disk, but four floppies!), then moving quickly on to assembler on the famous C64 and Amiga. During university I started professional development for Windows/Unix/Linux, using a myriad of languages (Pi, Assembler (6502, 68000, 80386/486), Cobol, Modula2, Prolog, OML, C, C++, C#, Java, Scala, Groovy, Clojure, VB, Eiffel, Delphi, Perl, Pascal, Javascript). Currently my favorite languages are Clojure, Ruby and modern Javascript.

Comments and Discussions

 
GeneralRe: Title sync problem *FIX* Pin
Obliterator11-Dec-02 5:13
MemberObliterator11-Dec-02 5:13 
GeneralRe: Title sync problem *FIX* Pin
HachiihcaH27-Apr-04 10:34
MemberHachiihcaH27-Apr-04 10:34 
GeneralRe: Title sync problem *FIX* Pin
Albert van Peppen28-Apr-04 21:36
professionalAlbert van Peppen28-Apr-04 21:36 
Generalshorter tab control Pin
Anonymous19-Jul-02 6:43
MemberAnonymous19-Jul-02 6:43 
QuestionAmpersand (&) in filenames? Pin
17-Jun-02 12:05
suss17-Jun-02 12:05 
AnswerRe: Ampersand (&) in filenames? Pin
Obliterator16-Aug-02 0:41
MemberObliterator16-Aug-02 0:41 
QuestionHow can I get the Tabs to show inside my CMDIChildWnd's? Pin
rbc15-May-02 18:42
Memberrbc15-May-02 18:42 
AnswerRe: How can I get the Tabs to show inside my CMDIChildWnd's? Pin
Christian Rodemeyer15-May-02 19:54
professionalChristian Rodemeyer15-May-02 19:54 
This is not possible with CMDITabs. CMDITabs manages only views inside the main window.

Christian
---
Always expect the unexpected!
GeneralDebug Assertion Failed Pin
rbc17-Apr-02 18:09
Memberrbc17-Apr-02 18:09 
GeneralRe: Debug Assertion Failed Pin
rbc17-Apr-02 18:25
Memberrbc17-Apr-02 18:25 
GeneralRe: Debug Assertion Failed Pin
rbc18-Apr-02 7:04
Memberrbc18-Apr-02 7:04 
GeneralRe: Debug Assertion Failed Pin
Christian Rodemeyer18-Apr-02 8:05
professionalChristian Rodemeyer18-Apr-02 8:05 
GeneralBoundschecker warning Pin
Warren Stevens13-Feb-02 4:59
MemberWarren Stevens13-Feb-02 4:59 
GeneralFail to add grid control Pin
Kevin Iu17-Nov-01 17:42
MemberKevin Iu17-Nov-01 17:42 
GeneralMDITabs and Print Preview Pin
8-Nov-01 2:07
suss8-Nov-01 2:07 
GeneralRe: MDITabs and Print Preview Pin
Warren Stevens13-Feb-02 4:42
MemberWarren Stevens13-Feb-02 4:42 
GeneralUpdate Pin
Christian Rodemeyer20-Oct-01 9:04
professionalChristian Rodemeyer20-Oct-01 9:04 
Generala suggestion (request) Pin
PJ Arends14-Oct-01 21:55
professionalPJ Arends14-Oct-01 21:55 
GeneralRe: a suggestion (request) Pin
Christian Rodemeyer15-Oct-01 8:23
professionalChristian Rodemeyer15-Oct-01 8:23 
GeneralRe: a suggestion (request) Pin
Eric Jacobsen25-Jan-02 5:20
MemberEric Jacobsen25-Jan-02 5:20 

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.