Click here to Skip to main content
Licence CPOL
First Posted 23 Mar 2009
Views 51,928
Downloads 2,458
Bookmarked 111 times

Tab Control with Correct Bottom Theming in .NET

By | 8 Mar 2010 | Article
How to correctly draw the .NET Tab control with bottom tabs and visual styles enabled

Using XPTabControl

Introduction

Most developers face the same problem when using a tab control with a tab orientation other than Top: when visual styles are enabled, the tabs are drawn incorrectly (to say more precisely - in top orientation only). The same thing happened for me one day. I had a task to convert an old .NET 1.1 application to run under .NET Framework 2.0. I wanted to add visual styles support, and oops - we have a known tab problem. I had been searching the web and had found some solutions, but none of them satisfied me fully. I decided to write my own control XPTabControl by subclassing System.Windows.Forms.TabControl.

You might find my control useful because I think many of you still use Windows Forms and not WPF, and it seems Microsoft hasn't added correct tab drawing in SysTabControl32 even in Windows 7. Those guys just require only the top tabs for you to get your application Microsoft certified (by the way, Apple has implemented their tabs correctly in any orientation from stock :-); you know what I mean if you have used Interface Builder deep enough at least once).

Background

I've used the Adi DEDIC article here on CodeProject as a starting point so you can read the Background section from there. Adi explains how to rotate tabs and draw them. My control uses a similar technique. But Adi's control is written in C++ and uses MFC, mine is written mostly in managed code though it uses some platform interops. I have also used some tips about the tab scroller from Mick Doherty's site. You can find the bottom tab control there also in his implementation, but without the source code.

Using the Code

The simplest way of using the control is to add it to your toolbox. To do this, just right-click on the preferred section of the toolbox and then select "Choose items..." in the popped up menu; click "Browse" in the ".NET Framework Components" tab and look for XPTabControl.dll. You're done! Now, you should see XPTabControl in your toolbox, and you can drag it on your form like any other control.

If you don't want to use it in the way described above for some reasons (you aren't using Visual Studio but Notepad and command prompt, for example), you have to add XPTabControl.dll to your project reference libraries. After that, in the code where you are planning to use the control, add the following directive:

using VSControls;

Or in Visual Basic:

Imports VSControls

Now, you can create and manipulate XPTabControl the same way as System.Windows.Forms.TabControl:

XPTabControl myControl = new XPTabControl();
myControl.Location = new Point(10, 10);
//other code you need

In Visual Basic:

Dim myControl As New XPTabControl
myControl.Location = New Point(10, 10)
'other code you need

If you're going to use the sources instead of the binary DLL provided with the demo, take note that the XPTabControl.Designer.cs file isn't only designer generated, because I've added some additional disposing code to the Dispose function located there. And, one more note: all projects and solutions are generated with Visual Studio 2008 and .NET Framework 2.0.

Requirements & Limitations

  • .NET Framework 2.0 or newer is required to run or compile the code (because I used classes from the System.Windows.Forms.VisualStyles namespace which was introduced in that version of the framework).
  • Custom drawing is not guaranteed to work correctly with visual styles other than Microsoft's ones, i.e., Luna, Media Center Royale, Zune, and... yes, Windows Vista Aero (as you can see in the screenshot above).
  • Supported Windows systems: XP, Vista, 7. Of course, visual styles must be enabled; otherwise, drawing will be done by the default Windows engine (that's the way I've designed it).
  • Left and right tab alignment isn't supported. I suppose it is not very convenient for the user to rotate his/her head in the shoulder direction for a tab's caption to be read, don't you think so? But, if you badly need that feature, you can implement it by yourself.
  • Transparent background for the whole control isn't supported yet.

Points of Interest

  • This article shows how you can subclass some standard .NET controls and override some properties to reach the desired effect.
  • You will learn how to use the System.Windows.Forms.VisualStyles namespace to draw parts of some controls (tab control, in our case).
  • Also, the code shows how to override WndProc to react on some Windows messages.

History

  • 20.03.2009 - Version 1.0
  • 7.03.2010 - Version 1.0.1
    • Fixed bug with tab text (thanks to SierraMike for reporting). Now text is drawn by GDI instead of GDI+ (I found the solution for "memory bitmap + TextRenderer + ClearType enabled" problem) and so it fits inside tabs

License

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

About the Author

VUnreal

Software Developer (Senior)
Scand Ltd.
Belarus Belarus

Member

I've have been programming since 1998. I started from command line applications written in Express Pascal 2.0 under CP-M. In 2002 I've learned C++ and had been programming with Borland C++ Builder. In 2005 I've discovered C# for myself. My currently known languages are: C++, Pascal(Delphi), C#, VB.NET, Objective-C and Java.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThats great! Pingroupvlad7819:22 7 Jun '11  
AnswerRe: Thats great! PinmemberVUnreal10:09 8 Jun '11  
GeneralRe: Thats great! Pingroupvlad78118:14 8 Jun '11  
GeneralRe: Thats great! PinmemberVUnreal11:14 12 Jun '11  
GeneralRe: Thats great! Pingroupvlad78115:51 13 Jun '11  
GeneralMy vote of 5 PinmemberHeaven20208:49 5 Nov '10  
QuestionChange background color? Pinmemberbobmck2:34 16 Oct '10  
AnswerRe: Change background color? PinmemberVUnreal10:34 18 Oct '10  
QuestionWhat about a Close-Button (X) PinmemberT_uRRiCA_N22:43 20 Jun '10  
AnswerRe: What about a Close-Button (X) PinmemberVUnreal9:13 21 Jun '10  
GeneralMissing Left & Right Pinmembertim_mcgwyn2:19 18 Nov '09  
AnswerRe: Missing Left & Right PinmemberVUnreal22:37 18 Nov '09  
GeneralRe: Missing Left & Right PinmemberQue Trac12:59 7 Apr '10  
AnswerRe: Missing Left & Right PinmemberVUnreal9:16 8 Apr '10  
QuestionSoftware Learning PinmemberQue Trac12:26 8 Apr '10  
AnswerRe: Software Learning PinmemberVUnreal11:13 9 Apr '10  
QuestionNice control, but still some bugs ... PinmemberSierraMike7:59 15 Oct '09  
AnswerRe: Nice control, but still some bugs ... PinmemberSierraMike12:34 15 Oct '09  
AnswerRe: Nice control, but still some bugs ... [modified] PinmemberVUnreal22:03 15 Oct '09  
Generalunsafe code isn't cool Pinmembersotona2:34 7 Apr '09  
AnswerRe: unsafe code isn't cool PinmemberVUnreal9:12 7 Apr '09  
GeneralRe: unsafe code isn't cool Pinmembersotona20:01 7 Apr '09  
JokeRe: unsafe code isn't cool PinmemberVUnreal20:26 7 Apr '09  
GeneralThanks PinmemberRick Hansen7:35 31 Mar '09  
GeneralRe: Thanks [modified] PinmemberVUnreal9:50 31 Mar '09  
Me too, and after quite long search in Web for a free control that can behave as close as possible like standard tab control I had to write it myself.

modified on Wednesday, July 27, 2011 5:22 PM

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 8 Mar 2010
Article Copyright 2009 by VUnreal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid