.NET Programmer Thoughts

Friday, April 01, 2005

Using XP Style Themes in Visual Studio 2003

Using XP Style themes in Visual Studio 2003 is easier than you might think. There are two simple steps and your forms will have an XP Style theme.

Here is a comparison of what a VS 2003 windows form looks like with and without XP Style themes.





Here is how you do it.

Step 1:

Option 1:
Create a manifest file. This file must reside with the executable file. Place the following XML in the file.



The only change you need to make is to the name attribute: Microsoft.Winweb.appname

Option 2:
Add two lines of code to the beginning of Main(). These two lines must be executed before any controls are drawn.

Application.EnableVisualStyles();
Application.DoEvents();

The DoEvents() line corrects a bug in .NET 1.1. If you don’t include it, images won’t be displayed on your buttons.


Step 2: Add some code to your windows form to change the style of the buttons. When you use themed controls, you must set the FlatStyle property to System. The system must control the entire drawing process. The FlatStyle property is used on any class that’s derived from System.Windows.Forms.ButtonBase.



When you implement these two steps, your application will emulate the theme that the operating system is using. So if you aren’t using an XP style theme, then you won’t see an XP style theme in your application. Also, you will not get XP style themes on pre-XP operating systems.

Windows Server 2003 requires another step to get XP Style themes. You have to go into services and start up a service named “Themes”. Then go into the Appearance tab of the Display properties and chose an XP Style theme.

Windows XP/2003 ships with two versions of the Common Controls library (comctl32.dll). They are referred to as version 5 and version 6. Version 5 renders controls using the standard look and feel. Version 6 renders controls with the XP look and feel. Windows Form 1.1 uses version 5 as its default library. When you include this manifest file or use EnableVisualStyles(), you are telling your application to use version 6 of the common control library.

This is something we won’t have to deal with in Windows Forms 2.0. Version 6 of the Common Control library will be the default library.