How to add Visual Styles (the Windows XP look and feel) to your C# Application
Hello buddies! I have spending my time workinga lot and haven't time to write articles or show some sampleapplications. This article demonstrates how to add Visual Styles toyour application.
The first way to afford Visual Styles is bythe use of an XML application manifest file. This file must be in thesame directory as the application itself and this file must have thesame name of your application followed by the file extension".manifest". The file must contain an attribute called "dependency" andunder this attribute we must set the identity of the component ourapplication is dependent, in this case the new "Microsoft WindowsCommon-Controls" version 6.0. The following example shows theappearance 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 seethe effect. Legacy applications can take advantage of this approachalso, but on some you may experience bugs like incorrect rendering. Thefollowing table shows the description of each element in theapplication 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 renderedlike a Windows XP button. Can we apply Visual Style to the button? Yeswe 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 customcontrol you must take care of one thing, the "UxTheme.dll" is foundonly in Windows XP actually, this means if you try running anapplication on the Windows 98 for example, your application will crash.How can I take care of this? Verifying the Windows version and ID. Thefollowing 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 disableVisual Styles under Windows XP and your application could crash again.And now, how can I handle this? Verifying if Visual Styles are enabledor 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). Thefollowing 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.












No comments yet... Be the first to leave a reply!