Blog Stats
  • Posts - 36
  • Articles - 0
  • Comments - 11
  • Trackbacks - 23

 

Windows XP styling for yr Windows Forms

Prologue: Comctl32.dll, version 6. Comctl32.dll, or the Common Controls, have been around for a very long time. This library provided basic common controls while User32.dll provided user controls. In version 6 of the Common Controls, all of the controls were put in Comctl32.dll so that all the controls could support theming. Comctl32.dll, version 6, is not redistributable, however, unlike previous versions of Comctl32.dll. In order to use the new Windows XP Visual Styles, you must be using an operating system that contains Comctl32.dll, such as Windows XP.

For supported controls, the control style is associated with a particular theme resource that is drawn in the client area of the control. For controls deriving from ButtonBase, GroupBox, and Label, these controls must use the FlatStyle.System enumeration member in their FlatStyle property so that they allow the operating system to draw the controls.

In order to tell Windows to use theming for your controls, you need to tell Windows to bind to Comctl32.dll, version 6. By default, Windows binds to Comctl32.dll, version 5. Similar to the binding policies of .NET, however, we can tell executables at runtime to bind to other assemblies, or Win32 libraries in this case.

So, add a new XML file to your project and call it .exe.manifest, where is the name of the output file your project generates, which is your project name by default. Set the Compile Type to "None", since we'll only including this in your project for simplicity.

If you used the "AssemblyInfo.cs" code above, your newly created .exe.manifest should look like:

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



You should replace the first "version" and "name" attributes with appropriate values for your project, which are the AssemblyVersion and AssemblyTitle attributes respectively. The "description" element should also match your AssemblyDescription attribute.

Next, open a command prompt and change directories to your project output directory, which should be "bin\Debug" with your project directory as the root. Assuming that you have "sn.exe" in your PATH (part of the .NET Framework SDK), type the following:

sn.exe -k ..\..\KeyFile.snk


This should put a public key file named KeyFile.snk - which we referred to earlier in our AssemblyInfo.cs file - in your project directory. This public key is necessary to sign your assembly with a strong name, which is required in many situations and is recommend for every assembly.


Adding the Manifest and the Final Touches

1. You should now be in the same directory as your project output file. In my example, my application is simply "WindowsApplication1.exe", which I'll refer to later.

2. Now, back in Visual Studio .NET, click on the File->Open... menu, browse to your application such as "WindowsAppication1.exe", and click open. You should now see your resources in a tree view.

3. Right-click on the root node ("WindowsApplication.exe") and select "Add Resource...".

4. Click "Import...", browse for "WindowsApplication.exe.manifest" in your project directory, and click "Open".

5. In the "Resource Type" text box, type RT_MANIFEST and click "OK".
6. Save all files and go back to your resource view you were previously in. You should now see a folder named "RT_MANIFEST".

7. Click on the newly added RT_MANIFEST resource, probably named 101. Change its ID to 1 in the property grid and save your application again.

After closing the resource view in which your application was open, go back to the command prompt and type the following:

sn -R WindowsApplication1.exe ..\..\KeyFile.snk


The tool should print some version info and then display:

Assembly 'WindowsApplication1.exe' successfully re-signed

Run your application and see Windows XP Visual Styles at work in your .NET application!


Summary

Windows XP Visual Styles can be a very good addition to your applications and control libraries and they don't take a lot of work. Supporting this new visual style will give your application a common look-and-feel with Windows, which is always a definite plus in commercial applications. Many companies strive to keep up with Windows standards and now you can do it without a lot of work.

Just remember to set the FlatStyle property of any applicable control to FlatStyle.System and to embed the manifest resource like the boilerplate above in your executable after compiling. After having done all that, you need only finish signing your assembly, otherwise it won't pass validation if you specified an AssemblyKeyFile. If you need to test your application or have an authority sign it, you can turn off validation for that assembly by typing the following:

sn.exe -Vr WindowsApplication1.exe


Resource:

Windows XP Visual Styles for Windows Forms

MSDN

.NET Ideas
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Windows XP styling for yr Windows Forms

Gravatar hope this works
1/3/2004 5:39 PM | whatever

# re: Windows XP styling for yr Windows Forms

Gravatar Thanks for this info, the part about FlatStyle is a golden nugget that I hadn't seen before

I experimented with this and you do not actually need to put the assemblyIdentity element in the manifest file. Which means you do not need to keep the Version numbers in sync.

Also if you want to keep the option of which style of buttons so they can be changed at runtime then you can just have the manifest XML file in the same folder as the EXE 1/15/2004 1:52 PM | Derek Wilson

# re: Windows XP styling for yr Windows Forms

Gravatar Another method is to switch the controls to FlatStyle=System. Then just add this one line of code to the New sub just before you call initcontrols.

Application.EnableVisualStyles

Easy as that. 7/8/2004 4:45 PM | Michael Herbert

# re: Windows XP styling for yr Windows Forms

Gravatar Hey, really its Working Good Yaar!!! 6/18/2005 2:51 PM | G.G.Pradeep Babu

# re: Windows XP styling for yr Windows Forms

Gravatar This solution works when the app is run locally. However it doesn't work when app is launched via a URL as a smart client? 10/24/2005 5:22 PM | Merv Harrington

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © Ramesh Arimilli