Click here to Skip to main content
13,867,908 members
Click here to Skip to main content
Add your own
alternative version


82 bookmarked
Posted 31 Jan 2000
Licenced Public Domain

Automatic Tab Bar for MDI Frameworks

, 3 Jan 2003
Rate this:
Please Sign up or sign in to vote.
A dockable bar containing a tabbed list of open windows

Sample Image - mditab.gif

<!-- Article Starts -->

1. Introduction

This code implements MDI tab view for easy navigation. The views are supported on a control bar, which can be floated (of course docked!).

2. Implemented Features

  1. Control bar-based owner-drawn tab view. Supports fonts and color settings, the default is as follows
    • The normal/inactive tab text is painted black,
    • The active tab text is painted blue,
    • The tab text of a modified document is painted red.
  2. The control bar can be docked (currently only top and bottom) or floated.
  3. Custom MDI window list dialogs, similar to VC++.
  4. Full screen modes.
  5. Cool menu popup, three types.
    • From a right click on the tab control itself, as shown above.
    • From the right click on the tab control bar.
    • From a right click in the MDI main window client area, just a place holder (demo) build yours.
  6. Display of company logo text in the MDI main window client area.
  7. MDI client background text logo and banner painting.
  8. Saves and restores the state of the MDI child window, i.e. normal or maximize, and the position of the control bars and the main window frame.
  9. Tool tips and tab icons (icons!...just a demo-do not know what will be better, let me know your views).
  10. Minimum modifications to existing projects, say automated! Few changes to only existing main frame class and maybe application class, no base class to derive from.

3. Unicode?

There is no reason why it should not work!

4. Files required:

  1. WindowManager.cpp/h: Manages the window list, and subclass the MDI client to create the tab view bar. It also contain the class CDocumentList, which lists all open documents. This is really where life begins...
    NOTE: The CDocumentList class can be very useful for many applications, take a good look.
  2. ViewManager.cpp/h: Manages the views of the application, creating the view tabs.
  3. WindowTabCtrl.cpp/h: Codes implementing the tab control.
  4. PopupMenu.cpp/h: Codes implementing the popup menu.


  5. tabview.rc: The full screen toolbar, popup menus and window list dialog resource.
  6. tabview.h: Contains the resource ids needed by the controls. (NOTE: Not used directly, deleted later).
  7. tabview.bmp: The full screen and popup menus bitmap.

5. How to use it?

