Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Applying XP Visual Styles to Windows Forms Applications

0.00/5 (No votes)
11 Sep 2008 2  
How to make a Windows Forms program using Windows XP styles.

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
{
    /// <summary>
    /// Represents different procedures that allows to turn
    /// on XP visual style for Windows Forms application
    /// </summary>

    public class XPStyle
    {
        /// <summary>
        /// Gets a value indicating whether XP themes feature is present.
        /// </summary>
        /// <value><c>true</c> if XP themes feature is present;
        ///           otherwise, <c>false</c>.</value>

        public static bool IsXPThemesPresent {
            get { 
                return OSFeature.Feature.IsPresent(OSFeature.Themes); 
            }
        }

        /// <summary>
        /// Enables the visual styles for application.
        /// </summary>
        public static void EnableVisualStyles() {
            if (!IsXPThemesPresent) return;
            Application.EnableVisualStyles();
            Application.DoEvents();
        }

        /// <summary>
        /// Applies the visual styles for some control.
        /// </summary>
        /// <param name=&quot;control&quot;>The control.</param>

        public static void ApplyVisualStyles(Control control) {
            if (!IsXPThemesPresent) return;
            ChangeControlFlatStyleToSystem(control);
        }

        private static void ChangeControlFlatStyleToSystem(Control control) {
            // If the control derives from ButtonBase, 
            // set its FlatStyle property to FlatStyle.System.
            if(control.GetType().BaseType == typeof(ButtonBase)) {
                ((ButtonBase)control).FlatStyle = FlatStyle.System; 
            }

            // If the control holds other controls, iterate through them also.
            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);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here