Monday, June 12, 2006

Windows XP Look and Feel in C# Windows Forms Application

The look and feel of Windows C# application can be changed to Windows XP style, with a XML manifest file. The file must be kept in the same directory with the same name of the executable with ".manifest" extension. For example if "User.exe" is executable name then the manifest file name is "user.exe.manifest".

The file must contain the dependancy attribute and under this attribute we must set the identity of the component our application is dependenting, in this case the new "Microsoft Windows Common-Controls" version 6.0. The following example shows the appearance of this application manifest file.

1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

2 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">

3 <assemblyidentity name="MyVisualStyleApp" type="win32" processorarchitecture="X86" version="">

4 <description>Your app description here</descriptio>>

5 <dependency>

6 <dependentassembly>

7 <assemblyidentity language="*" name="Microsoft.Windows.Common-Controls" type="win32" processorarchitecture="X86" version="" publickeytoken="6595b64144ccf1df">

8 </dependentassembly>

9 </dependency>

10 </assembly>


The line 3 says the version of the application, the processor architecture for which the application is developed, name of the application and type of the application.
The line 7 says the Type of the dependency component, such as Win32, name of the component, version of the component, processor that the component is designed for, Key token used with this component and Language of the component.

How this look and feel? 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- DrawThemeParentBackgroundAnd don't forget to close the theme handle when you exit the program! You can do it by using "CloseThemeData" method.

Button Look? If we apply this using manifest file, the button visual is not changed so how to change the button, for changing the button visual set the "FlatStyle" property of the button for "System".


Post a Comment

<< Home