Click here to Skip to main content
Click here to Skip to main content
Go to top

Ownerdraw Tab Controls - Borders and All

, 30 Jun 2002
Rate this:
Please Sign up or sign in to vote.
A framework for overriding all aspects of a tab control's apprearance, including the borders, the background and of course the tabs themselves.
<!-- Download Links --> <!-- Article image -->

Sample Image - TabControl.jpg

<!-- Add the rest of your HTML here -->

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.
 
For all his latest freeware visit AbstractSpoon.

Comments and Discussions

 
QuestionThanks for your solution! PinmemberWindSparrow17-Apr-12 1:02 
Generalwin32api help Pinmembertootall3-Aug-10 10:54 
Generallittle Problem Pinmembergrandmasta126-Jun-07 20:59 
GeneralUsing Tab Controls to indicate progress through a procedure Pinmemberreegan4125-May-07 2:23 
QuestionHow can I redraw the navigation arrows appear on the right of the tab control? Pinmemberlarusoft12-Jul-06 17:48 
GeneralOwnerdrawing TabView.. PinmemberAbinThomas16-Jun-06 22:56 
GeneralMaxthon browser style tabctrl PinmemberTcpip200513-Jun-06 21:23 
GeneralAccess the edit control inside the tab Pinmemberiulikii_026-Apr-06 2:44 
GeneralSpace beetween tabs and spin PinmemberExsilius16-Feb-06 21:14 
GeneralRe: Space beetween tabs and spin PinmemberHillmann13-Mar-06 20:39 
JokeRe: Space beetween tabs and spin PinmemberExsilius13-Mar-06 20:51 
GeneralRe: Space beetween tabs and spin Pinmemberknareshkumar26-Sep-08 20:57 
GeneralYou code will not work after building it under release mode. PinmemberHanbo18-Jan-06 12:07 
GeneralRe: You code will not work after building it under release mode. Pinmember.dan.g.18-Jan-06 14:10 
GeneralRe: You code will not work after building it under release mode. PinmemberHanbo19-Jan-06 18:55 
GeneralRe: You code will not work after building it under release mode. PinmemberHanbo19-Jan-06 18:58 
Generalbug with flat view and partially shown tabs Pinmembercorey0121-Dec-05 9:09 
GeneralExcellent work PinmemberRSE Thomas22-Nov-05 1:11 
GeneralRe: Excellent work Pinmember.dan.g.22-Nov-05 14:32 
Generalis it possible to create tab button as in MSN messenger Pinmemberkalicharan23-May-05 1:53 
Questionone tab looks like a command button - HOW? Pinmemberpinoyonline18-Nov-04 14:04 
AnswerRe: one tab looks like a command button - HOW? Pinmember.dan.g.18-Nov-04 15:00 
GeneralNot correct repaint PinmemberMirikos19-Sep-04 11:29 
QuestionHow to change tab size PinmemberIMANTHA3-Aug-04 1:23 
AnswerRe: How to change tab size Pinmember.dan.g.3-Aug-04 17:08 
GeneralRe: How to change tab size PinmemberIMANTHA4-Aug-04 22:10 
GeneralDisplay Icoms with 256 colors and mor PinmemberRalph18-May-04 3:39 
GeneralRe: Display Icoms with 256 colors and mor Pinmember.dan.g.18-May-04 21:07 
GeneralCool article man. Now Drawing on bottom is OK Pinmembervmonster20-Feb-04 3:24 
GeneralCool enough!! thanks. Pinmembert2di4u23-Dec-03 21:30 
GeneralGet the initial tab for the TabCtrl PinmemberRuben93815-Sep-03 15:43 
Generalsupport for bottom style Pinmemberrtessler16-Mar-03 14:11 
AnswerRe: support for bottom style PinmemberBartosz Bien18-Apr-06 10:43 
GeneralOnwer Draw spin control only PinmemberWolfram Steinke22-Feb-03 12:48 
GeneralRe: Onwer Draw spin control only PinmemberDanG25-Feb-03 18:44 
GeneralRe: Onwer Draw spin control only PinmemberWolfram Steinke27-Feb-03 11:19 
QuestionWhat incase of bitmapped Dialogs PinsussAnonymous21-Nov-02 11:34 
AnswerRe: What incase of bitmapped Dialogs PinmemberDanG21-Nov-02 14:16 
GeneralPossible to remove spin control PinmemberAnonymous3-Jul-02 0:34 
GeneralRe: Possible to remove spin control PinmemberDanG3-Jul-02 17:13 
GeneralRe: Possible to remove spin control PinmemberAnonymous5-Jul-02 13:02 
General'Icons PinmemberAnonymous27-Jun-02 8:16 
GeneralRe: 'Icons PinmemberDanG27-Jun-02 17:35 
GeneralRe: Icons PinmemberGian27-Jun-02 21:45 
GeneralRe: Icons PinmemberDanG30-Jun-02 14:39 
GeneralVertical style not supported! PinmemberDmitry Sokolov31-Jan-02 16:20 
GeneralRe: Vertical style not supported! PinmemberDanG31-Jan-02 16:26 
GeneralRe: Vertical style not supported! PinmemberDmitry Sokolov31-Jan-02 16:32 
GeneralRe: Vertical style not supported! PinmemberArun N Kumar16-Dec-02 5:45 
GeneralSorry Pinmemberdvr9920-Dec-02 17:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 1 Jul 2002
Article Copyright 2002 by .dan.g.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid