Click here to Skip to main content
13,193,056 members (82,173 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

GeneralCannot build Pin
dva_dev17-Jun-02 4:45
memberdva_dev17-Jun-02 4:45 
GeneralTRAYNOTIFY and CLOCK class Pin
Anonymous19-Mar-02 13:40
memberAnonymous19-Mar-02 13:40 
GeneralRe: TRAYNOTIFY and CLOCK class Pin
Zero_divide_118-May-03 9:40
memberZero_divide_118-May-03 9:40 
GeneralOpenThemeData for main window Pin
kasturirawat7-Feb-02 20:43
memberkasturirawat7-Feb-02 20:43 
GeneralRe: OpenThemeData for main window Pin
Venkatesan Murugesan13-Feb-02 20:21
memberVenkatesan Murugesan13-Feb-02 20:21 
GeneralRe: OpenThemeData for main window Pin
Anonymous2-Aug-02 3:10
sussAnonymous2-Aug-02 3:10 
QuestionButtons? Pin
swinefeaster5-Feb-02 21:05
memberswinefeaster5-Feb-02 21:05 
AnswerRe: Buttons? Pin
Venkatesan Murugesan7-Feb-02 9:58
memberVenkatesan Murugesan7-Feb-02 9:58 
GeneralRe: Buttons? Pin
swinefeaster7-Feb-02 10:13
memberswinefeaster7-Feb-02 10:13 
QuestionHow about plateforms other than XP? Pin
yellowine31-Jan-02 3:39
memberyellowine31-Jan-02 3:39 
AnswerRe: How about plateforms other than XP? Pin
swinefeaster6-Feb-02 22:12
memberswinefeaster6-Feb-02 22:12 
GeneralCode optimization Pin
Pål K Tønder24-Jan-02 3:22
memberPål K Tønder24-Jan-02 3:22 
GeneralWrapping <tt>HTHEME</tt> Pin
Thomas Freudenberg22-Jan-02 22:18
memberThomas Freudenberg22-Jan-02 22:18 
GeneralTypos Pin
Yogurt24-Nov-03 1:00
memberYogurt24-Nov-03 1:00 
You'd write
[__b__]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 Bhat4-Jan-10 23:30
memberPrasanna Bhat4-Jan-10 23:30 
GeneralGreat! Pin
Davide Calabro9-Jan-02 4:54
memberDavide Calabro9-Jan-02 4:54 
GeneralGetThemeColor Pin
Christopher Canossi1-Jan-02 9:52
memberChristopher Canossi1-Jan-02 9:52 
GeneralRe: GetThemeColor Pin
vvab6-Jan-02 8:38
membervvab6-Jan-02 8:38 
GeneralRe: GetThemeColor Pin
Venkatesan Murugesan5-Feb-02 19:04
memberVenkatesan Murugesan5-Feb-02 19:04 
GeneralRe: GetThemeColor Pin
Christopher Canossi17-Feb-02 13:26
memberChristopher Canossi17-Feb-02 13:26 
GeneralRe: GetThemeColor Pin
Anonymous22-Feb-02 12:48
memberAnonymous22-Feb-02 12:48 
GeneralRe: GetThemeColor Pin
Steve Stansbury2-Jan-03 6:45
sussSteve Stansbury2-Jan-03 6:45 
GeneralRe: GetThemeColor Pin
Tim Stubbs17-Jul-03 1:49
memberTim Stubbs17-Jul-03 1:49 
GeneralRe: GetThemeColor Pin
Anonymous6-May-04 11:59
sussAnonymous6-May-04 11:59 
GeneralDouble call to OpenThemeData() Pin
Matthias Mann25-Dec-01 1:02
memberMatthias Mann25-Dec-01 1:02 
GeneralRe: Double call to OpenThemeData() Pin
David Yuheng Zhao25-Dec-01 3:05
memberDavid Yuheng Zhao25-Dec-01 3:05 
GeneralRe: Double call to OpenThemeData() Pin
George Clarence27-Dec-01 7:29
memberGeorge Clarence27-Dec-01 7:29 
GeneralRe: Double call to OpenThemeData() Pin
Matthias Mann27-Dec-01 9:37
memberMatthias Mann27-Dec-01 9: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
Web03 | 2.8.171017.2 | Last Updated 23 Dec 2001
Article Copyright 2001 by David Y. Zhao
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid