Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

How to add Visual Styles (the Windows XP look and feel) to your C# Application
By Gildeoni Nogueira Santos

Hello buddies! I have spending my time working a lot and haven't time to write articles or show some sample applications. This article demonstrates how to add Visual Styles to your application.

The first way to afford Visual Styles is by the use of an XML application manifest file. This file must be in the same directory as the application itself and this file must have the same name of your application followed by the file extension ".manifest". The file must contain an attribute called "dependency" and under this attribute we must set the identity of the component our application is dependent, in this case the new "Microsoft Windows Common-Controls" version 6.0. The following example shows the appearance of this application manifest file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
<assemblyIdentity 
    version="1.0.0.0" 
    processorArchitecture="X86" 
    name="MyVisualStyleApp"
    type="win32" 
/> 
<description>Your app description here</description> 
<dependency> 
    <dependentAssembly> 
        <assemblyIdentity 
            type="win32" 
            name="Microsoft.Windows.Common-Controls" 
            version="6.0.0.0" 
            processorArchitecture="X86" 
            publicKeyToken="6595b64144ccf1df" 
            language="*" 
        /> 
    </dependentAssembly> 
</dependency> 
</assembly>
MyVisualStyleApp.exe.manifest

If you run the sample application you will see the effect. Legacy applications can take advantage of this approach also, but on some you may experience bugs like incorrect rendering. The following table shows the description of each element in the application manifest file.

My Assembly:

Attribute	        Description

Version	                Version of the manifest. The version must be in the form 
                        major.minor.revision.build (that is, n.n.n.n, where n < = 65535). 
processorArchitecture	Processor for which your application is developed. 
Name	                Includes company name, product name and application name. 
Type	                Type of your application, such as Microsoft Win32. 
My Assembly Dependency:
Attribute	        Description

Type	                Type of the dependency component, such as Win32.
Name	                Name of the component. 
Version	                Version of the component. 
processorArchitecture	Processor that the component is designed for. 
publicKeyToken	        Key token used with this component. 
Language	        Language of the component. 
And the button? The button wasn't rendered like a Windows XP button. Can we apply Visual Style to the button? Yes we can. So how? Accessing Windows API and drawing the button manually. All the functions necessary to draw a Windows XP style button is in "UxTheme.dll" under the system directory. Before draw your custom control you must take care of one thing, the "UxTheme.dll" is found only in Windows XP actually, this means if you try running an application on the Windows 98 for example, your application will crash. How can I take care of this? Verifying the Windows version and ID. The following lines of code show you how to do that.
PlatformID platformId = Environment.OSVersion.Platform;

Version version = Environment.OSVersion.Version;
Version targetVersion = new Version("5.1.2600.0");

if ((version > = targetVersion) && (platformId == PlatformID.Win32NT))
There's another thing, the user can disable Visual Styles under Windows XP and your application could crash again. And now, how can I handle this? Verifying if Visual Styles are enabled or disabled with the following piece of code.

if (NativeMethods.IsThemeActive() == 1)

The "IsThemeActive" method of the "UxTheme.dll" library will return the value 1 if Visual Styles are enabled or 0 if not.

To apply the theme call the "OpenThemeData" method this way.

hTheme = NativeMethods.OpenThemeData(this.Handle, "Button");

"hTheme" holds the handle returned from the "OpenThemeData" method containing the Visual Style (if exists). The following methods could be used to draw Windows XP style controls.

- DrawThemeBackground
- DrawThemeText
- DrawThemeParentBackground
And don't forget to close the theme handle when you exit the program! You can do it by using "CloseThemeData" method.

There are other controls that don't take the advantage of application manifest files, like RadioButton for example.

You can now draw your Windows XP style control. Good programming for all of you.

WindowsXPLook.zip