Click here to Skip to main content
15,881,281 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.6K   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

 
Generalnice work Pin
Biruk Abebe13-Apr-15 8:27
Biruk Abebe13-Apr-15 8:27 
GeneralMy vote of 5 Pin
chaos_xia10-Jul-12 4:07
chaos_xia10-Jul-12 4:07 
GeneralDrag to change order of tabs - *solution* (wohoo) Pin
Gernot Frisch3-Nov-09 5:02
Gernot Frisch3-Nov-09 5:02 
I got it so you can drag a tab to the left / right and change the order of the views.

Open the header, and insert:
class CMDITabs : public CXPTabCtrl{
...
private:
  int        m_iMouseDownItem; // index when mouse goes down
public:
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);


In the implementation add:
CMDITabs::CMDITabs()
{
  m_iMouseDownItem=-1;


also
void CMDITabs::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CXPTabCtrl::OnLButtonDown(nFlags, point);

	TCHITTESTINFO tcHit;
	tcHit.pt = point;
	int selected = HitTest(&tcHit);
	int current = GetCurSel();
    // *** NEW ***
    m_iMouseDownItem = selected;
    SetCapture();
    // *** /NEW ***


Then add the functions:
  ON_WM_MOUSEMOVE()
  ON_WM_LBUTTONUP()

void CMDITabs::OnLButtonUp(UINT nFlags, CPoint point)
{
	CTabCtrl::OnLButtonUp(nFlags, point);
	m_iMouseDownItem = -1;
	ReleaseCapture();
}

void CMDITabs::OnMouseMove(UINT nFlags, CPoint point)
{
	CTabCtrl::OnMouseMove(nFlags, point);
	TCHITTESTINFO tcHit;
	tcHit.pt = point;

	if(m_iMouseDownItem >=0)
	{
		int selected = HitTest(&tcHit);
		if(selected>=0 && selected != m_iMouseDownItem)
		{
			TCITEM itema;
			char texta[256];
			itema.cchTextMax = 255;
			itema.pszText = texta;
		    itema.mask = TCIF_TEXT|TCIF_PARAM|TCIF_IMAGE|TCIF_STATE;
			TCITEM itemb;
			char textb[256];
			itemb.cchTextMax = 255;
			itemb.pszText = textb;
		    itemb.mask = TCIF_TEXT|TCIF_PARAM|TCIF_IMAGE|TCIF_STATE;

			BOOL ok = GetItem(selected, &itema);
			GetItem(m_iMouseDownItem, &itemb);
			
			
			ok = SetItem(m_iMouseDownItem, &itema);
			SetItem(selected, &itemb);
			m_iMouseDownItem = selected;
			
			this->SetCurSel(selected);
		}
	}
}

GeneralRe: Drag to change order of tabs - *solution* (wohoo) Pin
Jason Tian23-Jul-11 22:12
Jason Tian23-Jul-11 22:12 
QuestionHow to use in SDI application ? Pin
Gobsek7-Jul-09 23:47
Gobsek7-Jul-09 23:47 
Generalsimply to use indeed Pin
dickens200821-Nov-08 19:50
dickens200821-Nov-08 19:50 
GeneralExtending up and down Pin
Vaclav_26-Sep-07 4:57
Vaclav_26-Sep-07 4:57 
QuestionWM_SIZEPARENT Pin
sharp_k28-Aug-07 3:10
sharp_k28-Aug-07 3:10 
QuestionWork in Visual Studio like interface Pin
9353201823-May-07 16:00
9353201823-May-07 16:00 
Generalcontrol the child window Pin
coronys4-Jan-07 7:50
coronys4-Jan-07 7:50 
GeneralNICE Pin
ajs218-May-06 15:34
ajs218-May-06 15:34 
GeneralVS. NET 2005 - MFC 8.0 LNK 2019 Pin
BrownJacket26-Dec-05 8:24
BrownJacket26-Dec-05 8:24 
GeneralRe: VS. NET 2005 - MFC 8.0 LNK 2019 Pin
zhangyuan_cau3-Feb-06 17:02
zhangyuan_cau3-Feb-06 17:02 
QuestionHow to create more than one object of forma? Pin
zhangwm18-Sep-05 23:48
zhangwm18-Sep-05 23:48 
GeneralFlorin Octavian Ochiana's Code Pin
Dr.Funk14-Sep-05 19:03
Dr.Funk14-Sep-05 19:03 
GeneralTab text Color Pin
Fad B16-Jul-05 20:42
Fad B16-Jul-05 20:42 
QuestionHow to hide this control directly, i.e. form main menu, for example (sorry for bad English)? Pin
Bazzam13-May-05 6:23
Bazzam13-May-05 6:23 
GeneralXP Style Pin
ehh22-Mar-05 8:35
ehh22-Mar-05 8:35 
GeneralRe: XP Style Pin
Robo Jon15-Aug-05 7:26
Robo Jon15-Aug-05 7:26 
QuestionIcon ??? Pin
Master^Tristar16-Jan-05 0:56
Master^Tristar16-Jan-05 0:56 
GeneralPlease correct text Pin
Alexander Shevchenko31-Oct-04 9:46
Alexander Shevchenko31-Oct-04 9:46 
GeneralOnSizeParent - problem + solution Pin
adrian_conlon25-Feb-04 2:02
adrian_conlon25-Feb-04 2:02 
GeneralRe: OnSizeParent - problem + solution Pin
alex.barylski27-Apr-04 16:29
alex.barylski27-Apr-04 16:29 
GeneralRe: OnSizeParent - problem + solution Pin
adrian_conlon28-Apr-04 5:50
adrian_conlon28-Apr-04 5:50 
GeneralMDI Tabs Code Licence Pin
narm gadiraju8-Nov-03 17:58
narm gadiraju8-Nov-03 17:58 

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.