Introduction
Sometimes, Windows Forms programs by default look like old-style Win32 API applications — they simply do not use Windows XP visual styles to render controls.
First of all, this problem is generally for the projects created for .NET 1.1. But you can encounter it in .NET 2.0 (and higher) projects which are ported from, or must be compatible with .NET 1.1.
The Solution
However, it is not as bad as it could be. Microsoft allows us to include XP theme support into our projects using one of the following methods:
- By adding a manifest file into your project. Here is an MSDN article which describes this.
- Using the
Application.EnableVisualStyles()
method call — much more simple, but sometimes buggy in .NET 1.1.
Personally, I prefer the second way, especially after Microsoft proposed a workaround for the bug with the EnableVisualStyles()
method in .NET 1.1.
So, to enable XP styles in your Windows Forms applications, you just need to place the Application.EnableVisualStyles()
call in your project's main()
procedure and make an additional Application.DoEvents()
call to fix the possible problems in .NET 1.1. Is that all? Not exactly.
If you simply place your controls on a form from the Toolbox
, then most buttons, checkboxes, and other "button-like" controls will still have the old look even if you call EnableVisualStyles()
at the beginning of your program. Why? Because all these components have their FlatStyle
property equal to Standard
, by default, while it should be set to System
.
So, we need to run through all the controls on each form and set the FlastStyle
property to FlatStyle.System
. To simplify all the described tasks, I have written a special XPStyle
class which contains several static
methods. Using these methods, you can easily add XP styles support into your existing Windows Forms programs.
using System;
using System.Windows.Forms;
namespace Korzh.WinForms
{
public class XPStyle
{
public static bool IsXPThemesPresent {
get {
return OSFeature.Feature.IsPresent(OSFeature.Themes);
}
}
public static void EnableVisualStyles() {
if (!IsXPThemesPresent) return;
Application.EnableVisualStyles();
Application.DoEvents();
}
public static void ApplyVisualStyles(Control control) {
if (!IsXPThemesPresent) return;
ChangeControlFlatStyleToSystem(control);
}
private static void ChangeControlFlatStyleToSystem(Control control) {
if(control.GetType().BaseType == typeof(ButtonBase)) {
((ButtonBase)control).FlatStyle = FlatStyle.System;
}
for(int i = 0; i < control.Controls.Count; i++) {
ChangeControlFlatStyleToSystem(control.Controls[i]);
}
}
}
}
Now, you should call the EnableVisualStyles()
method of this class at the beginning of your program, and then call ApplyVisualStyles()
for each form (at the end of the form's constructor or in the Load
event handler).
. . . . . . .
static void Main(string[] args) {
XpStyle.EnableVisualStyles();
Application.Run(new MainForm());
}
. . . . . . .
. . . . . . .
private void MainForm_Load(object sender, System.EventArgs e) {
XPStyle.ApplyVisualStyles(this);
}