Click here to Skip to main content
6,291,522 members and growing! (12,771 online)
Email Password   helpLost your password?
Desktop Development » Tabs & Property Pages » Owner Drawn     Intermediate License: The Code Project Open License (CPOL)

Ownerdraw Tab Controls - Borders and All

By .dan.g.

A framework for overriding all aspects of a tab control's apprearance, including the borders, the background and of course the tabs themselves.
VC6Win2K, MFC, Dev
Posted:30 Jan 2002
Updated:30 Jun 2002
Views:216,442
Bookmarked:66 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
39 votes for this article.
Popularity: 7.04 Rating: 4.43 out of 5
1 vote, 2.9%
1

2
1 vote, 2.9%
3
6 votes, 17.6%
4
26 votes, 76.5%
5

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)

About the Author

.dan.g.


Member
.dan.g. is a former chartered structural engineer from the uk. He's been programming for 25 years since university and has been developing commercial windows software in Australia since 1998. He has a shaved head and assorted whiskers should you want to recognize him. [update] Dan has now grown his hair and removed his whiskers in an attempt to appear more normal. However he can still be recognized (in the summer months) by the aqua nail-polish he uses on his big toes. [/update]

For all his latest freeware visit http://www.abstractspoon.com/
Occupation: Software Developer (Senior)
Location: Australia Australia

Other popular Tabs & Property Pages articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 53 (Total in Forum: 53) (Refresh)FirstPrevNext
Generallittle Problem Pinmembergrandmasta121:59 26 Jun '07  
GeneralUsing Tab Controls to indicate progress through a procedure Pinmemberreegan413:23 25 May '07  
GeneralHow can I redraw the navigation arrows appear on the right of the tab control? Pinmemberlarusoft18:48 12 Jul '06  
GeneralOwnerdrawing TabView.. PinmemberAbinThomas23:56 16 Jun '06  
GeneralMaxthon browser style tabctrl PinmemberTcpip200522:23 13 Jun '06  
GeneralAccess the edit control inside the tab Pinmemberiulikii_03:44 26 Apr '06  
GeneralSpace beetween tabs and spin PinmemberExsilius22:14 16 Feb '06  
GeneralRe: Space beetween tabs and spin PinmemberHillmann21:39 13 Mar '06  
JokeRe: Space beetween tabs and spin PinmemberExsilius21:51 13 Mar '06  
GeneralRe: Space beetween tabs and spin Pinmemberknareshkumar21:57 26 Sep '08  
GeneralYou code will not work after building it under release mode. PinmemberHanbo13:07 18 Jan '06  
GeneralRe: You code will not work after building it under release mode. Pinmember.dan.g.15:10 18 Jan '06  
GeneralRe: You code will not work after building it under release mode. PinmemberHanbo19:55 19 Jan '06  
GeneralRe: You code will not work after building it under release mode. PinmemberHanbo19:58 19 Jan '06  
Generalbug with flat view and partially shown tabs Pinmembercorey0110:09 21 Dec '05  
GeneralExcellent work PinmemberRSE Thomas2:11 22 Nov '05  
GeneralRe: Excellent work Pinmember.dan.g.15:32 22 Nov '05  
Generalis it possible to create tab button as in MSN messenger Pinmemberkalicharan2:53 23 May '05  
Generalone tab looks like a command button - HOW? Pinmemberpinoyonline15:04 18 Nov '04  
GeneralRe: one tab looks like a command button - HOW? Pinmember.dan.g.16:00 18 Nov '04  
GeneralNot correct repaint PinmemberMirikos12:29 19 Sep '04  
GeneralHow to change tab size PinmemberIMANTHA2:23 3 Aug '04  
GeneralRe: How to change tab size Pinmember.dan.g.18:08 3 Aug '04  
GeneralRe: How to change tab size PinmemberIMANTHA23:10 4 Aug '04  
GeneralDisplay Icoms with 256 colors and mor PinmemberRalph4:39 18 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jun 2002
Editor: Nishant Sivakumar
Copyright 2002 by .dan.g.
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project