Click here to Skip to main content
11,929,675 members (59,219 online)
Click here to Skip to main content
Add your own
alternative version


155 bookmarked

Add XP Visual Style Support to OWNERDRAW Controls

, 22 Dec 2001 CPOL
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. Smile | :)

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

QuestionButtons? Pin
swinefeaster5-Feb-02 22:05
memberswinefeaster5-Feb-02 22:05 
AnswerRe: Buttons? Pin
Venkatesan Murugesan7-Feb-02 10:58
memberVenkatesan Murugesan7-Feb-02 10:58 
GeneralRe: Buttons? Pin
swinefeaster7-Feb-02 11:13
memberswinefeaster7-Feb-02 11:13 
QuestionHow about plateforms other than XP? Pin
yellowine31-Jan-02 4:39
memberyellowine31-Jan-02 4:39 
AnswerRe: How about plateforms other than XP? Pin
swinefeaster6-Feb-02 23:12
memberswinefeaster6-Feb-02 23:12 
GeneralCode optimization Pin
Pål K Tønder24-Jan-02 4:22
memberPål K Tønder24-Jan-02 4:22 
GeneralWrapping <tt>HTHEME</tt> Pin
Thomas Freudenberg22-Jan-02 23:18
memberThomas Freudenberg22-Jan-02 23:18 
GeneralTypos Pin
Yogurt24-Nov-03 2:00
memberYogurt24-Nov-03 2:00 
You'd write
m_hTheme = m_StylesXP.OpenThemeData (hWnd, pszClassList);
operator HTHEME() { return m_hTheme; }
in order to compile it.

And of course, you'd put a semicolon at the end of the class declaration.
GeneralRe: Wrapping HTHEME Pin
Prasanna Bhat5-Jan-10 0:30
memberPrasanna Bhat5-Jan-10 0:30 
GeneralGreat! Pin
Davide Calabro9-Jan-02 5:54
memberDavide Calabro9-Jan-02 5:54 
GeneralGetThemeColor Pin
Christopher Canossi1-Jan-02 10:52
memberChristopher Canossi1-Jan-02 10:52 
GeneralRe: GetThemeColor Pin
vvab6-Jan-02 9:38
membervvab6-Jan-02 9:38 
GeneralRe: GetThemeColor Pin
Venkatesan Murugesan5-Feb-02 20:04
memberVenkatesan Murugesan5-Feb-02 20:04 
GeneralRe: GetThemeColor Pin
Christopher Canossi17-Feb-02 14:26
memberChristopher Canossi17-Feb-02 14:26 
GeneralRe: GetThemeColor Pin
Anonymous22-Feb-02 13:48
memberAnonymous22-Feb-02 13:48 
GeneralRe: GetThemeColor Pin
Steve Stansbury2-Jan-03 7:45
sussSteve Stansbury2-Jan-03 7:45 
GeneralRe: GetThemeColor Pin
Tim Stubbs17-Jul-03 2:49
memberTim Stubbs17-Jul-03 2:49 
GeneralRe: GetThemeColor Pin
Anonymous6-May-04 12:59
sussAnonymous6-May-04 12:59 
GeneralDouble call to OpenThemeData() Pin
Matthias Mann25-Dec-01 2:02
memberMatthias Mann25-Dec-01 2:02 
GeneralRe: Double call to OpenThemeData() Pin
David Yuheng Zhao25-Dec-01 4:05
memberDavid Yuheng Zhao25-Dec-01 4:05 
GeneralRe: Double call to OpenThemeData() Pin
George Clarence27-Dec-01 8:29
memberGeorge Clarence27-Dec-01 8:29 
GeneralRe: Double call to OpenThemeData() Pin
Matthias Mann27-Dec-01 10:37
memberMatthias Mann27-Dec-01 10: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 23 Dec 2001
Article Copyright 2001 by David Y. Zhao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid