Click here to Skip to main content
Click here to Skip to main content

Add XP Visual Style Support to OWNERDRAW Controls

By , 22 Dec 2001
 

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. :)

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 "VisualStylesXP.h"

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)

About the Author

David Y. Zhao
Web Developer
Sweden Sweden
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralEXPLORERBAR appears to be theme awarememberfred7918 Apr '08 - 9:09 
QuestionCan also use /DELAYLOAD for thismemberPaul Sanders (AlpineSoft)21 Oct '07 - 4:44 
Questionv2.0 net framework controls not displayingmemberCE987618 May '07 - 0:33 
Questionhelp with GUI repairs [modified]membermonographix19 Mar '07 - 8:06 
GeneralMaybe you know ... (Vista)memberc0d3Dr4g0n8 Oct '06 - 12:11 
GeneralRe: Maybe you know ... (Vista)membermiloush.uam21 Jan '07 - 22:01 
I have no troubles running this example on Vista RC2.
GeneralRe: Maybe you know ... (Vista)memberTim Stubbs26 Jan '07 - 0:57 
GeneralGreatmemberSanbrother27 Aug '06 - 1:00 
GeneralNot very useful for mememberRené Greiner30 May '06 - 10:10 
GeneralTaskbarmemberAndrewVos14 Mar '06 - 18:43 
QuestionWhich Theme have you used?memberana_v1236 Aug '05 - 11:05 
GeneralTheme Background in explorer deskbandmemberRemco24 Feb '05 - 2:08 
GeneralRe: Theme Background in explorer deskbandmemberfewfewfewfgwew9 Mar '05 - 9:06 
GeneralRe: Theme Background in explorer deskbandmemberHumberto16 Aug '05 - 12:39 
GeneralRe: Theme Background in explorer deskbandmemberyingyuan.cheng19 May '10 - 18:15 
GeneralGetCurrentThemeName(...) doesn't work.memberLiran F.8 Oct '04 - 9:10 
GeneralGetCurrentThemeName(...) doesn't work.memberLiran F.8 Oct '04 - 9:08 
Questiongetthemefont... can you help me?sussmikebyte7 May '04 - 10:52 
GeneralVery Nice...Question though...memberrossryan16 Mar '04 - 9:19 
QuestionCan i do the same thing with windows 98/MEmemberburhankhan28 Nov '03 - 15:36 
GeneralLoads of unused parameters in VisualStylesXP.hmemberYogurt24 Nov '03 - 1:05 
GeneralSome Controls Dont Appear...memberMealMate18 Nov '03 - 16:23 
GeneralRe: Some Controls Dont Appear...memberHawkeye6424 Dec '03 - 5:43 
GeneralVery helpfull, but __stdcall key word is missingmemberThomas Haase13 Nov '03 - 4:10 
QuestionWhere is uxtheme.h?memberHDF SingKyo3 Oct '03 - 1:17 
AnswerRe: Where is uxtheme.h?memberHDF SingKyo3 Oct '03 - 1:19 
GeneralRe: Where is uxtheme.h?sussAnonymous21 Nov '03 - 0:42 
GeneralRe: Where is uxtheme.h?sussAnonymous21 Nov '03 - 0:42 
AnswerRe: Where is uxtheme.h?memberY. S. Jung24 Oct '05 - 3:10 
GeneralVery useful code !sussStanislav Panasik2 Sep '03 - 19:30 
GeneralRe: Very useful code !membersbuonocore22 Jan '04 - 21:43 
Generalsorry,the demo can not run,pls help mememberNickTang7 Apr '03 - 22:21 
Generaloffice 2003 (office11) visual styles!memberschaereran@gmx.net7 Apr '03 - 2:01 
QuestionHow can I detect if XP styles are available?memberBodekaer17 Feb '03 - 23:07 
AnswerRe: How can I detect if XP styles are available?membereastar09213 Sep '04 - 17:48 
GeneralBeware font types in Theme APImemberSashkaP25 Nov '02 - 6:22 
GeneralRe: Beware font types in Theme APImemberHawkeye6424 Dec '03 - 5:45 
GeneralWindows XP menu visual stylesussAnonymous7 Aug '02 - 8:06 
GeneralCannot buildmemberdva_dev17 Jun '02 - 4:45 
GeneralTRAYNOTIFY and CLOCK classmemberAnonymous19 Mar '02 - 13:40 
GeneralRe: TRAYNOTIFY and CLOCK classmemberZero_divide_118 May '03 - 9:40 
GeneralOpenThemeData for main windowmemberkasturirawat7 Feb '02 - 20:43 
GeneralRe: OpenThemeData for main windowmemberVenkatesan Murugesan13 Feb '02 - 20:21 
GeneralRe: OpenThemeData for main windowsussAnonymous2 Aug '02 - 3:10 
QuestionButtons?memberswinefeaster5 Feb '02 - 21:05 
AnswerRe: Buttons?memberVenkatesan Murugesan7 Feb '02 - 9:58 
GeneralRe: Buttons?memberswinefeaster7 Feb '02 - 10:13 
QuestionHow about plateforms other than XP?memberyellowine31 Jan '02 - 3:39 
AnswerRe: How about plateforms other than XP?memberswinefeaster6 Feb '02 - 22:12 
GeneralCode optimizationmemberPål K Tønder24 Jan '02 - 3:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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