Click here to Skip to main content
Click here to Skip to main content

Applying XP visual styles to Windows Forms applications

, 11 Sep 2008
Rate this:
Please Sign up or sign in to vote.
How to make a Windows Forms program use 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 so 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
{
    /// <span class="code-SummaryComment"><summary></span>
    /// Represents different procedures that allows to turn
    /// on XP visual style for Windows Forms application
    /// <span class="code-SummaryComment"></summary></span>

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

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

        /// <span class="code-SummaryComment"><summary></span>
        /// Enables the visual styles for application.
        /// <span class="code-SummaryComment"></summary></span>
        public static void EnableVisualStyles() {
            if (!IsXPThemesPresent) return;
            Application.EnableVisualStyles();
            Application.DoEvents();
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Applies the visual styles for some control.
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name=&quot;control&quot;>The control.</param></span>

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

About the Author

Sergiy Korzh
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;
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralOnly for .NET 2.0 or less! PinmemberSergiy Korzh9-Sep-08 6:00 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 11 Sep 2008
Article Copyright 2008 by Sergiy Korzh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid