Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C++

Add XP Visual Style Support to OWNERDRAW Controls

Rate me:
Please Sign up or sign in to vote.
4.95/5 (40 votes)
22 Dec 20012 min read 381K   9.4K   156   67
Wrapper class that uses visual style APIs available in WinXP
In this article, you will see a wrapper class to use the visual style APIs available in Windows XP.

Sample Image - xpvisualstyle.gif

Introduction

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.

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

Problems arise when you run 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 implementation and wraps the full set of visual style APIs from the Microsoft 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.

C++
#include "VisualStylesXP.h"

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

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

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

C++
#ifdef _VISUALSTYLE_XP_H_
    if (g_xpStyle.IsAppThemed())
    {
        HTHEME hTheme = g_xpStyle.OpenThemeData(GetSafeHwnd(), L"TOOLBAR");
        g_xpStyle.DrawThemeBackground(hTheme, pDC->GetSafeHdc(), 
                                            TP_BUTTON, TS_CHECKED, &rc, 0);
        g_xpStyle.CloseThemeData(hTheme);
    }
    else
    {
#endif
    pDC->DrawEdge(....);
#ifdef _VISUALSTYLE_XP_H_
    }
#endif

Copyright

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

That's all. Happy coding!

History

  • 23rd December, 2001: Initial version

License

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.


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeed to use this component in my employer's application Pin
Member 220527429-Mar-23 10:19
Member 220527429-Mar-23 10:19 
GeneralEXPLORERBAR appears to be theme aware Pin
fred7918-Apr-08 9:09
fred7918-Apr-08 9:09 
QuestionCan also use /DELAYLOAD for this Pin
Paul Sanders (the other one)21-Oct-07 4:44
Paul Sanders (the other one)21-Oct-07 4:44 
Questionv2.0 net framework controls not displaying Pin
CE987618-May-07 0:33
CE987618-May-07 0:33 
Questionhelp with GUI repairs [modified] Pin
monographix19-Mar-07 8:06
monographix19-Mar-07 8:06 
GeneralMaybe you know ... (Vista) Pin
c0d3Dr4g0n8-Oct-06 12:11
c0d3Dr4g0n8-Oct-06 12:11 
GeneralRe: Maybe you know ... (Vista) Pin
Jan Kučera21-Jan-07 22:01
Jan Kučera21-Jan-07 22:01 
GeneralRe: Maybe you know ... (Vista) Pin
Tim Stubbs26-Jan-07 0:57
Tim Stubbs26-Jan-07 0:57 
GeneralGreat Pin
Sanbrother27-Aug-06 1:00
Sanbrother27-Aug-06 1:00 
GeneralNot very useful for me Pin
René Greiner30-May-06 10:10
René Greiner30-May-06 10:10 
GeneralTaskbar Pin
AndrewVos14-Mar-06 18:43
AndrewVos14-Mar-06 18:43 
QuestionWhich Theme have you used? Pin
ana_v1236-Aug-05 11:05
ana_v1236-Aug-05 11:05 
GeneralTheme Background in explorer deskband Pin
Remco24-Feb-05 2:08
Remco24-Feb-05 2:08 
GeneralRe: Theme Background in explorer deskband Pin
fewfewfewfgwew9-Mar-05 9:06
fewfewfewfgwew9-Mar-05 9:06 
GeneralRe: Theme Background in explorer deskband Pin
Humberto16-Aug-05 12:39
Humberto16-Aug-05 12:39 
GeneralRe: Theme Background in explorer deskband Pin
yingyuan.cheng19-May-10 18:15
yingyuan.cheng19-May-10 18:15 
GeneralGetCurrentThemeName(...) doesn't work. Pin
Liran F.8-Oct-04 9:10
Liran F.8-Oct-04 9:10 
GeneralGetCurrentThemeName(...) doesn't work. Pin
Liran F.8-Oct-04 9:08
Liran F.8-Oct-04 9:08 
Questiongetthemefont... can you help me? Pin
mikebyte7-May-04 10:52
mikebyte7-May-04 10:52 
GeneralVery Nice...Question though... Pin
rossryan16-Mar-04 9:19
rossryan16-Mar-04 9:19 
QuestionCan i do the same thing with windows 98/ME Pin
burhankhan28-Nov-03 15:36
burhankhan28-Nov-03 15:36 
GeneralLoads of unused parameters in VisualStylesXP.h Pin
Yogurt24-Nov-03 1:05
Yogurt24-Nov-03 1:05 
GeneralSome Controls Dont Appear... Pin
MealMate18-Nov-03 16:23
MealMate18-Nov-03 16:23 
GeneralRe: Some Controls Dont Appear... Pin
Hawkeye6424-Dec-03 5:43
Hawkeye6424-Dec-03 5:43 
GeneralVery helpfull, but __stdcall key word is missing Pin
Thomas Haase13-Nov-03 4:10
Thomas Haase13-Nov-03 4:10 

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.