Click here to Skip to main content
13,053,891 members (58,986 online)
Click here to Skip to main content
Add your own
alternative version


153 bookmarked
Posted 15 Jun 2002

Get the real XP look with Tab Pages

, 27 Jul 2003
Rate this:
Please Sign up or sign in to vote.
The original implementation of class TabPage provided by Microsoft is broken: it displays a gray background color rather than the shiny smooth shade expected under Windows XP.

Sample Image - themedtabpage.gif


When you create an application which contains a tabbed view and define a manifest file (as explained on this MSDN page), you would expect the tabbed view to display with the active theme. But it does not! Why? System.Windows.Forms.TabPage does not support the XP visual style, as shown in the screen shot (left tabbed view), but System.Windows.Forms.TabControl does.

I searched the web, hoping to find a solution. I only found people who observed exactly the same problem as I did; and there was no solution readily available to C# programmers.

Trying to set the TabPage.BackColor to transparent did not help. I had to implement my own OnPaintBackground method and draw the themed background myself, if I wanted the tabbed view to display just like those found in non-.NET applications (standard applications don't have a TabPage control above their TabControl; the TabPage  is mainly used to simplify the manipulation of the tab page contents).

Wrapping uxTheme

In order to draw the themed background, you have to call uxtheme.dll  function DrawThemeBackground, which means calling into unmanaged code. I hacked the sources of Microsoft's sample ThemeExplorer in order to extract the C++ wrappers needed to call uxtheme.dll. I implemented the following  functions in an unmanaged C++ DLL (that is, pure Win32 code) named OPaC.uxTheme.Win32.dll :

  • bool Wrapper_IsAppThemed ();
    Test if the application has to use themes to display itself. This function returns true on XP if the user has activated the "Windows XP style" in the display properties. On systems which do not support uxtheme.dll, the function returns false.
  • bool Wrapper_DrawBackground (const wchar_t* name, const wchar_t* part, const wchar_t* state, HDC hdc, ...);
    Draw the background of the specified widget. The name and part describe the widget ("BUTTON"/"CHECKBOX" and "TAB"/"BODY" would be valid names/parts, for instance).
    The meaning of state depends on the widget (for the checkbox, you could specify "UNCHECKEDNORMAL", "UNCHECKEDHOT", "UNCHECKEDPRESSED", "UNCHECKEDDISABLED", "CHECKEDNORMAL", etc.)
  • bool Wrapper_DrawThemeParentBackground (HWND hwnd, HDC hdc);
  • bool Wrapper_DrawThemeParentBackgroundRect (HWND hwnd, HDC hdc, int ox, int oy, int dx, int dy);
    Draw the background of the parent view showing through.
  • bool Wrapper_GetTextColor (const wchar_t* name, const wchar_t* part, const wchar_t* state, int* r, int* g, int* b);
    Get the color of the specified text. The name, part and state strings have the same meaning as with Wrapper_DrawBackground.

All these functions degrade gracefully if there is no uxtheme.dll present on the system (they just return false ). They are accessible from .NET as static functions published by the  OPaC.uxTheme.Wrapper class. The C# wrapper is just a collection of P/Invoke functions declared with the DllImport attribute.

In my first version of this article, I implemented the wrapper functions in a managed C++ assembly, but the resulting DLL was way too large (more than 160 KB). By implementing the wrapper as an unmanaged DLL, with just very little C# glue in a separate assembly, I reduced the combined DLL size to about 35 KB, which is much more reasonable.

Here is the implementation of the wrapper used to get the color of a text element using uxtheme.dll :

bool __stdcall Wrapper_GetTextColor (const wchar_t* name,
                                     const wchar_t* part_name,
                                     const wchar_t* state_name,
                                     int* r, int* g, int* b)
    bool ok = false;
    if (xpStyle.IsAppThemed ())
        HTHEME theme = xpStyle.OpenThemeData (NULL, name);
        if (theme != NULL)
                int part;
                int state;
                if (FindVisualStyle (name, part_name, state_name, part, state))
                    COLORREF color;
                    int prop = TMT_TEXTCOLOR;
                    if (S_OK == xpStyle.GetThemeColor (theme, part, state, prop, &color))
                        *r = GetRValue (color);
                        *g = GetGValue (color);
                        *b = GetBValue (color);
                        ok = true;
            catch (...)
                // Swallow any exceptions - just in case, so we don't crash the caller 
                // if something gets really wrong.
            xpStyle.CloseThemeData (theme);
    return ok;

Deriving TabPage

I derived System.Windows.Forms.TabPage and wrote the override for method OnPaintBackground , which called into the wrapper assembly. But there still was a problem I had to address : the text labels I put on the tabbed view did not display the proper background; when setting the control's property TabPage.BackColor to Colors.Transparent, the background has not exactly the expected shade. What happens, in fact, when you tell .NET that the background of a control is transparent, is that the erasing code simply calls the parent's OnPaintBackground method, specifying a clipping region matching the control's shape. When subsequently calling DrawThemeBackground, the theme gets drawn with the wrong origin (the origin of the clipping region is used, instead of the origin of the containing TabPage). Code to offset the theme is therefore required...

My first naive attempt was to just offset the background to match the origin of the clipping region. This worked fine as long as the view did not get partially obscured, and then partially repainted : in this case, the clipping origin no longer matched the origin of the label object and the background was, once more, painted with the wrong offset. I finally came up with a rather complex piece of code which walks trough all children, trying to identify which one's background is being erased and using the appropriate offset :

public class TabPage : System.Windows.Forms.TabPage
    public TabPage()
    public bool UseTheme
        get { return OPaC.uxTheme.Wrapper.IsAppThemed (); }
    protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        if (this.UseTheme)
            int ox = (int) e.Graphics.VisibleClipBounds.Left;
            int oy = (int) e.Graphics.VisibleClipBounds.Top;
            int dx = (int) e.Graphics.VisibleClipBounds.Width;
            int dy = (int) e.Graphics.VisibleClipBounds.Height;
            if ((ox != 0) || (oy != 0) || (dx != this.Width) || (dy != this.Height))
                this.PaintChildrenBackground (e.Graphics, this, 
                                new System.Drawing.Rectangle (ox, oy, dx, dy), 0, 0);
                this.ThemedPaintBackground (e.Graphics, 0, 0, 
                                            this.Width, this.Height, 0, 0);
            base.OnPaintBackground (e);
    private bool PaintChildrenBackground(System.Drawing.Graphics graphics,
                                         System.Windows.Forms.Control control,
                                         System.Drawing.Rectangle rect,
                                         int ofx, int ofy)
        foreach (System.Windows.Forms.Control child in control.Controls)
            System.Drawing.Rectangle find
                         = new System.Drawing.Rectangle (child.Location, child.Size);
            if (find.Contains (rect))
                System.Drawing.Rectangle child_rect = rect;
                child_rect.Offset (- child.Left, - child.Top);
                if (this.PaintChildrenBackground (graphics, child, child_rect, 
                                                  ofx + child.Left, ofy + child.Top))
                    return true;
                this.ThemedPaintBackground (graphics, child.Left, child.Top, 
                                            child.Width, child.Height, ofx, ofy);
                return true;
        return false;

    private void ThemedPaintBackground(System.Drawing.Graphics graphics,
                                       int ox, int oy, int dx, int dy, int ofx, int ofy)
        System.IntPtr hdc  = graphics.GetHdc ();
        OPaC.uxTheme.Wrapper.DrawBackground ("TAB", "BODY", null, hdc, -ofx, -ofy,
                                             this.Width, this.Height, ox, oy, dx, dy);
        graphics.ReleaseHdc (hdc);

More trouble... GroupBox and CheckBox

I soon discovered that the System.Windows.Forms.GroupBox did not paint its background with the proper color when using FlatStyle.System, which is quite annoying. It insisted on painting the background with the default background color. This meant that I had to derive GroupBox and provide my own OnPaint implementation, based on uxtheme.dll. The replacement class I provide can be used both with the classic and the XP styles (and it supports switching from one to the other).

After I started using my own GroupBox, it became obvious that I had to derive System.Windows.Forms.CheckBox too, since it too insisted on painting its background itself... Implementing the check box was not really complicated : I just had to make sure that the look and feel was exactly the same as the original version shipped by Microsoft (drawing the caption at the right place, drawing the focus rectangle, reflecting the state of the widget - hot/pressed/disabled/etc.)

If you are interested by the gory details, have a look at the sources...

Using OPaC.Themed.Forms.TabPage

To use the code provided in this article, you must compile the unmanaged DLL (OPaC.uxTheme.Win32.dll) and copy the contents of the bin\debug and bin\release directories found in the solution root to your application's bin\debug and bin\release directories. You then add a reference to OPaC.Themed.Forms.dll (which depends on OPaC.uxTheme.dll and OPaC.uxTheme.Win32.dll)...

Create your user interface and then replace System.Windows.Forms.TabPage with OPaC.Themed.Forms.TabPage, System.Windows.Forms.GroupBox with OPaC.Themed.Forms.GroupBox and System.Windows.Forms.CheckBox with OPaC.Themed.Forms.CheckBox. Do not set these object's BackColor to Color.Transparent or try to force their FlatStyle to anything else than FlatStyle.Standard.

To make sure your controls display as desired when used in a TabPage, follow these additional rules :

  • Button. FlatStyle = FlatStyle.System;
  • RadioButton.Fl atStyle = FlatStyle.System;
  • Label.B ackColor = Color.Transparent;

Keep the default values for the FlatStyle and BackColor properties for all other controls.

Final note

I tried out every possible solution I could dream of in order to get the controls to paint properly in a tab control. Only one solution addressed all possible problems, and it is far from elegant. If only Microsoft made the sources of System.Windows.Forms available (as they did for the Rotor implementation of the .NET framework), fixing the controls would have been simple, efficient and elegant.

A helpful reader hinted me to use function EnableThemeDialogTexture in uxtheme.dll, which can be used in Win32 to tell a window how to paint its background, but as expected, it does not work either with the Windows Forms implementation. So the solution presented in this article still seems to be the only way to go.


The source files were last updated on 24 July 2003.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Pierre Arnaud
Web Developer
Switzerland Switzerland

Pierre Arnaud got a Ph.D. in computer science at the Swiss Federal Institute of Technology; he currently works both as an independent contractor on hardware and software projects at OPaC bright ideas and as a senior software designer at EPSITEC.

Pierre was a key contributor to the Smaky computer, a real time, multitasking system based on the Motorola 680x0 processor family.

Now, Pierre works on his spare time for the Creative Docs .NET project: it is a vector based drawing and page layout software based on .NET and AGG.

You may also be interested in...


Comments and Discussions

QuestionLicense Pin
Alan Hayslep29-Aug-13 0:36
memberAlan Hayslep29-Aug-13 0:36 
AnswerRe: License Pin
Pierre Arnaud29-Aug-13 1:26
memberPierre Arnaud29-Aug-13 1:26 
GeneralRe: License Pin
Alan Hayslep29-Aug-13 3:04
memberAlan Hayslep29-Aug-13 3:04 
GeneralAnother Simple Fix Pin
phanf15-Jun-07 7:56
memberphanf15-Jun-07 7:56 
GeneralOpenThemeData HTHEME Limit Pin
Paul F. Williams11-Apr-06 6:14
memberPaul F. Williams11-Apr-06 6:14 
Generaldumb question Pin
melanieab30-Mar-05 8:32
membermelanieab30-Mar-05 8:32 
QuestionA slightly less painful approach? Pin
Mclusky8-Feb-05 15:48
memberMclusky8-Feb-05 15:48 
AnswerRe: A slightly less painful approach? Pin
Pierre Arnaud8-Feb-05 19:25
memberPierre Arnaud8-Feb-05 19:25 
GeneralCheckBox error on scroll and resize Pin
Frederik W. Johansen26-Aug-04 23:18
memberFrederik W. Johansen26-Aug-04 23:18 
QuestionAn even easier way? Pin
lyangsprint28-Jul-04 15:08
memberlyangsprint28-Jul-04 15:08 
AnswerRe: An even easier way? Pin
Kenneth Beckett3-Aug-04 10:06
memberKenneth Beckett3-Aug-04 10:06 
AnswerRe: An even easier way? Pin
Pierre Arnaud7-Aug-04 9:34
memberPierre Arnaud7-Aug-04 9:34 
GeneralUnable to load DLL (OPaC.uxTheme.Win32.dll) - RESOLVED Pin
matherb10-May-04 9:39
membermatherb10-May-04 9:39 
GeneralRe: Unable to load DLL (OPaC.uxTheme.Win32.dll) - RESOLVED Pin
manishma25-Feb-05 9:14
membermanishma25-Feb-05 9:14 
GeneralRe: Unable to load DLL (OPaC.uxTheme.Win32.dll) - RESOLVED Pin
manishma25-Feb-05 9:22
membermanishma25-Feb-05 9:22 
GeneralSplitters Pin
Draugluin19-Apr-04 11:35
memberDraugluin19-Apr-04 11:35 
GeneralSplitters fix? Pin
Draugluin19-Apr-04 11:49
memberDraugluin19-Apr-04 11:49 
QuestionIs it possible to port it to native c++ (MFC based)? Pin
Amit Gefen7-Apr-04 18:34
memberAmit Gefen7-Apr-04 18:34 
GeneralProblem running on Win98 Pin
gnosis15-Mar-04 16:10
membergnosis15-Mar-04 16:10 
QuestionThis is a bit cleaner? Pin
sichbo310-Mar-04 7:13
membersichbo310-Mar-04 7:13 
AnswerRe: This is a bit cleaner? Pin
Pierre Arnaud10-Mar-04 9:49
memberPierre Arnaud10-Mar-04 9:49 
AnswerRe: This is a bit cleaner? Pin
Cole_S23-May-04 10:21
memberCole_S23-May-04 10:21 
AnswerRe: This is a bit cleaner? Pin
Anonymous2-Apr-05 13:23
sussAnonymous2-Apr-05 13:23 
GeneralSetWindowTheme Pin
shalnov15-Jan-04 4:39
membershalnov15-Jan-04 4:39 
GeneralOPaC.uxTheme.Win32.dll not found Pin
Schonki8-Jan-04 4:37
memberSchonki8-Jan-04 4:37 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 28 Jul 2003
Article Copyright 2002 by Pierre Arnaud
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid