6.20 Apply Windows XP Control Styles


Problem

You want your controls to have the updated Windows XP appearance on Windows XP systems.

Solution

Set the FlatStyle property to FlatStyle.System for all controls that support it. In the .NET Framework version 1.0, you must create a manifest file. In .NET Framework version 1.1, you simply need to call the Application.EnableVisualStyles method.

Discussion

Windows XP styles are automatically applied to non-client areas of a form (such as the border and the minimize and maximize buttons). However, they won't be applied to controls such as buttons and group boxes unless you take additional steps.

First of all, you must configure all your form's button-style controls (such as buttons, check boxes, and radio buttons). These controls provide a FlatStyle property, which must be set to System .

The next step depends on the version of .NET that you are using. If you are using the .NET Framework version 1.1, you simply need to call the Application.EnableVisualStyles method before you show any forms. For example, you can start your application with the Main method shown here:

 public static void Main() {     // Enable visual styles.     Application.EnableVisualStyles();     // Show the main form for your application.     Application.Run(new StartForm) } 

If you are using the .NET Framework version 1.0, you don't have the convenience of the Application.EnableVisualStyles method. However, you can still use visual styles ”you simply need to create a manifest file for your application. This manifest file (an ordinary text file with XML content) tells Windows XP that your application requires the new version of the Comctl32.dll file. This file, which defines the new control styles, is included on all Windows XP computers. Windows XP will read and apply the settings from the manifest file automatically, provided you deploy it in the application directory and give it the correct name. The manifest file should have the same name as the executable used for your application, plus the extension .manifest (so TheApp.exe would have the manifest file TheApp.exe.manifest ”even though this looks like two extensions).

Following is a sample manifest file. You can copy this file for your own applications ”just rename it accordingly . It's also recommended that you modify the name value to use your application name, although this step isn't necessary.

 <?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="TheApp"      type="win32" />  <dependency>  <dependentAssembly>  <assemblyIdentity      type="win32"      name="Microsoft.Windows.Common-Controls"      version="6.0.0.0"      processorArchitecture="X86"      publicKeyToken="6595b64144ccf1df"      language="*" />  </dependentAssembly>  </dependency>  </assembly> 

The Windows XP styles won't appear in the Visual Studio .NET design- time environment. To test that this technique is working, run the application. Figure 6.13 shows the difference between the Windows XP and non “Windows XP control styles.

click to expand
Figure 6.13: Control styles with and without Windows XP.
Note  

If you supply a manifest file for an application running on a pre “Windows XP version of Windows, it will simply be ignored, and the classic control styles will be used. For this reason, you might want to test your application both with and without a manifest file.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net