Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

Ownerdraw Tab Controls - Borders and All

Rate me:
Please Sign up or sign in to vote.
4.90/5 (44 votes)
30 Jun 2002CPOL2 min read 430.3K   17.3K   103   55
A framework for overriding all aspects of a tab control's apprearance, including the borders, the background and of course the tabs themselves.

Sample Image - TabControl.jpg

Introduction

Tab controls when used well are a very valuable ui tool.

However, I have more recently felt that the original (and current) look of the standard tab control was too heavy: instead of just looking at the contents of the tab control I was distracted by the 3D borders which overplay the folder metaphor.

As many of you will know, Windows provides for limited customizing of the tab control appearance via owner-draw but this is restricted to the tab labels only and not to the tab border or the background.

The obvious solution was to override WM_PAINT to extend the owner-draw mechanism to handle the main tab control border and the individual label borders, and to override WM_ERASEBKGND to paint the background.

The result is CBaseTabControl and CEnTabControl.

CBaseTabControl is the class which contains the necessary hooks to allow derived classes to paint whichever parts of the tab control they choose, and CEnTabControl is an example derived class which implements some features that I have found useful.

Some Comments on the Source Code

  • The background painting is done in CBaseTabControl because this has to be done in a very specific way to prevent flicker and overcome some assumptions Microsoft made during the underlying tab control development (see source comments)
  • If you request that you want to override aspects of the drawing and then do not provide a virtual override, CBaseTabControl will ASSERT just like the standard MFC code and no drawing will be done.
  • CEnTabControl implements its custom drawing using static flags so that every tab control instantiated will share the same attributes. I did it this way so that all the tab controls in an application will have the same look and feel.
  • If you use property sheets alot and want the same functionality, DON'T worry. All you have to do is subclass the tab control within the property sheet like this:
    BOOL CMyPropertySheet::OnInitDialog() 
    {
    	CPropertySheet::OnInitDialog();
    
    	...
    
    	// subclass tab control
    	m_tabCtrl.SubclassDlgItem(
                CPropertySheet::GetTabControl()->GetDlgCtrlID(), this);
    
    	...
    }

Code Fix 1.1

Gian (saviour@libero.it) correctly pointed out that if you try to attach a CImageList to the tab control then you get an ASSERT in the drawing code.

This occurs because the drawing code temporarily attaches the tab control imagelist to a CImageList for drawing purposes and MFC asserts that its already attached to a CImageList (the original).

The reason MFC asserts is because it keeps a static map for converting between HIMAGELIST and CImageList*, and the implementation of the underlying map prevents an HIMAGELIST being mapped to more than one CImageList*.

The fix is to use ImageList_Draw() for drawing.

Notes

  • The code has not been thoroughly tested for bottom and side tabs, so please don't complain if it doesn't work as expected.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions

 
AnswerRe: How to change tab size Pin
.dan.g.3-Aug-04 17:08
professional.dan.g.3-Aug-04 17:08 
GeneralRe: How to change tab size Pin
IMANTHA4-Aug-04 22:10
IMANTHA4-Aug-04 22:10 
GeneralDisplay Icoms with 256 colors and mor Pin
Ralph18-May-04 3:39
Ralph18-May-04 3:39 
GeneralRe: Display Icoms with 256 colors and mor Pin
.dan.g.18-May-04 21:07
professional.dan.g.18-May-04 21:07 
GeneralCool article man. Now Drawing on bottom is OK Pin
vmonster20-Feb-04 3:24
vmonster20-Feb-04 3:24 
GeneralCool enough!! thanks. Pin
t2di4u23-Dec-03 21:30
t2di4u23-Dec-03 21:30 
GeneralGet the initial tab for the TabCtrl Pin
Ruben93815-Sep-03 15:43
Ruben93815-Sep-03 15:43 
Generalsupport for bottom style Pin
rtessler16-Mar-03 14:11
rtessler16-Mar-03 14:11 
Smile | :) coloring the tab control when tab control uses a bottom
(upside down) style did not work so I modified the code:

BOOL CBaseTabCtrl::OnEraseBkgnd(CDC* pDC)
{
CRect rClient, rTab, rTotalTab, rBkgnd, rEdge;
COLORREF crBack;
int nTab, nTabHeight = 0;

CTabCtrl::OnEraseBkgnd(pDC);

// calc total tab width
GetClientRect(rClient);
nTab = GetItemCount();
rTotalTab.SetRectEmpty();

while (nTab--)
{
GetItemRect(nTab, rTab);
rTotalTab.UnionRect(rTab, rTotalTab);
}

nTabHeight = rTotalTab.Height();

// add a bit
rTotalTab.InflateRect(2, 3);
rEdge = rTotalTab;

// then if background color is set, paint the visible background
// area of the tabs in the bkgnd color
// note: the mfc code for drawing the tabs makes all sorts of assumptions
// about the background color of the tab control being the same as the page
// color - in some places the background color shows thru' the pages!!
// so we must only paint the background color where we need to, which is that
// portion of the tab area not excluded by the tabs themselves
crBack = (m_crBack == -1) ? ::GetSysColor(COLOR_3DFACE) : m_crBack;

pDC->SetBkColor(crBack);

if ( GetStyle() & TCS_RIGHT )
{
// full width of tab ctrl below bottom of tabs
rBkgnd = rClient;
rBkgnd.top = rBkgnd.bottom - 3;
pDC->ExtTextOut(rBkgnd.left, rBkgnd.top, ETO_CLIPPED | ETO_OPAQUE, rBkgnd, "", NULL);

// width of tab ctrl visible bkgnd including bottom pixel of tabs to left of tabs
rBkgnd = rClient;
rBkgnd.right = 2;
rBkgnd.top = rBkgnd.bottom - (nTabHeight + 2);
pDC->ExtTextOut(rBkgnd.left, rBkgnd.top, ETO_CLIPPED | ETO_OPAQUE, rBkgnd, "", NULL);

// to right of tabs
rBkgnd = rClient;
rBkgnd.left += rTotalTab.Width() - 2;
rBkgnd.top = rBkgnd.bottom - (nTabHeight + 2);
pDC->ExtTextOut(rBkgnd.left, rBkgnd.top, ETO_CLIPPED | ETO_OPAQUE, rBkgnd, "", NULL);
}
else
{
// full width of tab ctrl above top of tabs
rBkgnd = rClient;
rBkgnd.bottom = rTotalTab.top + 3;
pDC->ExtTextOut(rBkgnd.left, rBkgnd.top, ETO_CLIPPED | ETO_OPAQUE, rBkgnd, "", NULL);

// width of tab ctrl visible bkgnd including bottom pixel of tabs to left of tabs
rBkgnd = rClient;
rBkgnd.right = 2;
rBkgnd.bottom = rBkgnd.top + (nTabHeight + 2);
pDC->ExtTextOut(rBkgnd.left, rBkgnd.top, ETO_CLIPPED | ETO_OPAQUE, rBkgnd, "", NULL);

// to right of tabs
rBkgnd = rClient;
rBkgnd.left += rTotalTab.Width() - 2;
rBkgnd.bottom = rBkgnd.top + (nTabHeight + 2);
pDC->ExtTextOut(rBkgnd.left, rBkgnd.top, ETO_CLIPPED | ETO_OPAQUE, rBkgnd, "", NULL);
}

return TRUE;
}

Robert Tessler
AnswerRe: support for bottom style Pin
Bartosz Bien18-Apr-06 10:43
Bartosz Bien18-Apr-06 10:43 
GeneralOnwer Draw spin control only Pin
Wolfram Steinke22-Feb-03 12:48
Wolfram Steinke22-Feb-03 12:48 
GeneralRe: Onwer Draw spin control only Pin
.dan.g.25-Feb-03 18:44
professional.dan.g.25-Feb-03 18:44 
GeneralRe: Onwer Draw spin control only Pin
Wolfram Steinke27-Feb-03 11:19
Wolfram Steinke27-Feb-03 11:19 
QuestionWhat incase of bitmapped Dialogs Pin
confuzed121-Nov-02 11:34
confuzed121-Nov-02 11:34 
AnswerRe: What incase of bitmapped Dialogs Pin
.dan.g.21-Nov-02 14:16
professional.dan.g.21-Nov-02 14:16 
GeneralPossible to remove spin control Pin
3-Jul-02 0:34
suss3-Jul-02 0:34 
GeneralRe: Possible to remove spin control Pin
.dan.g.3-Jul-02 17:13
professional.dan.g.3-Jul-02 17:13 
GeneralRe: Possible to remove spin control Pin
5-Jul-02 13:02
suss5-Jul-02 13:02 
General'Icons Pin
27-Jun-02 8:16
suss27-Jun-02 8:16 
GeneralRe: 'Icons Pin
.dan.g.27-Jun-02 17:35
professional.dan.g.27-Jun-02 17:35 
GeneralRe: Icons Pin
Gian27-Jun-02 21:45
Gian27-Jun-02 21:45 
GeneralRe: Icons Pin
.dan.g.30-Jun-02 14:39
professional.dan.g.30-Jun-02 14:39 
GeneralVertical style not supported! Pin
31-Jan-02 16:20
suss31-Jan-02 16:20 
GeneralRe: Vertical style not supported! Pin
.dan.g.31-Jan-02 16:26
professional.dan.g.31-Jan-02 16:26 
GeneralRe: Vertical style not supported! Pin
Dmitry Sokolov31-Jan-02 16:32
Dmitry Sokolov31-Jan-02 16:32 
GeneralRe: Vertical style not supported! Pin
Arun N Kumar16-Dec-02 5:45
Arun N Kumar16-Dec-02 5:45 

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.