Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

A Managed C++ Wrapper Around the Windows XP Theme API - Part 2

4.83/5 (16 votes)
24 Apr 2008CPOL3 min read 2   1K  
This is an update to Don Kackman's UxTheme component originally written for Visual Studio 2003
ThemeExplorer2.JPG

Introduction

This is an updated release of Don Kackman's excellent UxTheme control, ported to VC 8.0. The theme explorer has been updated to add theme color detail to the explorer interface. The code has been scrubbed to remove old syntax. Class names have been modified slightly to fit my naming conventions. (Class names begin with C, and structure names begin with S.) I removed references to vcclr.h. String handling is performed with Marshal::StringToHGlobalAuto. Appropriate macros for accessing managed strings are located in stdafx.h. At the urging of Don, I changed the name space from System::Windows::Froms::Themes to UxThemeTool.

Background

I became interested in Don's original work while I developed a custom button in managed C++ using .NET 2.0. The tool allowed me to add theme elements to my button control. However, I really wanted to use it without having to use the /clr:oldSyntax setting. So, I became sidetracked for a couple of days while I ported this code to VS 2005.

Using the Code

There really is nothing difficult about using this code. Simply add a reference to the UxThemeTool.dll to your C# or VB.NET project. You will need to be familiar with the Theme API included in the platform SDK for Windows XP or Windows Server 2003.

Initializing the control is simple. In your application's form, place the following code in the form_load method:

C++
// Call IsAppThemed first. This checks for theming before loading the UxTheme.dll.
// You do not want to access any theme methods if this property returns false.
if ( CUxTheme.IsAppThemed )
{
    // Do something with themes here
}
else
{
    MessageBox.Show( "Themes are not enabled" );
    this.Close();
}

Points of Interest

I was able to add additional documentation using the XML comment nomenclature outlined by Microsoft. However, the documentation process for managed C++ is much more labor intensive than for C#.

I was surprised at how lax the VS 2003 C++ compiler is. While porting the VS 2003 project code, I found a class that inherited (implemented) an interface, yet one of the functions was not implemented in the derived class. VS 2003 (VC 7.1) did not complain, but VS 2005 (VC 8.0) issued an error. Most of the porting work had to do with changes in syntax (removing _gc constructs and changing or adding abstract and override qualifiers). I also wanted to get rid of all dependencies on vcclr.h. (I just don't like dragging in gcroot.h everywhere, even if it's not used.)

Update

I added a default color parameter to CUxTheme::GetColor. I also removed a throw from this method. It turns out that most theme colors do not exist (are not used). Rather than throw an exception, I decided it would be better to simply return a default color. The method treeView1_AfterSelect_1 in file Form1.cs illustrates the coding style you can use to retrieve colors.

Compiling in Visual Studio 2008

If you want to compile this in Visual Studio 2008, be aware that Microsoft has introduced a bug in tmschema.h and schemadef.h distributed with the Platform SDK. There is #pragma once included at the beginning of these files. Since tmschema.def is meant for inclusion twice in your source code, the #pragma once statement prevents the second pass from parsing correctly. Please see the post in my blog for further explanation. The simplest way to fix the problem is to comment out the #pragma once statements at the top of tmschema.h and schemadef.h. You should be aware that these files do not include updates for Vista. Microsoft has changed the internal structure for access Windows Vista themes. I am working on updating the code to handle Vista themes.

History

  • Version 1.0 April 10, 2008
    • Original port of code from VC 7.1 to VC 8.0
    • Removed all old syntax
  • Version 1.1 April 24, 2008
    • Updated interface to indicate theme colors

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)