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

Add XP Visual Style Support to OWNERDRAW Controls

, 22 Dec 2001 CPOL
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

Comments and Discussions

 
GeneralVery Nice...Question though... Pinmemberrossryan16-Mar-04 10:19 

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.141216.1 | Last Updated 23 Dec 2001
Article Copyright 2001 by David Y. Zhao
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid