Click here to Skip to main content
15,121,790 members
Articles / Desktop Programming / MFC
Article
Posted 30 Jan 2002

Stats

411.9K views
16.6K downloads
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

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)

Share

About the Author

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

Comments and Discussions

 
GeneralRe: How to change tab size Pin
IMANTHA4-Aug-04 23:10
MemberIMANTHA4-Aug-04 23:10 
GeneralDisplay Icoms with 256 colors and mor Pin
Ralph18-May-04 4:39
MemberRalph18-May-04 4:39 
GeneralRe: Display Icoms with 256 colors and mor Pin
.dan.g.18-May-04 22:07
professional.dan.g.18-May-04 22:07 
GeneralCool article man. Now Drawing on bottom is OK Pin
vmonster20-Feb-04 4:24
Membervmonster20-Feb-04 4:24 
GeneralCool enough!! thanks. Pin
t2di4u23-Dec-03 22:30
Membert2di4u23-Dec-03 22:30 
GeneralGet the initial tab for the TabCtrl Pin
Ruben93815-Sep-03 16:43
MemberRuben93815-Sep-03 16:43 
Generalsupport for bottom style Pin
rtessler16-Mar-03 15:11
Memberrtessler16-Mar-03 15:11 
AnswerRe: support for bottom style Pin
Bartosz Bien18-Apr-06 11:43
MemberBartosz Bien18-Apr-06 11:43 
GeneralOnwer Draw spin control only Pin
Wolfram Steinke22-Feb-03 13:48
MemberWolfram Steinke22-Feb-03 13:48 
GeneralRe: Onwer Draw spin control only Pin
.dan.g.25-Feb-03 19:44
professional.dan.g.25-Feb-03 19:44 
GeneralRe: Onwer Draw spin control only Pin
Wolfram Steinke27-Feb-03 12:19
MemberWolfram Steinke27-Feb-03 12:19 
QuestionWhat incase of bitmapped Dialogs Pin
confuzed121-Nov-02 12:34
Memberconfuzed121-Nov-02 12:34 
AnswerRe: What incase of bitmapped Dialogs Pin
.dan.g.21-Nov-02 15:16
professional.dan.g.21-Nov-02 15:16 
GeneralPossible to remove spin control Pin
3-Jul-02 1:34
suss3-Jul-02 1:34 
GeneralRe: Possible to remove spin control Pin
.dan.g.3-Jul-02 18:13
professional.dan.g.3-Jul-02 18:13 
GeneralRe: Possible to remove spin control Pin
5-Jul-02 14:02
suss5-Jul-02 14:02 
General'Icons Pin
27-Jun-02 9:16
suss27-Jun-02 9:16 
GeneralRe: 'Icons Pin
.dan.g.27-Jun-02 18:35
professional.dan.g.27-Jun-02 18:35 
GeneralRe: Icons Pin
Gian27-Jun-02 22:45
MemberGian27-Jun-02 22:45 
GeneralRe: Icons Pin
.dan.g.30-Jun-02 15:39
professional.dan.g.30-Jun-02 15:39 
GeneralVertical style not supported! Pin
31-Jan-02 17:20
suss31-Jan-02 17:20 
GeneralRe: Vertical style not supported! Pin
.dan.g.31-Jan-02 17:26
professional.dan.g.31-Jan-02 17:26 
GeneralRe: Vertical style not supported! Pin
Dmitry Sokolov31-Jan-02 17:32
MemberDmitry Sokolov31-Jan-02 17:32 
GeneralRe: Vertical style not supported! Pin
Arun N Kumar16-Dec-02 6:45
MemberArun N Kumar16-Dec-02 6:45 
GeneralSorry Pin
dvr9920-Dec-02 18:30
Memberdvr9920-Dec-02 18:30 

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.