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

 
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 
I was very pleasantly surprised to see EXPLORERBAR responding to theme changes (for example Homestead and Silver on XP). I know at one time uxtheme.dll wasn't fully supporting various themes for EXPLORERBAR components. Developers had to dig through ShellStyles.dll to find the appropriate bitmaps, etc.
 
When did this change?
QuestionCan also use /DELAYLOAD for thismemberPaul Sanders (AlpineSoft)21 Oct '07 - 4:44 
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 displayingmemberCE987618 May '07 - 0:33 
I'm new to this code, just taken over from someone else, but I'm getting a problem after updating my c# code to v2.0 .net framework. The check box control doesn't draw properly on the form unless you enlarge the form. This works fine in v1.1 .net framework. Has anyone any ideas why this is the case or the areas to investigate?
Questionhelp with GUI repairs [modified]membermonographix19 Mar '07 - 8:06 
Sorry to use this place to ask this. (i am not a coder)
 
I dont know what has messed up my win XP ui. I am suspecting GameXP did it probably, but although i did restore some glitches like images preview not working, filmstrip not working, jpg thumbnails not working, etc there is a number of other issues that i cant fix. Is there a way to repair / restore the windows XP GUI as a whole? (or at least the issues i mention below?)
 
I tried [sfc /scannow] and XP repair install with no results.
 
Here are some of the glitches i have found so far, i am sure there are more. Highlighted by the red outlines on the screenshots....
 
http://www.ideart.gr/delete/XP_GUI_2.jpg[^]
http://www.ideart.gr/delete/XP_GUI_1.jpg[^]
http://www.ideart.gr/delete/XP_GUI_3.jpg[^]
 

- second color in title bar is off
- background color in top toolbars menu items switching colors abnormaly
 
also poster here:
http://www.5starsupport.com/ipboard/index.php?showtopic=6708[^]
http://forum.tweaks.com/forum/Topic218015-7-1.aspx[^]
http://www.flexbeta.net/forums/index.php?s...mp;#entry107546[^]
 
I already contacted the GameXP author to provide me with a list of registry alterations their program does but no response yet
 

 
just for the record other glitches i noticeably came across :
- one specific usb root hub or a device attached to it (joystick) needs to have its "allow pc to turn power off in this device" option unchecked in order hibernation to work properly (otherwise it would automatically resume back instead of shutting down)
 
- video files associations messed up especially optical media autorun acossiations related to cyberlink power dvd (could fix that installing again power dvd i guess?)
 
- strange dvd movies playback glitches: the monitor going blank reporting off range frequency or the whole desktop moving down the screen and losing the cursor or the bottom of the desktop (including the taskbar) being lost in a black horizontal zone .....
 

 

 
-- modified at 14:12 Monday 19th March, 2007
GeneralMaybe you know ... (Vista)memberc0d3Dr4g0n8 Oct '06 - 12:11 
Hello,
 
Excellent, succinct example. As you know, the XP theme library is called uxtheme.dll. Would you know of the vista theme dll yet? Any plans to migrate this project to a vista theme-capable solution?
 
Many thanks,
 
c0d3Dr4g0n
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 
Yes it runs but it's doesn't show the new parts and states in Vista. There is a new file "vssym32.h" which is included in preference to "tmschema.h" (obsolete) under Vista - install the Vista SDK to see it.

 
Tim Stubbs

GeneralGreatmemberSanbrother27 Aug '06 - 1:00 
Oh, this is really a great job.
Thank you.
GeneralNot very useful for mememberRené Greiner30 May '06 - 10:10 
I just see a simple class with wraps simple C API into a class namespace without any C++ nor compiler advantages.
 
1. The code could be inlined without increasing the build size. That way it just reduces performance and increases link time.
2. Calling GetProcAddress everytimes the wrapper function is called is a very slow thing. It won't take much memory to buffer these function pointers.
3. It would be good to wrap the HTHEME handle into an auto_ptr class to be exceptionsafe and its destructor will automatically close the opened theme.

GeneralTaskbarmemberAndrewVos14 Mar '06 - 18:43 
Can anyone find the taskbar window buttons? The ones with the icon and text of the running windows, I have no idea what they're called Big Grin | :-D
 


All your source are belong to us!

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.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