There is a bit of work involved in integrating the resource file into your project. Lets do it, the difficult part first...
  1. Move the bitmap file, tabview.bmp to your res directory, and tabview.rc to the project directory.
  2. Add the resource file tabview.rc to the project's *.rc2 file, and merge the resource id file tabview.h with your resource file, resource.h. If you have not being manually modifying resource files then read this...
    • Identify the last resource type (numbering starts from 128) in your resource.h file, copy and paste the resource type identifiers (2) from the tabview.h, give each an incremental id and finally increment the VC++ object _APS_NEXT_RESOURCE_VALUE by 2, i.e. 1 more than the last incremental id.
    • Similarly, copy and paste the dialog control ids (8) (numbering starts from 1000) and increment the _APS_NEXT_CONTROL_VALUE by 8.
    • Finally, copy and paste the menu items ids (9) ( numbering starts from 32771) and increment the _APS_NEXT_COMMAND_VALUE by 9. The project should now compile without any problem, and you can now delete the tabview.h file.
  3. Now, move the files WindowManager.cpp/h, ViewManager.cpp/h, WindowTabCtrl.cpp/h and PopupMenu.cpp/h to the project directory and add the implementation files to the project.
  4. Open the WindowManager.h file, and in the Forward Declaration part change the CMainFrame to your main window frame class name (see TODO). Also include the header file of your main frame class in the WindowManager.cpp.
  5. Include header files, WindowManager.h and ViewManager.h in your main frame header file and declare in a public section the following:
    CMDIClient   m_MDIClient;
    void GetControlBarsEx(CArray<CControlBar*, CControlBar*>& arrBars);
    • m_MDIClient, is an instance of the window manager, where all events are coordinated.
    • GetControlBarsEx, a new method to help the full screen mode. Provide an implementation of this method similar to this: (add all controls bars you wish to manage in full screen to the array parameter)
      void CMainFrame::GetControlBarsEx(CArray<CControlBar*, 
                                        CControlBar*>& arrBars)
          if (::IsWindow(m_wndToolBar.m_hWnd))
          if (::IsWindow(m_wndStatusBar))
          //...more here
  6. Towards the end of the OnCreate() function of the main frame add the ff:
    The SubclassMDIClient() is prototyped as
    BOOL SubclassMDIClient(CMDIFrameWnd* pMDIFrameWnd,
        CViewManager* pViewManager, UINT uID = ID_VIEW_VIEWTAB);
    • pMDIFrameWnd, is a pointer to the application's main frame.
    • pViewManager, a pointer to an instance of the viewmanager. The viewmanager, control bar and tab control, is created by the window manager.
    • uID, an id of the control bar. With the defaulted value, hiding and showing of the control bar is already implemented. If, however, you decide to use a different id-say ID_THE_NEWVALUE, you can easily implement the hiding and showing of the tab control bar by inserting the following in your main frame message map. No further code is required. This is what is done to the default status bar and toolbar created by the AppWizard...
  7. In order for command messages to be handled directly in the CWindowManager, use the ClassWizard to override the OnCmdMsg() method of the main frame and modify it to be similar to the ff:
    // This function routes commands to window manager, then to rest of system.
    BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode,
            void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
        // Without this, the window manager menu commands will be disabled,
        // this is because without routing the command to the window manager,
        // MFC thinks there is no handler for it.
        if (m_MDIClient.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
            return TRUE;
        return CMDIFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  8. Tired eh! Just compile and have fun...
  9. Well, you will need this the menus, all the menu ids are already defined so just select them from the ID combo box of the property sheet.
    • On the View menu build the ff:

      ID_VIEW_VIEWTAB Op&en File Tabs
      ID_VIEW_FULLSCREEN F&ull Screen

    • On the Window menu build the ff:
      ID_WINDOW_NEXT Ne&xt Window
      ID_WINDOW_PREVIOUS Pre&vious Window
      ID_WINDOW_CLOSE_ALL C&lose All
      ID_WINDOW_SAVE_ALL &Save All
    • Note: The Windows... menu is build for you automatically.
  10. Well, well, well...if you need to support the position and control bar restoration then do this...(unfortunately, neither the main frame class destructor nor the window manager class destructor is called by the MFC framework!)
    • Add message handle for the WM_CLOSE message for your main frame, or modify the existing one adding the following single line, calling the SaveMainFrameState() method...
      void CMainFrame::OnClose()
    • Finally! replace the main frame displaying code at the end of the InitInstance() of application class with the RestoreMainFrameState() as
      BOOL CDemoApp::InitInstance()
          // The main window has been initialized, so show and update it.
      //    pMainFrame->ShowWindow(m_nCmdShow); ///// <--- we do not need this one!
          return TRUE;

6. Code Snippets

The icon support in the tab is application specific. What I mean is you will need to build a more suitable solution for your application. If, however, you have some ideas as how to implement something general let me know.

For the current implementation...
The OnCreate() function of the CViewManager, which created both the tab and itself (the tab bar), simply creates a place holder icon to fill an image list, which is then attached to the tab.

int CViewManager::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CControlBar::OnCreate(lpCreateStruct) == -1)
        return -1;

    m_ViewTabImages.Create(16, 16, ILC_MASK, 5, 5);

        CRect(0, 0, 0, 0), this, ID_VIEWTAB);

    // Build the image list here
    HICON hIcon = AfxGetApp()->LoadStandardIcon(IDI_APPLICATION);
//    HICON hIcon = AfxGetApp()->LoadIcon(IDR_DEMOTYPE);
    // Enable tooltips for all controls

    return 0;
LoadStandardIcon() is used to load system icon in there. You may wish to replace this with the commented code, IDR_DEMOTYPE is your application specific resource type.
HICON hIcon = AfxGetApp()->LoadIcon(IDR_DEMOTYPE);
In the AddView() function of the same class, the tab image index is set to 0 (zero) the only image in the image list. Finally, in the DrawItem() function of the tab, CWindowTabCtrl, the dummy icon is replaced by the small icon attached to the frame of your child window, parent of the view.
void CWindowTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

    CView* pView    = reinterpret_cast<CView*>(tci.lParam);
    CDocument* pDoc = pView->GetDocument();

    // Draw image
    if (m_bDisplayIcons)
        CImageList* pImageList = GetImageList();
        CMDIChildWnd* pViewFrame = static_cast<CMDIChildWnd*>(pView->GetParent());
        HICON hIcon = reinterpret_cast<HICON>(GetClassLong(pViewFrame->m_hWnd, 
        pImageList->Replace(nTabIndex, hIcon);
Initially, I considered getting the icon from the Windows shell, based on the registered file extension SHGetFileInfo() API. However, it does not look nice for the view frame icon to be different from the tab view icon. By the current implementation, all is needed is a good citizenship like the VC++ itself. Let your application child window system icons reflect the file type and there will be no need to write extra codes.

7. Credits

The code is based on code and ideas shared by the following:
  • Iuri Apollonio, he wrote the base codes using status bar. What is his new email address?
  • Ivan Zhakov, his "MDI Windows Manager dialog" is better than Iuri's. The Window manager class is now the main engine driving the view manager.
  • Chris Maunder, his owner-drawn tab control code snippets are used to improve Iuri's.
  • Adolf Szabo, his Full-screen mode idea is simpler than that implemented by MS and MS's Mike B.
  • YOU, and many others...
Use this code in any project, there is no restriction! Write whatever you like or do not like about this code in the comment section, I will take note of all.
Happy coding...

Paul Selormey, Japan.

8. To Do

  1. Ability to dock on all sides of the main frame, involves more work since the tab control is owner-drawn.
  2. Improved main frame window position saving and restoration...
    • Support for multiple monitors-I do not have the OS (Win98/Win2000) to test this now (if I do, not the video card and monitors!)
    • Support for screen resolution changes. I have API code for my C/SDK application, well tested on Win95 using the QuickRes program. I do not use QuickRes currently, so a bit reluctant to implement it.
  3. Your wishes...

9. Known Issues

  1. The popup menu does not currently support accelerators (but is this needed?).
  2. The modified flag is not re-drawn immediately, I do not wish to play any game to introduce flickers! (implementation still good enough!).
  3. Add yours...

10. In this Update

  1. Many parts are rewritten to address most of the issues in the comment section.
  2. The "tab view" is now CMDIChildWnd class, so splitters and others should work.
  3. Many bug fixes.
  4. Should work with existing codes without little modifications, see Step 5.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Paul Selormey
Japan Japan
Systems Engineer

You may also be interested in...

Comments and Discussions

GeneralFlicker Removal for OnMDIActivate Pin
Japanime26-Mar-07 2:51
memberJapanime26-Mar-07 2:51 
GeneralDocking right of a toolbar Pin
Dieter Hammer31-Jan-07 23:10
memberDieter Hammer31-Jan-07 23:10 
GeneralMemory leak in constructor Pin
totof21-Oct-05 1:31
membertotof21-Oct-05 1:31 
QuestionHow can I use flat buttons, without 3D look for selected items Pin
avnerw9-Aug-04 7:56
memberavnerw9-Aug-04 7:56 
GeneralPositioning the controlbar Pin
*Dreamz7-Apr-04 1:50
member*Dreamz7-Apr-04 1:50 
GeneralRe: Positioning the controlbar Pin
kolu narayan,3-Sep-04 8:44
susskolu narayan,3-Sep-04 8:44 
Generalproblem to make full-screen Pin
Member 2259828-Mar-04 21:49
memberMember 2259828-Mar-04 21:49 
GeneralRe: problem to make full-screen Pin
Paul Selormey28-Mar-04 22:00
memberPaul Selormey28-Mar-04 22:00 
GeneralDemo does not compile Pin
shubharamani23-Sep-03 13:49
membershubharamani23-Sep-03 13:49 
GeneralRe: Demo does not compile Pin
Trevor Larkum10-Feb-04 2:17
memberTrevor Larkum10-Feb-04 2:17 
GeneralThank! Pin
conquestjy8-Jan-03 19:43
memberconquestjy8-Jan-03 19:43 
GeneralRe: Thank! Pin
vikas amin22-Sep-05 0:31
membervikas amin22-Sep-05 0:31 
GeneralWant buttons Pin
Stefan Dahlin5-Jan-03 2:29
memberStefan Dahlin5-Jan-03 2:29 
GeneralRe: Want buttons Pin
Paul Selormey5-Jan-03 3:27
memberPaul Selormey5-Jan-03 3:27 
Generala bug in the CTabCtrl Pin
benben24-Dec-02 3:35
memberbenben24-Dec-02 3:35 
GeneralRe: a bug in the CTabCtrl Pin
Paul Selormey5-Jan-03 3:34
memberPaul Selormey5-Jan-03 3:34 
GeneralRe: a bug in the CTabCtrl Pin
Ali Zamurad12-Apr-04 10:42
memberAli Zamurad12-Apr-04 10:42 
AnswerRe: a bug in the CTabCtrl Pin
code_discuss22-Apr-08 19:36
membercode_discuss22-Apr-08 19:36 
GeneralBUG in article source (edited) Pin
Roger Allen12-Dec-02 6:58
memberRoger Allen12-Dec-02 6:58 
GeneralRe: BUG in article source (edited) Pin
Paul Selormey12-Dec-02 14:18
memberPaul Selormey12-Dec-02 14:18 
GeneralChanging views problem Pin
Stefan Dahlin6-Dec-02 6:16
memberStefan Dahlin6-Dec-02 6:16 
GeneralRe: Changing views problem Pin
Paul Selormey8-Dec-02 2:29
memberPaul Selormey8-Dec-02 2:29 
GeneralNot working for everyone. Pin
Rainer Koschnick12-Sep-02 13:04
memberRainer Koschnick12-Sep-02 13:04 
GeneralRe: Not working for everyone. Pin
Paul Selormey12-Sep-02 16:12
memberPaul Selormey12-Sep-02 16:12 
GeneralRe: Not working for everyone. Pin
Rainer Koschnick12-Sep-02 22:17
memberRainer Koschnick12-Sep-02 22:17 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 4 Jan 2003
Article Copyright 2000 by Paul Selormey
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid