Click here to Skip to main content
11,641,668 members (45,558 online)
Click here to Skip to main content

Add XP Visual Style Support to OWNERDRAW Controls

, 22 Dec 2001 CPOL 320.9K 8.4K 155
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

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.

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

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);
g_xpStyle.CloseThemeData(hTheme);

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

#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!

License

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

Share

About the Author

David Y. Zhao
Web Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: Where is uxtheme.h? Pin
Anonymous21-Nov-03 0:42
sussAnonymous21-Nov-03 0:42 
AnswerRe: Where is uxtheme.h? Pin
Y. S. Jung24-Oct-05 3:10
memberY. S. Jung24-Oct-05 3:10 
GeneralVery useful code ! Pin
Stanislav Panasik2-Sep-03 19:30
sussStanislav Panasik2-Sep-03 19:30 
GeneralRe: Very useful code ! Pin
sbuonocore22-Jan-04 21:43
membersbuonocore22-Jan-04 21:43 
Generalsorry,the demo can not run,pls help me Pin
NickTang7-Apr-03 22:21
memberNickTang7-Apr-03 22:21 
Generaloffice 2003 (office11) visual styles! Pin
schaereran@gmx.net7-Apr-03 2:01
memberschaereran@gmx.net7-Apr-03 2:01 
QuestionHow can I detect if XP styles are available? Pin
Bodekaer17-Feb-03 23:07
memberBodekaer17-Feb-03 23:07 
Hi,

How can I detect if XP styles are available?

I just want to know if the computer is Windows XP, and if it uses the standard XP style, og the old windows 2000 look.

Thanks in advance,

Michael
AnswerRe: How can I detect if XP styles are available? Pin
eastar09213-Sep-04 17:48
membereastar09213-Sep-04 17:48 
GeneralBeware font types in Theme API Pin
SashkaP25-Nov-02 6:22
memberSashkaP25-Nov-02 6:22 
GeneralRe: Beware font types in Theme API Pin
Hawkeye6424-Dec-03 5:45
memberHawkeye6424-Dec-03 5:45 
GeneralWindows XP menu visual style Pin
Anonymous7-Aug-02 8:06
sussAnonymous7-Aug-02 8:06 
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 
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    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
Web02 | 2.8.150731.1 | Last Updated 23 Dec 2001
Article Copyright 2001 by David Y. Zhao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid