Click here to Skip to main content
13,706,038 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 9 Sep 2008
Licenced CPOL

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.


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;

        /// <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;

        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++) {

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) {

    Application.Run(new MainForm());
.  .  .  .  .  .  .

.  .  .  .  .  .  .

private void MainForm_Load(object sender, System.EventArgs e) {


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sergiy Korzh
Ukraine Ukraine
Software developer and entrepreneur.

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

You may also be interested in...


Comments and Discussions

GeneralOnly for .NET 2.0 or less! Pin
Sergiy Korzh9-Sep-08 6:00
memberSergiy Korzh9-Sep-08 6:00 
GeneralUhm dunno what to say but... Pin
f r i s c h9-Sep-08 4:38
memberf r i s c h9-Sep-08 4:38 
GeneralRe: Uhm dunno what to say but... Pin
Sergiy Korzh9-Sep-08 4:42
memberSergiy Korzh9-Sep-08 4:42 
GeneralRe: Uhm dunno what to say but... Pin
f r i s c h9-Sep-08 21:18
memberf r i s c h9-Sep-08 21:18 
GeneralRe: Uhm dunno what to say but... Pin
Sergiy Korzh9-Sep-08 21:35
memberSergiy 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 11 Sep 2008
Article Copyright 2008 by Sergiy Korzh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid