Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C++

Adding XP Visual Style Support to OWNERDRAW Controls using HTHEME Wrapper

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
29 Jan 20023 min read 156.8K   999   25   36
A wrapper class to use the visual styles APIs available in Windows XP
In this article, you will see a wrapper class for the HTHEME handle used in connection with the Visual Styles API available in Windows XP. The class is heavily based on the CVisualStylesXP class by David Yuheng Zhao.

Introduction

This is a wrapper class for the HTHEME handle used in connection with the Visual Styles API available in Windows XP. This article and the code provided is heavily based on Add XP Visual Style Support in OWNERDRAW Controls by David Yuheng Zhao. To understand this article and the code provided, I strongly recommend that you read that article.

The class provided is based on the CVisualStylesXP class described in the previously mentioned article, indeed several portions of the code are copied directly, but some major conceptual changes have been made. Instead of the handle to the uxtheme.dll being a normal member of the CVisualStylesXP class, it has been made static in CXPTheme, resulting in a single load per application. Also, the function pointers returned by GetProcAddress are cached in static function pointers, which are then used in subsequent calls. In the situation where the DLL is not available, the addresses of special failure member methods providing reasonable responses are assigned to these static function pointers instead. This results in a highly optimized execution.

A more significant difference for the users of this class is the fact that CXPTheme wraps the HTHEME handle. This provides a more object-oriented approach to using the API. Unless specifically needed, the user never interacts directly with the HTHEME handle. Instead, the handle is kept in a member variable and internally applied where necessary. The handle is closed in the class destructor. The class provides several constructors and overloaded operators for ease of use.

The API functions not directly related to a HTHEME are created as static member methods, not requiring an instance of the class. In addition, the static member method IsAvailable has been added for a specific check on whether the current platform supports theming. In most cases, it will suffice to use the IsAppThemed API function because the corresponding special failure method will return FALSE on platforms which do not provide theming, signalling "old-style" drawing.

How to Use

The class is very easy to use. First, you need to include the header, preferably in stdafx.h and add the CPP file in the project.

C++
#include "XPTheme.h"

Then you can either create a local CXPTheme variable where needed, or add it as a class member of the control.

C++
CXPTheme theme(GetSafeHwnd(), L"TOOLBAR");
theme.DrawBackground(pDC->GetSafeHdc(), TP_BUTTON, TS_CHECKED, &rc, 0);
// the handle is closed when the variable goes out of scope.

To make your application work under all Windows versions, you should do something like this:

C++
#ifdef _XPTHEME_H_
    if (CXPTheme::IsAppThemed())
    {
        CXPTheme theme(GetSafeHwnd(), L"TOOLBAR");
        theme.DrawBackground(pDC->GetSafeHdc(), TP_BUTTON, TS_CHECKED, &rc, 0);
    }
    else
    {
#endif
        pDC->DrawEdge(....);
#ifdef _XPTHEME_H_
    }
#endif

The preferred way to use this class would probably be to make it a control member variable which is initialized in the control constructor. Once the control is deleted, the CXPTheme variable will be deleted as well, and the handle closed safely. The control should handle the new WM_THEMECHANGED message, and close and reopen the theme using the Close and Open member methods. Actually, the Open method could be used alone, because an open handle is closed automatically before opening a new one.

C++
case WM_THEMECHANGED:
    m_theme.Close();
    m_theme.Open(GetSafeHwnd(), L"TOOLBAR");
    break;

The download includes the class itself in addition to three header files taken from the Platform SDK. These are necessary to compile the code without the latest SDK. For the same reason, a typedef for the WM_THEMECHANGED message has been added to the XPTheme.h file.

History

  • 30th January, 2002: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Works on all versions of Windows or Not? Pin
