Click here to Skip to main content
6,629,885 members and growing! (20,973 online)
Email Password   helpLost your password?
Languages » C++ / CLI » C++/CLI     Advanced License: The Code Project Open License (CPOL)

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

By CodeWizard1951

This is an update to Don Kackman's UxTheme component originally written for Visual Studio 2003
C++ (VC6, VC7, VC7.1, VC8.0), C++/CLI, C, Windows (WinXP, Win2003), .NET (.NET 2.0), Win32, Dev
Posted:10 Apr 2008
Updated:24 Apr 2008
Views:16,415
Bookmarked:18 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 5.61 Rating: 4.77 out of 5

1

2
1 vote, 6.7%
3
2 votes, 13.3%
4
12 votes, 80.0%
5
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:

    // 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 but 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. Orignal 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)

About the Author

CodeWizard1951


Member
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
Occupation: Software Developer (Senior)
Company: Southwest Research Institute
Location: United States United States

Other popular C++ / CLI articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralUnhandled Exception PinmemberSteve Thresher13:15 12 Aug '08  
GeneralRe: Unhandled Exception PinmemberCodeWizard195113:51 12 Aug '08  
QuestionSystem.Windows.Forms.VisualStyles Pinmember8:46 15 May '08  
AnswerRe: System.Windows.Forms.VisualStyles PinmemberCodeWizard19519:46 15 May '08  
GeneralCan't Compile Code PinmemberChrisP111817:45 14 May '08  
GeneralRe: Can't Compile Code PinmemberCodeWizard195118:45 14 May '08  
RantRe: Can't Compile Code PinmemberCodeWizard195118:56 14 May '08  
GeneralPraise...and a suggestion. PinmemberHumble Programmer9:42 25 Apr '08  
GeneralRe: Praise...and a suggestion. PinmemberCodeWizard195110:16 25 Apr '08  
GeneralAdding Themes to this control PinmemberCodeWizard19514:56 25 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Apr 2008
Editor:
Copyright 2008 by CodeWizard1951
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project