Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#

Applying XP Visual Styles to Windows Forms Applications

Rate me:
Please Sign up or sign in to vote.
2.67/5 (9 votes)
11 Sep 2008CPOL2 min read 40.4K   420   13   5
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.

C#
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).

C#
.  .  .  .  .  .  .
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Korzh.com
Ukraine Ukraine
Software developer and entrepreneur.

Main projects:
* EasyQuery - ad-hoc data filtering UI for .NET applications;
* Localizer - localization tool kit for Delphi projects;

Comments and Discussions

 
GeneralOnly for .NET 2.0 or less! Pin
Sergiy Korzh9-Sep-08 6:00
professionalSergiy Korzh9-Sep-08 6:00 
GeneralUhm dunno what to say but... Pin
f r i s c h9-Sep-08 4:38
f r i s c h9-Sep-08 4:38 
GeneralRe: Uhm dunno what to say but... Pin
Sergiy Korzh9-Sep-08 4:42
professionalSergiy Korzh9-Sep-08 4:42 
GeneralRe: Uhm dunno what to say but... Pin
f r i s c h9-Sep-08 21:18
f r i s c h9-Sep-08 21:18 
GeneralRe: Uhm dunno what to say but... Pin
Sergiy Korzh9-Sep-08 21:35
professionalSergiy Korzh9-Sep-08 21:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.