Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
Application Class
Application Methods
 EnableVisualStyles Method
Collapse All/Expand All Collapse All
.NET Framework Class Library
Application..::.EnableVisualStyles Method

Enables visual styles for the application.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
Public Shared Sub EnableVisualStyles
C#
public static void EnableVisualStyles()
Visual C++
public:
static void EnableVisualStyles()
F#
static member EnableVisualStyles : unit -> unit 

This method enables visual styles for the application. Visual styles are the colors, fonts, and other visual elements that form an operating system theme. Controls will draw with visual styles if the control and the operating system support it. To have an effect, EnableVisualStyles()()() must be called before creating any controls in the application; typically, EnableVisualStyles()()() is the first line in the Main function. A separate manifest is not required to enable visual styles when calling EnableVisualStyles()()().

NoteNote

Prior to the .NET Framework 2.0, the FlatStyle property of some controls, such as controls that derive from ButtonBase, had to be set to FlatStyle..::.System in order for the controls to be drawn with visual styles. In applications written with the .NET Framework 2.0, this is no longer necessary.

NoteNote

This method will have no effect for controls hosted in Internet Explorer.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are only supported on these platforms.

The following code example demonstrates calling EnableVisualStyles in the Main function to enable visual styles for the application.

Visual Basic
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace VStyles

    ' Summary description for Form1.
    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private button1 As System.Windows.Forms.Button

        <System.STAThread()> _
        Public Shared Sub Main()

            System.Windows.Forms.Application.EnableVisualStyles()
            System.Windows.Forms.Application.Run(New Form1)
        End Sub 'Main

        Public Sub New()

            Me.button1 = New System.Windows.Forms.Button()
            Me.button1.Location = New System.Drawing.Point(24, 16)
            Me.button1.Size = New System.Drawing.Size(120, 100)
            Me.button1.FlatStyle = FlatStyle.System
            Me.button1.Text = "I am themed."

            ' Sets up how the form should be displayed and adds the controls to the form.
            Me.ClientSize = New System.Drawing.Size(300, 286)
            Me.Controls.Add(Me.button1)

            Me.Text = "Application.EnableVisualStyles Example"
        End Sub 'New 

    End Class 'Form1
End Namespace 'VStyles
C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace VStyles
{
    public class Form1 : System.Windows.Forms.Form
    {

        private System.Windows.Forms.Button button1;

        [STAThread]
        static void Main() 
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button1.Location = new System.Drawing.Point(24, 16);
            this.button1.Size = new System.Drawing.Size(120, 100);
            this.button1.FlatStyle = FlatStyle.System;
            this.button1.Text = "I am themed.";

            // Sets up how the form should be displayed and adds the controls to the form.
            this.ClientSize = new System.Drawing.Size(300, 286);
            this.Controls.Add(this.button1);

            this.Text = "Application.EnableVisualStyles Example";

        }
    }
}
Visual C++
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace VStyles
{
   public ref class Form1: public System::Windows::Forms::Form
   {
   private:
      System::Windows::Forms::Button^ button1;

   public:
      Form1()
      {
         this->button1 = gcnew System::Windows::Forms::Button;
         this->button1->Location = System::Drawing::Point( 24, 16 );
         this->button1->Size = System::Drawing::Size( 120, 100 );
         this->button1->FlatStyle = FlatStyle::System;
         this->button1->Text = "I am themed.";

         // Sets up how the form should be displayed and adds the controls to the form.
         this->ClientSize = System::Drawing::Size( 300, 286 );
         this->Controls->Add( this->button1 );
         this->Text = "Application::EnableVisualStyles Example";
      }

   };

}


[STAThread]
int main()
{
   Application::EnableVisualStyles();
   Application::Run( gcnew VStyles::Form1 );
}

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Roll your own version!      Daniel Normal Johnson   |   Edit   |   Show History
If you can't use a proper manifest (perhaps you are using ClickOnce) you can reimplement EnableVisualStyles yourself! MS has a sample that does just that:

http://support.microsoft.com/kb/830033

The "EnableThemingInScope" class given does the trick, and can be used before any part of Windows Forms is invoked. And you will still get SEHExceptions that have the error code E_FAIL, because the sample code is buggy. But you can fix it!

The sample declares a field "cookie" and two parameters "lpCookie" as 'uint', but it should be UIntPtr (or in one place 'out UIntPtr'). In a 32-bit process, there is no real diference, but in a 64-bit process a UIntPtr expands to 64-bit. If you don't do this, then the call to ActivateActCtx() scribbles over the stack and could corrupt it; the call to DeactivateActCtx() knackers the stack, and P/Invoke throws the SEHException.
Tags What's this?: Add a tag
Flag as ContentBug
Intermittent SEHExceptions      Daniel Normal Johnson   |   Edit   |   Show History
It seems like using this will cause the functions that retrieve assembly resources to fail, intermittently and rarely, with an SEHException that has the error code E_FAIL. It's very odd and hard to reproduce, and the SEHException seems to be treated as a 'corupted state exception', so you can't catch it normally.

The best answer to this is simply not to use this method anyway; there is no real need for it in most cases. Instead should set up a manifest to do it, as desribed in this article: http://msdn.microsoft.com/en-us/library/bb773175(v=vs.85).aspx

The difference, I suspect, is that with a manifest you load the correct version of comctl32.dll at startup, and with the method call you wind up changing which comctl32 you are using 'midstream'.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker