Click here to Skip to main content
6,595,854 members and growing! (18,267 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Win32/64 SDK & OS » General     Intermediate License: The Code Project Open License (CPOL)

Add XP Visual Style Support to OWNERDRAW Controls

By David Y. Zhao

A wrapper class to use the visual style APIs available in Windows XP
VC6, VC7WinXP, Dev
Posted:22 Dec 2001
Views:250,036
Bookmarked:126 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
46 votes for this article.
Popularity: 7.77 Rating: 4.67 out of 5
3 votes, 7.9%
1

2
1 vote, 2.6%
3
2 votes, 5.3%
4
32 votes, 84.2%
5

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


Member

Occupation: Web Developer
Location: Sweden Sweden

Other popular Win32/64 SDK & OS articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 64 (Total in Forum: 64) (Refresh)FirstPrevNext
GeneralEXPLORERBAR appears to be theme aware Pinmemberfred7910:09 18 Apr '08  
GeneralCan also use /DELAYLOAD for this PinmemberPaul Sanders (AlpineSoft)5:44 21 Oct '07  
Questionv2.0 net framework controls not displaying PinmemberCE98761:33 18 May '07  
Questionhelp with GUI repairs [modified] Pinmembermonographix9:06 19 Mar '07  
GeneralMaybe you know ... (Vista) Pinmemberc0d3Dr4g0n13:11 8 Oct '06  
GeneralRe: Maybe you know ... (Vista) Pinmembermiloush.uam23:01 21 Jan '07  
GeneralRe: Maybe you know ... (Vista) PinmemberTim Stubbs1:57 26 Jan '07  
GeneralGreat PinmemberSanbrother2:00 27 Aug '06  
GeneralNot very useful for me PinmemberRené Greiner11:10 30 May '06  
GeneralTaskbar PinmemberAndrewVos19:43 14 Mar '06  
GeneralWhich Theme have you used? Pinmemberana_v12312:05 6 Aug '05  
GeneralTheme Background in explorer deskband PinmemberRemco3:08 24 Feb '05  
GeneralRe: Theme Background in explorer deskband Pinmemberfewfewfewfgwew10:06 9 Mar '05  
GeneralRe: Theme Background in explorer deskband PinmemberHumberto13:39 16 Aug '05  
GeneralGetCurrentThemeName(...) doesn't work. PinmemberLiran F.10:10 8 Oct '04  
GeneralGetCurrentThemeName(...) doesn't work. PinmemberLiran F.10:08 8 Oct '04  
Generalgetthemefont... can you help me? Pinsussmikebyte11:52 7 May '04  
GeneralVery Nice...Question though... Pinmemberrossryan10:19 16 Mar '04  
GeneralCan i do the same thing with windows 98/ME Pinmemberburhankhan16:36 28 Nov '03  
GeneralLoads of unused parameters in VisualStylesXP.h PinmemberYogurt2:05 24 Nov '03  
GeneralSome Controls Dont Appear... PinmemberMealMate17:23 18 Nov '03  
GeneralRe: Some Controls Dont Appear... PinmemberHawkeye646:43 24 Dec '03  
GeneralVery helpfull, but __stdcall key word is missing PinmemberThomas Haase5:10 13 Nov '03  
GeneralWhere is uxtheme.h? PinmemberHDF SingKyo2:17 3 Oct '03  
GeneralRe: Where is uxtheme.h? PinmemberHDF SingKyo2:19 3 Oct '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Dec 2001
Editor: Chris Maunder
Copyright 2001 by David Y. Zhao
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project