Click here to Skip to main content
15,867,568 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

 
QuestionThanks for your solution! Pin
WindSparrow17-Apr-12 1:02
WindSparrow17-Apr-12 1:02 
Generalwin32api help Pin
tootall3-Aug-10 10:54
tootall3-Aug-10 10:54 
Generallittle Problem Pin
Taulie26-Jun-07 20:59
Taulie26-Jun-07 20:59 
GeneralUsing Tab Controls to indicate progress through a procedure Pin
reegan4125-May-07 2:23
reegan4125-May-07 2:23 
You know how on some websites..say if you're filling out an application that is flash driven or something, that as you go through the different screens (or steps), there are tabs at the top with a label briefly describing what that step is (usually on a website). So like, when you're purchasing something online, and the first step is putting it in your 'shopping cart' or whatever, then when you go to the next screen, the next tab would highlight and say 'payment method', then at the next screen, the following tab would highlight and say 'billing address', etc.

Long story short: is there (that you know of) an ActiveX (VB) control that would allow me to utilize this kind of object within MS Access?

-
reegan41
QuestionHow can I redraw the navigation arrows appear on the right of the tab control? Pin
larusoft12-Jul-06 17:48
larusoft12-Jul-06 17:48 
GeneralOwnerdrawing TabView.. Pin
AbinThomas16-Jun-06 22:56
AbinThomas16-Jun-06 22:56 
GeneralMaxthon browser style tabctrl Pin
Tcpip200513-Jun-06 21:23
Tcpip200513-Jun-06 21:23 
GeneralAccess the edit control inside the tab Pin
iulikii_026-Apr-06 2:44
iulikii_026-Apr-06 2:44 
GeneralSpace beetween tabs and spin Pin
Exsilius16-Feb-06 21:14
Exsilius16-Feb-06 21:14 
GeneralRe: Space beetween tabs and spin Pin
Hillmann-202313-Mar-06 20:39
Hillmann-202313-Mar-06 20:39 
JokeRe: Space beetween tabs and spin Pin
Exsilius13-Mar-06 20:51
Exsilius13-Mar-06 20:51 
GeneralRe: Space beetween tabs and spin Pin
knareshkumar26-Sep-08 20:57
knareshkumar26-Sep-08 20:57 
GeneralYou code will not work after building it under release mode. Pin
Han Bo Sun18-Jan-06 12:07
professionalHan Bo Sun18-Jan-06 12:07 
GeneralRe: You code will not work after building it under release mode. Pin
.dan.g.18-Jan-06 14:10
professional.dan.g.18-Jan-06 14:10 
GeneralRe: You code will not work after building it under release mode. Pin
Han Bo Sun19-Jan-06 18:55
professionalHan Bo Sun19-Jan-06 18:55 
GeneralRe: You code will not work after building it under release mode. Pin
Han Bo Sun19-Jan-06 18:58
professionalHan Bo Sun19-Jan-06 18:58 
Generalbug with flat view and partially shown tabs Pin
corey0121-Dec-05 9:09
corey0121-Dec-05 9:09 
GeneralExcellent work Pin
RSE Thomas22-Nov-05 1:11
RSE Thomas22-Nov-05 1:11 
GeneralRe: Excellent work Pin
.dan.g.22-Nov-05 14:32
professional.dan.g.22-Nov-05 14:32 
Generalis it possible to create tab button as in MSN messenger Pin
kalicharan23-May-05 1:53
kalicharan23-May-05 1:53 
Questionone tab looks like a command button - HOW? Pin
Member 122346018-Nov-04 14:04
Member 122346018-Nov-04 14:04 
AnswerRe: one tab looks like a command button - HOW? Pin
.dan.g.18-Nov-04 15:00
professional.dan.g.18-Nov-04 15:00 
GeneralNot correct repaint Pin
Mirikos19-Sep-04 11:29
Mirikos19-Sep-04 11:29 
QuestionHow to change tab size Pin
IMANTHA3-Aug-04 1:23
IMANTHA3-Aug-04 1:23 
AnswerRe: How to change tab size Pin
.dan.g.3-Aug-04 17:08
professional.dan.g.3-Aug-04 17:08 

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.