Pål K Tønder5-Feb-02 21:43
Pål K Tønder5-Feb-02 21:43 
GeneralRe: Works on all versions of Windows or Not? Pin
Swinefeaster6-Feb-02 9:14
Swinefeaster6-Feb-02 9:14 
GeneralRe: Works on all versions of Windows or Not? Pin
Pål K Tønder6-Feb-02 20:43
Pål K Tønder6-Feb-02 20:43 
GeneralA demo project wanted Pin
yellowine31-Jan-02 3:29
yellowine31-Jan-02 3:29 
GeneralRe: A demo project wanted Pin
Andreas Saurwein31-Jan-02 23:34
Andreas Saurwein31-Jan-02 23:34 
GeneralRe: A demo project wanted Pin
Pål K Tønder4-Feb-02 21:44
Pål K Tønder4-Feb-02 21:44 
QuestionWhat differences? Pin
Davide Calabro31-Jan-02 1:54
Davide Calabro31-Jan-02 1:54 
AnswerRe: What differences? Pin
Pål K Tønder31-Jan-02 2:37
Pål K Tønder31-Jan-02 2:37 
There are several differences.

As I see it the class provided by David Yuheng Zhao, CVisualStylesXP, is just a thin layer over the C API, not really taking advantage of the benefits of object-oriented programming. You have to handle the HTHEME the same as if you used the C API directly, open it, send it in to every method, and closing it afterwards. Only in the sense that it hides the loading of the dll and the use of GetProcAdress does it differ from using the API directly.

My class CXPTheme also hides the dll handling, but optimizes it considerably by caching the function pointers. CVisualStylesXP calls GetProcAdress EVERY TIME an API function is called, my class does it once per function. Also, the dll handle is static, ensuring only one loading of the dll. The CVisualStylesXP class tries to load it for each instance.

More importantly though, is the focus on objectifying the API by hiding the HTHEME handle. There is nothing magic about OO other than it makes programming easier (and more fun). By wrapping the HTHEME handle one takes advantage of some of the virtues of OO. Consider some code throwing an exception. Using local variables of each class, my class would automatically close the HTHEME handle, while CVisualStylesXP would leak resources. Also, the fact that the HTHEME handle is treated as an object instead of an opaque entity makes it fit better with the rest of the OO code.

The main conclusion is that the classes have different focal points. CVisualStylesXP focuses on making sure the code can run on several platforms. CXPTheme focuses on giving an object-oriented version of the Theming API. It also has the added benefit of being able to run on several platforms.

This you can really see by inspecting the data members of the classes. The only data member in CVisualStylesXP is the dll handle. Without the cross-platform code it would not be a true class, it would not have data members. If you on the other hand removed the cross-platform code from my class, confining it to XP only, it would still be a true class providing some benefit, however small, to an C++ programmer, namely treating themes as objects. I do recognize, however, that the true benefit of the class is the combination of OO and cross-platform abilility. As such, I consider that my class is one up on CVisualStylesXP. Big Grin | :-D
GeneralRe: What differences? Pin
Venkatesan Murugesan31-Jan-02 13:25
Venkatesan Murugesan31-Jan-02 13:25 
GeneralRe: What differences? Pin
Andreas Saurwein31-Jan-02 23:32
Andreas Saurwein31-Jan-02 23:32 
GeneralRe: What differences? Pin
Pål K Tønder3-Feb-02 21:07
Pål K Tønder3-Feb-02 21:07 
General'Maintenance?' Pin
Venkatesan Murugesan4-Feb-02 20:49
Venkatesan Murugesan4-Feb-02 20:49 
GeneralRe: 'Maintenance?' Pin
Pål K Tønder4-Feb-02 21:39
Pål K Tønder4-Feb-02 21:39 
GeneralRe: 'Maintenance?' Pin
Venkatesan Murugesan5-Feb-02 18:47
Venkatesan Murugesan5-Feb-02 18:47 
QuestionSwitching XP visual styles when a modal dialog is up causes problem? Pin
30-Jan-02 15:56
suss30-Jan-02 15:56 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.