• Download demo - 195.81 KB
  • Download source - 81 KB
Sample Image

Introduction

This control is based on CWnd class and can be placed as a child window anywhere, for example in the client area of the frame or dialog. Tabs can be top or bottom of child windows. The user can drag tabs using the mouse. Control has a zoom (shrink of tabs) and scrolling tabs mode. Also, if there is one tab the area of tabs can be hidden.

Using the Code

Child windows are added using their HWND and they can be of any type, for example modeless dialogs. TabCtrlconsists of three areas: control area, tabs area, windows area.

Sample Image

To create the control and add elements to it, you can do the next steps:

  TabCtrlEx<tabctrlstyle_vs2003_client /> m_TabCtrl;  CListCtrl m_List1, m_List2, m_List3;    ...  ...    // Creation and initialization of TabCtrl.  if(m_TabCtrl.Create(this,WS_CHILD | WS_VISIBLE,  CRect(0,0,0,0),100/*id of TabCtrl*/)==false) return -1;  m_TabCtrl.SetBehavior(TAB_BEHAVIOR_SCROLL);    CImageList imagelist, imagelistSys;  CBitmap bmp, bmpSys;    imagelist.Create(16,16,ILC_COLOR24 | ILC_MASK,3,0);  bmp.LoadBitmap(IDB_BITMAP1);  imagelist.Add(&bmp,RGB(255,0,255));    imagelistSys.Create(14,14,ILC_COLOR24 | ILC_MASK,7,0);  bmpSys.LoadBitmap(IDB_BITMAP2);  imagelistSys.Add(&bmpSys,RGB(255,0,255));    m_TabCtrl.SetImageLists(&imagelist,NULL);  m_TabCtrl.SetSystemImageList(&imagelistSys);    // Creation of child windows.  if(m_List1.Create(WS_CHILD | WS_CLIPCHILDREN | LVS_SHOWSELALWAYS |   LVS_REPORT,CRect(0,0,0,0),&m_TabCtrl,2001)==0 ||     m_List2.Create(WS_CHILD | WS_CLIPCHILDREN | LVS_SHOWSELALWAYS |      LVS_REPORT,CRect(0,0,0,0),&m_TabCtrl,2002)==0 ||     m_List3.Create(WS_CHILD | WS_CLIPCHILDREN | LVS_SHOWSELALWAYS |      LVS_REPORT,CRect(0,0,0,0),&m_TabCtrl,2003)==0)     return -1;       // Initialization of child windows.  m_List1.InsertColumn(0,_T("Mail"),LVCFMT_LEFT,100);  m_List1.InsertItem(0,_T("Mail 1"));  m_List2.InsertColumn(0,_T("Calendar"),LVCFMT_LEFT,100);  m_List2.InsertItem(0,_T("Calendar 1"));  m_List3.InsertColumn(0,_T("Contacts"),LVCFMT_LEFT,100);  m_List3.InsertItem(0,_T("Contact 1"));    // Attaching of child windows to the TabCtrl.  if(m_TabCtrl.Add(m_List1,_T("1.Mail"),0)==NULL ||     m_TabCtrl.Add(m_List2,_T("2.Calendar"),1)==NULL ||     m_TabCtrl.Add(m_List3,_T("3.Contacts"),2)==NULL)     return -1;       // Load state from registry and update.  m_TabCtrl.LoadState(AfxGetApp(),_T("TabCtrl"),_T("State"));  m_TabCtrl.Update();

Class TabCtrl not perform any rendering. For its drawing, it calls the functions of TabCtrlDraw interface. In general, to set the style TabCtrl you need to create an object inherited from interface ITabCtrlStyle and pass a pointer to it, using the function InstallStyle. This interface should provide a pointer to TabCtrlDraw for drawing,ITabCtrlRecalc to specify the size and spacing of their regions, ITabCtrlBehavior to correct the behavior of the control and ITabCtrlToolTip to define custom tooltip for the tabs and buttons. Object of class of style must exist during work of control. To do this, you can create an intermediate class like TabCtrlComplex. If you are working with only one style, then use the template class TabCtrlEx. The class name of the style is defined as a template parameter, for example:

TabCtrlEx<TabCtrlStyle_VS2003_client> m_TabCtrl;

Some styles have already been created. For example, styles similar to the tabs of working files and docking/floating panels in Visual Studio 2003, 2008 and 2010. To create your own styles, see classesTabCtrlStyle_VS2003_clientTabCtrlStyle_VS2003_barsTabCtrlStyle_VS2008_client_classic, etc. Classes TabCtrlRecalcStub and TabCtrlBehaviorStub create a default implementation for the functions ofITabCtrlRecalc and ITabCtrlBehavior interfaces respectively. You can use them to create your own style objects.

Control requires a call of Update(true) after you add or delete tabs, as well as change its properties and state.

If the text of the tab is not fully visible and allowed tooltips (ToolTipEnable) and the mouse cursor is over the tab, then tooltip appears with its text. But if the function SetTabTooltipText was called, then the specified text is displayed always.

Control does not send messages to the parent window and uses an interface TabCtrlNotify for notification of the events. Use SetNotifyManager to set the pointer to your implementation of TabCtrlNotify.

By default, all drawing is based on double buffering, it excludes any blinking. If you want, useVirtualWindow::DoubleBuffering(false) to disable double buffering.

Good luck.

History

  • 28th May, 2010: Initial version
  • 10th June, 2010: Added redirect WM_NOTIFY message from child windows to a parent of the TabCtrl control
  • 12th June, 2010: Corrected error display of tooltips.