Click here to Skip to main content
12,999,417 members (53,527 online)
Click here to Skip to main content
Add your own
alternative version


156 bookmarked
Posted 22 Dec 2001

Add XP Visual Style Support to OWNERDRAW Controls

, 22 Dec 2001
Rate this:
Please Sign up or sign in to vote.
A wrapper class to use the visual style APIs available in Windows XP

Sample Image - xpvisualstyle.gif


This is a wrapper class to use the visual style APIs available in Windows XP. Visual style makes it possible to change the look and feel of all the "supported" applications. It is very easy to add support for visual styles in an application. Check on MSDN for more information.

However, if you plan to use any OWNERDRAW controls, you won't get the new look automatically. Windows is just not smart enough to know how your control should look. You have to make calls directly to the new UxTheme APIs.

It is quite simple to use the API, and in most cases you just need a few of them. The sample below draws a checked button in TOOLBAR style.

HTHEME hTheme = OpenThemeData(GetSafeHwnd(), L"TOOLBAR");
DrawThemeBackground(hTheme, pDC->GetSafeHdc(),TP_BUTTON, TS_CHECKED, &rc, 0);

Problems arise when you running the application under an earlier version of Windows, since calling these APIs directly makes your application dependent on the new DLLs which are not redistributable. The class provided in this article tries to solve this problem by wrapping the APIs and doing run-time linking. It is just a lot of copy-n-paste work, no fun at all. :)

Microsoft has actually done a thin wrapper in MFC 7.0 (winctrl3.cpp), but it only wraps a few of the APIs and they are mostly for MFC's internal usage. This class is based on the MFC implemenation and wraps the full set of visual style APIs from the Micrsoft Platform SDK August 2001. In order to compile this class in VC++ 6.0, you will need to have the latest Platform SDK, or at least one with the new XP headers. Under VC++ 7.0, no additional headers are required.

How to use

It is very simple to use this class. You need first to include the header, preferably in stdafx.h and add the CPP file to the project.

#include <span class="code-string">"VisualStylesXP.h"</span>

You can then either create a local CVisualStylesXP member and call the functions, or use the built-in global variable g_xpStyle.

HTHEME hTheme = g_xpStyle.OpenThemeData(GetSafeHwnd(), L"TOOLBAR");
g_xpStyle.DrawThemeBackground(hTheme, pDC->GetSafeHdc(), TP_BUTTON, TS_CHECKED, &rc, 0);

To make your application work under all windows versions, you should do something like this:

    if (g_xpStyle.IsAppThemed())
        HTHEME hTheme = g_xpStyle.OpenThemeData(GetSafeHwnd(), L"TOOLBAR");
        g_xpStyle.DrawThemeBackground(hTheme, pDC->GetSafeHdc(), 
                                            TP_BUTTON, TS_CHECKED, &rc, 0);


The demonstration application is a port from the ThemeExplorer application from MSDN.

That's all. Happy coding!


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


About the Author

David Y. Zhao
Web Developer
Sweden Sweden
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralEXPLORERBAR appears to be theme aware Pin
fred7918-Apr-08 9:09
memberfred7918-Apr-08 9:09 
QuestionCan also use /DELAYLOAD for this Pin
Paul Sanders (AlpineSoft)21-Oct-07 4:44
memberPaul Sanders (AlpineSoft)21-Oct-07 4:44 
Questionv2.0 net framework controls not displaying Pin
CE987618-May-07 0:33
memberCE987618-May-07 0:33 
Questionhelp with GUI repairs [modified] Pin
monographix19-Mar-07 8:06
membermonographix19-Mar-07 8:06 
Sorry to use this place to ask this. (i am not a coder)

I dont know what has messed up my win XP ui. I am suspecting GameXP did it probably, but although i did restore some glitches like images preview not working, filmstrip not working, jpg thumbnails not working, etc there is a number of other issues that i cant fix. Is there a way to repair / restore the windows XP GUI as a whole? (or at least the issues i mention below?)

I tried [sfc /scannow] and XP repair install with no results.

Here are some of the glitches i have found so far, i am sure there are more. Highlighted by the red outlines on the screenshots....[^][^][^]

- second color in title bar is off
- background color in top toolbars menu items switching colors abnormaly

also poster here:[^][^];#entry107546[^]

I already contacted the GameXP author to provide me with a list of registry alterations their program does but no response yet

just for the record other glitches i noticeably came across :
- one specific usb root hub or a device attached to it (joystick) needs to have its "allow pc to turn power off in this device" option unchecked in order hibernation to work properly (otherwise it would automatically resume back instead of shutting down)

- video files associations messed up especially optical media autorun acossiations related to cyberlink power dvd (could fix that installing again power dvd i guess?)

- strange dvd movies playback glitches: the monitor going blank reporting off range frequency or the whole desktop moving down the screen and losing the cursor or the bottom of the desktop (including the taskbar) being lost in a black horizontal zone .....

-- modified at 14:12 Monday 19th March, 2007
GeneralMaybe you know ... (Vista) Pin
c0d3Dr4g0n8-Oct-06 12:11
memberc0d3Dr4g0n8-Oct-06 12:11 
GeneralRe: Maybe you know ... (Vista) Pin
miloush.uam21-Jan-07 22:01
membermiloush.uam21-Jan-07 22:01 
GeneralRe: Maybe you know ... (Vista) Pin
Tim Stubbs26-Jan-07 0:57
memberTim Stubbs26-Jan-07 0:57 
GeneralGreat Pin
Sanbrother27-Aug-06 1:00
memberSanbrother27-Aug-06 1:00 
GeneralNot very useful for me Pin
René Greiner30-May-06 10:10
memberRené Greiner30-May-06 10:10 
GeneralTaskbar Pin
AndrewVos14-Mar-06 18:43
memberAndrewVos14-Mar-06 18:43 
QuestionWhich Theme have you used? Pin
ana_v1236-Aug-05 11:05
memberana_v1236-Aug-05 11:05 
GeneralTheme Background in explorer deskband Pin
Remco24-Feb-05 2:08
memberRemco24-Feb-05 2:08 
GeneralRe: Theme Background in explorer deskband Pin
fewfewfewfgwew9-Mar-05 9:06
memberfewfewfewfgwew9-Mar-05 9:06 
GeneralRe: Theme Background in explorer deskband Pin
Humberto16-Aug-05 12:39
memberHumberto16-Aug-05 12:39 
GeneralRe: Theme Background in explorer deskband Pin
yingyuan.cheng19-May-10 18:15
memberyingyuan.cheng19-May-10 18:15 
GeneralGetCurrentThemeName(...) doesn't work. Pin
Liran F.8-Oct-04 9:10
memberLiran F.8-Oct-04 9:10 
GeneralGetCurrentThemeName(...) doesn't work. Pin
Liran F.8-Oct-04 9:08
memberLiran F.8-Oct-04 9:08 
Questiongetthemefont... can you help me? Pin
mikebyte7-May-04 10:52
sussmikebyte7-May-04 10:52 
GeneralVery Nice...Question though... Pin
rossryan16-Mar-04 9:19
memberrossryan16-Mar-04 9:19 
QuestionCan i do the same thing with windows 98/ME Pin
burhankhan28-Nov-03 15:36
memberburhankhan28-Nov-03 15:36 
GeneralLoads of unused parameters in VisualStylesXP.h Pin
Yogurt24-Nov-03 1:05
memberYogurt24-Nov-03 1:05 
GeneralSome Controls Dont Appear... Pin
MealMate18-Nov-03 16:23
memberMealMate18-Nov-03 16:23 
GeneralRe: Some Controls Dont Appear... Pin
Hawkeye6424-Dec-03 5:43
memberHawkeye6424-Dec-03 5:43 
GeneralVery helpfull, but __stdcall key word is missing Pin
Thomas Haase13-Nov-03 4:10
memberThomas Haase13-Nov-03 4:10 
QuestionWhere is uxtheme.h? Pin
HDF SingKyo3-Oct-03 1:17
memberHDF SingKyo3-Oct-03 1:17 

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
Web01 | 2.8.170624.1 | Last Updated 23 Dec 2001
Article Copyright 2001 by David Y. Zhao
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid