Click here to Skip to main content
15,171,439 members
Articles / Desktop Programming / MFC
Posted 30 Jan 2002


101 bookmarked

Ownerdraw Tab Controls - Borders and All

Rate me:
Please Sign up or sign in to vote.
4.89/5 (42 votes)
30 Jun 2002CPOL2 min read
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


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() 
    	// subclass tab control
                CPropertySheet::GetTabControl()->GetDlgCtrlID(), this);

Code Fix 1.1

Gian ( 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.


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


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


About the Author

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 2:02
MemberWindSparrow17-Apr-12 2:02 
Generalwin32api help Pin
tootall3-Aug-10 11:54
Membertootall3-Aug-10 11:54 
Generallittle Problem Pin
Taulie26-Jun-07 21:59
MemberTaulie26-Jun-07 21:59 
GeneralUsing Tab Controls to indicate progress through a procedure Pin
reegan4125-May-07 3:23
Memberreegan4125-May-07 3:23 
QuestionHow can I redraw the navigation arrows appear on the right of the tab control? Pin
larusoft12-Jul-06 18:48
Memberlarusoft12-Jul-06 18:48 
GeneralOwnerdrawing TabView.. Pin
AbinThomas16-Jun-06 23:56
MemberAbinThomas16-Jun-06 23:56 
GeneralMaxthon browser style tabctrl Pin
Tcpip200513-Jun-06 22:23
MemberTcpip200513-Jun-06 22:23 
GeneralAccess the edit control inside the tab Pin
iulikii_026-Apr-06 3:44
Memberiulikii_026-Apr-06 3:44 
GeneralSpace beetween tabs and spin Pin
Exsilius16-Feb-06 22:14
MemberExsilius16-Feb-06 22:14 
GeneralRe: Space beetween tabs and spin Pin
Hillmann13-Mar-06 21:39
MemberHillmann13-Mar-06 21:39 
JokeRe: Space beetween tabs and spin Pin
Exsilius13-Mar-06 21:51
MemberExsilius13-Mar-06 21:51 
GeneralRe: Space beetween tabs and spin Pin
knareshkumar26-Sep-08 21:57
Memberknareshkumar26-Sep-08 21:57 
by using OnDrawItem u can set the bkgnd clr of the tabctrl.
u read the msdn articles of ctabctrl
iam also coloring the tabctrl.
i had filled the tabctrl with some color and then iam drawing each button.
on mousemove iam coding for highlight button

the only problem iam facing is flickering on mousemove as if i use compatible dc on mouse move highlighting of tab button gets slow.

one more prob. iam facing is that the last tab gets bulged(looks like poped up) after adding all tabctrl.

I am naresh kavali
intrested in formus of VC++, Com,ATL

GeneralYou code will not work after building it under release mode. Pin
Han Bo Sun18-Jan-06 13:07
professionalHan Bo Sun18-Jan-06 13:07 
GeneralRe: You code will not work after building it under release mode. Pin
.dan.g.18-Jan-06 15:10
professional.dan.g.18-Jan-06 15:10 
GeneralRe: You code will not work after building it under release mode. Pin
Han Bo Sun19-Jan-06 19:55
professionalHan Bo Sun19-Jan-06 19:55 
GeneralRe: You code will not work after building it under release mode. Pin
Han Bo Sun19-Jan-06 19:58
professionalHan Bo Sun19-Jan-06 19:58 
Generalbug with flat view and partially shown tabs Pin
corey0121-Dec-05 10:09
Membercorey0121-Dec-05 10:09 
GeneralExcellent work Pin
RSE Thomas22-Nov-05 2:11
MemberRSE Thomas22-Nov-05 2:11 
GeneralRe: Excellent work Pin
.dan.g.22-Nov-05 15:32
professional.dan.g.22-Nov-05 15:32 
Generalis it possible to create tab button as in MSN messenger Pin
kalicharan23-May-05 2:53
Memberkalicharan23-May-05 2:53 
Questionone tab looks like a command button - HOW? Pin
Member 122346018-Nov-04 15:04
MemberMember 122346018-Nov-04 15:04 
AnswerRe: one tab looks like a command button - HOW? Pin
.dan.g.18-Nov-04 16:00
professional.dan.g.18-Nov-04 16:00 
GeneralNot correct repaint Pin
Mirikos19-Sep-04 12:29
MemberMirikos19-Sep-04 12:29 
QuestionHow to change tab size Pin
IMANTHA3-Aug-04 2:23
MemberIMANTHA3-Aug-04 2:23 
AnswerRe: How to change tab size Pin
.dan.g.3-Aug-04 18:08
professional.dan.g.3-Aug-04 18: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.