Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Win32

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
24 Apr 2008CPOL3 min read 71.5K   1K   27   14
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)


Written By
Business Analyst Southwest Research Institute
United States United States
I am a C++ coder. I am also proficient with Oracle PL/SQL. A lot of folks dislike Oracle, but I find Oracle is a significant revenue enhancer. Customers pay for first rate Oracle programming skills.

I have extensive experience with COM+, COM, ATL, WTL and installation package development. I've developed several packages in C#, but I prefer managed/native C++. I've been coding for nearly thirty plus years, getting my start with atomic and molecular orbital calculations in FORTRAN. I've been working with C or C++ since the days of QuickC, Desmet C, Datalight C and MSC 5.1.

One of my pet peeves in life is a programmer's lack of attention to the details of error handling. Most example code I see on the internet lacks depth. No use of Window's Event Logging and a lack of understanding as to how to handle exceptions. If folks actually think about how to properly debug and test, there would be fewer "slop" articles and a lot higher quality.

Including instrumentation in your software to allow proper diagnosis of failures is far more important to a user than the latest Gee-Whiz-Bang visual effects. Graphical gotta-haves fade like the lettuce in a refrigerator, but solid programs just keep on running, no matter what environment they are placed in.

My Web Site, Blog & Wiki

Comments and Discussions

 
QuestionCan you supply a binary? Pin
.dan.g.17-Dec-10 23:40
professional.dan.g.17-Dec-10 23:40 
GeneralGetSchemaInfo': identifier not found Pin
Prasanna Bhat9-Nov-10 8:16
professionalPrasanna Bhat9-Nov-10 8:16 
GeneralDoes not work for me (WinXP SP2) Pin
llothar12-Jul-10 15:58
llothar12-Jul-10 15:58 
GeneralRe: Does not work for me (WinXP SP2) Pin
Gene OK12-Jul-10 16:05
Gene OK12-Jul-10 16:05 
GeneralUnhandled Exception Pin
Steve Thresher12-Aug-08 12:15
Steve Thresher12-Aug-08 12:15 
GeneralRe: Unhandled Exception Pin
Gene OK12-Aug-08 12:51
Gene OK12-Aug-08 12:51 
QuestionSystem.Windows.Forms.VisualStyles Pin
Đonny15-May-08 7:46
Đonny15-May-08 7:46 
AnswerRe: System.Windows.Forms.VisualStyles Pin
Gene OK15-May-08 8:46
Gene OK15-May-08 8:46 
GeneralCan't Compile Code Pin
ChrisP111814-May-08 16:45
ChrisP111814-May-08 16:45 
GeneralRe: Can't Compile Code Pin
Gene OK14-May-08 17:45
Gene OK14-May-08 17:45 
RantRe: Can't Compile Code Pin
Gene OK14-May-08 17:56
Gene OK14-May-08 17:56 
GeneralPraise...and a suggestion. Pin
Humble Programmer25-Apr-08 8:42
Humble Programmer25-Apr-08 8:42 
GeneralRe: Praise...and a suggestion. Pin
Gene OK25-Apr-08 9:16
Gene OK25-Apr-08 9:16 
GeneralAdding Themes to this control Pin
Gene OK25-Apr-08 3:56
Gene OK25-Apr-08 3: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.