Click here to Skip to main content
Click here to Skip to main content

CMDITabs

, 19 Oct 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
A CTabCtrl control to switch comfortably between MDI views

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 Wink | ;-) . 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 Wink | ;-) (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 Wink | ;-)

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_BOTTOM tabs appear at bottom
    MT_TOP tabs appear at top
    MT_IMAGES use view icons as images
    MT_HIDEWLT2VIEWS hide 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
Architect Kabel Deutschland
Germany Germany
I'm developing for fun since 1985, starting with UCSD Pascal on some old machines (no harddisk, 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, 386/486, Cobol, Modula2, OML, C, C++, VB, Prolog, Eiffel, Delphi, Perl, Pascal, Assembler). Currently my favorite languages are C# 3.0 and Python.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberchaos_xia10-Jul-12 5:07 
GeneralDrag to change order of tabs - *solution* (wohoo) PinmemberGernot Frisch3-Nov-09 6:02 
GeneralRe: Drag to change order of tabs - *solution* (wohoo) PinmemberJason Tian23-Jul-11 23:12 
QuestionHow to use in SDI application ? PinmemberGobsek8-Jul-09 0:47 
Generalsimply to use indeed Pinmemberdickens200821-Nov-08 20:50 
GeneralExtending up and down PinmemberVaclav_Sal26-Sep-07 5:57 
QuestionWM_SIZEPARENT PinmemberMark_VC28-Aug-07 4:10 
QuestionWork in Visual Studio like interface Pinmember9353201823-May-07 17:00 
Generalcontrol the child window Pinmembercoronys4-Jan-07 8:50 
GeneralNICE Pinmemberajs218-May-06 16:34 
GeneralVS. NET 2005 - MFC 8.0 LNK 2019 PinmemberBrownJacket26-Dec-05 9:24 
GeneralRe: VS. NET 2005 - MFC 8.0 LNK 2019 Pinmemberzhangyuan_cau3-Feb-06 18:02 
QuestionHow to create more than one object of forma? Pinmemberzhangwm19-Sep-05 0:48 
GeneralFlorin Octavian Ochiana's Code PinmemberDr.Funk14-Sep-05 20:03 
GeneralTab text Color PinmemberFad B16-Jul-05 21:42 
QuestionHow to hide this control directly, i.e. form main menu, for example (sorry for bad English)? PinmemberBazzam13-May-05 7:23 
GeneralXP Style Pinmemberehh22-Mar-05 9:35 
GeneralRe: XP Style PinmemberJonathan Attias15-Aug-05 8:26 
QuestionIcon ??? PinmemberDJ Mantra16-Jan-05 1:56 
GeneralPlease correct text PinmemberAlexander Shevchenko31-Oct-04 10:46 
GeneralOnSizeParent - problem + solution Pinmemberadrian_conlon25-Feb-04 3:02 
GeneralRe: OnSizeParent - problem + solution PinmemberHockey27-Apr-04 17:29 
GeneralRe: OnSizeParent - problem + solution Pinmemberadrian_conlon28-Apr-04 6:50 
GeneralMDI Tabs Code Licence Pinsussnarm8-Nov-03 18:58 
GeneralRe: MDI Tabs Code Licence PinmemberChristian Rodemeyer9-Nov-03 8:14 
GeneralRemove tab title order number Pinmemberzhj9-Oct-03 7:43 
Questionhow to specify form type not select from box when create new tab? Pinmemberyangguoqiang17-Sep-03 19:40 
GeneralXP Drawing problem PinmemberObliterator16-Sep-03 4:33 
GeneralDocument and its views tabs needed PinmemberVaclav16-Aug-03 16:54 
GeneralFlick Question Pinmemberstorein9-Jun-03 1:17 
GeneralRe: Flick Question Pinmemberselecta13-Jun-03 15:53 
GeneralRe: Flick Question Pinmemberstorein16-Jun-03 15:56 
GeneralProblem when using TCS_BUTTON style... Pinmemberselecta28-May-03 12:25 
GeneralRe: Problem when using TCS_BUTTON style... Pinmemberselecta28-May-03 12:51 
Generalwhen select change tab how to transfer the function of view Pinmembermaohj168816-Mar-03 19:21 
GeneralWindow Text as tab text Pinmemberajl8793-Feb-03 11:40 
GeneralSaving and Loading Muliple Views Pinmemberajl87917-Jan-03 4:31 
GeneralOnUpdateFrameTitle() PinmemberKannan Ramanathan17-Nov-02 21:26 
GeneralIs there any way not to let to close the MDItab by pressing Ctrl+F4 PinsussPL22-Oct-02 19:38 
GeneralFlicking problem PinsussAnonymous16-Oct-02 22:58 
GeneralRe: Flicking problem Pinsusskimdaejeong28-Apr-03 9:14 
GeneralRe: Flicking problem Pinmembergu mingqiu9-Mar-04 18:34 
GeneralTitle sync problem PinmemberObliterator14-Aug-02 10:44 
GeneralRe: Title sync problem PinsussAnonymous15-Aug-02 11:12 
GeneralRe: Title sync problem PinmemberObliterator16-Aug-02 1:38 
GeneralRe: Title sync problem *FIX* PinmemberAlbert van Peppen11-Dec-02 3:32 
GeneralRe: Title sync problem *FIX* PinmemberObliterator11-Dec-02 5:15 
GeneralRe: Title sync problem *FIX* PinmemberAlbert van Peppen11-Dec-02 5:26 
GeneralRe: Title sync problem *FIX* PinmemberObliterator11-Dec-02 6:13 
GeneralRe: Title sync problem *FIX* PinmemberHachiihcaH27-Apr-04 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Oct 2001
Article Copyright 2001 by Christian Rodemeyer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid