Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / MFC
Article

CMDITabs

Rate me:
Please Sign up or sign in to vote.
4.88/5 (43 votes)
19 Oct 2001CPOL4 min read 382.9K   12.3K   125   69
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)


Written By
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

 
Generalwhen select change tab how to transfer the function of view Pin
maohj168816-Mar-03 18:21
maohj168816-Mar-03 18:21 
GeneralWindow Text as tab text Pin
ajl8793-Feb-03 10:40
ajl8793-Feb-03 10:40 
GeneralSaving and Loading Muliple Views Pin
ajl87917-Jan-03 3:31
ajl87917-Jan-03 3:31 
GeneralOnUpdateFrameTitle() Pin
Kannan Ramanathan17-Nov-02 20:26
Kannan Ramanathan17-Nov-02 20:26 
GeneralIs there any way not to let to close the MDItab by pressing Ctrl+F4 Pin
PL22-Oct-02 18:38
PL22-Oct-02 18:38 
GeneralFlicking problem Pin
Anonymous16-Oct-02 21:58
Anonymous16-Oct-02 21:58 
GeneralRe: Flicking problem Pin
kimdaejeong28-Apr-03 8:14
kimdaejeong28-Apr-03 8:14 
GeneralRe: Flicking problem Pin
gu mingqiu9-Mar-04 17:34
gu mingqiu9-Mar-04 17:34 
If I don't misunderstand your meaning, I was troubled with the same problem. You may have a try to remove the WS_OVERLAPPED style of your CMDIChildFrame derived class by adding the following code in the function PreCreateWindow():
<br />
cs.style = WS_CHILD | WS_VISIBLE// | WS_OVERLAPPED | WS_CAPTION <br />
	| FWS_ADDTOTITLE| WS_MINIMIZEBOX | WS_MAXIMIZEBOX |WS_MAXIMIZE;<br />

GeneralTitle sync problem Pin
Obliterator14-Aug-02 9:44
Obliterator14-Aug-02 9:44 
GeneralRe: Title sync problem Pin
Anonymous15-Aug-02 10:12
Anonymous15-Aug-02 10:12 
GeneralRe: Title sync problem Pin
Obliterator16-Aug-02 0:38
Obliterator16-Aug-02 0:38 
GeneralRe: Title sync problem *FIX* Pin
Albert van Peppen11-Dec-02 2:32
professionalAlbert van Peppen11-Dec-02 2:32 
GeneralRe: Title sync problem *FIX* Pin
Obliterator11-Dec-02 4:15
Obliterator11-Dec-02 4:15 
GeneralRe: Title sync problem *FIX* Pin
Albert van Peppen11-Dec-02 4:26
professionalAlbert van Peppen11-Dec-02 4:26 
GeneralRe: Title sync problem *FIX* Pin
Obliterator11-Dec-02 5:13
Obliterator11-Dec-02 5:13 
GeneralRe: Title sync problem *FIX* Pin
HachiihcaH27-Apr-04 10:34
HachiihcaH27-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
Anonymous19-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
Obliterator16-Aug-02 0:41 
QuestionHow can I get the Tabs to show inside my CMDIChildWnd's? Pin
rbc15-May-02 18:42
rbc15-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 
GeneralDebug Assertion Failed Pin
rbc17-Apr-02 18:09
rbc17-Apr-02 18:09 
GeneralRe: Debug Assertion Failed Pin
rbc17-Apr-02 18:25
rbc17-Apr-02 18:25 
GeneralRe: Debug Assertion Failed Pin
rbc18-Apr-02 7:04
rbc18-Apr-02 7:04 

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.