Click here to Skip to main content
Licence CPOL
First Posted 22 Dec 2001
Views 291,236
Downloads 6,854
Bookmarked 145 times

Add XP Visual Style Support to OWNERDRAW Controls

By | 22 Dec 2001 | Article
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. :)

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



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEXPLORERBAR appears to be theme aware Pinmemberfred799:09 18 Apr '08  
QuestionCan also use /DELAYLOAD for this PinmemberPaul Sanders (AlpineSoft)4:44 21 Oct '07  
Another way to ensure that your application still runs on older versions of Windows while retaining the convenience of calling the theme functions directly is to use VC's DELAYLOAD feature (pass /DELAYLOAD:uxtheme.dll to the linker). Then, either test whether you are running on XP (or later) before calling any theme functions or catch the resulting exception on other platforms when you do.
 

Questionv2.0 net framework controls not displaying PinmemberCE98760:33 18 May '07  
Questionhelp with GUI repairs [modified] Pinmembermonographix8:06 19 Mar '07  
GeneralMaybe you know ... (Vista) Pinmemberc0d3Dr4g0n12:11 8 Oct '06  
GeneralRe: Maybe you know ... (Vista) Pinmembermiloush.uam22:01 21 Jan '07  
GeneralRe: Maybe you know ... (Vista) PinmemberTim Stubbs0:57 26 Jan '07  
GeneralGreat PinmemberSanbrother1:00 27 Aug '06  
GeneralNot very useful for me PinmemberRené Greiner10:10 30 May '06  
GeneralTaskbar PinmemberAndrewVos18:43 14 Mar '06  
QuestionWhich Theme have you used? Pinmemberana_v12311:05 6 Aug '05  
GeneralTheme Background in explorer deskband PinmemberRemco2:08 24 Feb '05  
GeneralRe: Theme Background in explorer deskband Pinmemberfewfewfewfgwew9:06 9 Mar '05  
GeneralRe: Theme Background in explorer deskband PinmemberHumberto12:39 16 Aug '05  
GeneralRe: Theme Background in explorer deskband Pinmemberyingyuan.cheng18:15 19 May '10  
GeneralGetCurrentThemeName(...) doesn't work. PinmemberLiran F.9:10 8 Oct '04  
GeneralGetCurrentThemeName(...) doesn't work. PinmemberLiran F.9:08 8 Oct '04  
Questiongetthemefont... can you help me? Pinsussmikebyte10:52 7 May '04  
GeneralVery Nice...Question though... Pinmemberrossryan9:19 16 Mar '04  
QuestionCan i do the same thing with windows 98/ME Pinmemberburhankhan15:36 28 Nov '03  
GeneralLoads of unused parameters in VisualStylesXP.h PinmemberYogurt1:05 24 Nov '03  
GeneralSome Controls Dont Appear... PinmemberMealMate16:23 18 Nov '03  
GeneralRe: Some Controls Dont Appear... PinmemberHawkeye645:43 24 Dec '03  
GeneralVery helpfull, but __stdcall key word is missing PinmemberThomas Haase4:10 13 Nov '03  
QuestionWhere is uxtheme.h? PinmemberHDF SingKyo1:17 3 Oct '03  

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.

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