VisualStyleElement Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Identifies a control or user interface (UI) element that is drawn with visual styles.

Namespace:   System.Windows.Forms.VisualStyles
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)


public class VisualStyleElement


Gets the class name of the visual style element that this VisualStyleElement represents.


Gets a value indicating the part of the visual style element that this VisualStyleElement represents.


Gets a value indicating the state of the visual style element that this VisualStyleElement represents.

System_CAPS_pubmethodSystem_CAPS_staticCreateElement(String, Int32, Int32)

Creates a new visual style element from the specified class, part, and state values.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

The VisualStyleElement class identifies a Windows control or user interface (UI) element that is defined by the current visual style of the operating system. A visual style element is defined in terms of class, part, and state:

  • The class identifies a group of related visual style elements that are typically drawn together. The class is specified by the ClassName property.

  • The part defines a discrete member of a class. The part is specified by the Part property.

  • The state describes the current visual state of the element defined by the class and part. The state is specified by the State property.

The System.Windows.Forms.VisualStyles namespace exposes hundreds of VisualStyleElement objects that represent the controls and user interface elements that can be defined by a visual style. These objects are exposed as static properties of nested classes of VisualStyleElement. For each property, the qualified name of the owning class represents the class and the part of the drawn element, and the property name represents the state of the element. For example, the VisualStyleElement.Button.PushButton.Pressed property returns a VisualStyleElement that can be used to draw a button in the pressed state with visual styles. The Pressed property name corresponds to the state, PushButton corresponds to the part, and Button identifies the class of the visual style element.

To draw a visual style element, create a VisualStyleRenderer, set it to the VisualStyleElement you wish to draw, and then call the DrawBackground method. To determine whether the current visual style provides a definition for a particular element, call the IsElementDefined method with the element as the argument.

VisualStyleElement and VisualStyleRenderer wrap the functionality of the visual styles API from the Windows Shell portion of the Windows Platform SDK. For more information on visual styles, see "Using Windows XP Visual Styles" in the Platform SDK portion of the MSDN Library at


Visual styles are supported only on these platforms.

The following code example is a complete application that provides a UI for browsing and drawing all of the VisualStyleElement objects exposed by the System.Windows.Forms.VisualStyles namespace. To view an element, use the ListView control on the left side of the window to navigate to the node that represents the state of the element you want to draw, and then click the node. This sample will draw the selected element only if the element is defined by the current visual style.

using System;
using System.Text;
using System.Drawing;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace VisualStyleElementViewer
    class Form1 : Form
        public Form1()
            ElementViewer ElementViewer1 = new ElementViewer();
            this.Text = ElementViewer1.Text;
            this.Size = new Size(700, 550);

        static void Main()
            Application.Run(new Form1());

    public class ElementViewer : UserControl
        private VisualStyleElement element;
        private VisualStyleRenderer renderer;
        private Dictionary<string, VisualStyleElement> elementDictionary =
            new Dictionary<string, VisualStyleElement>();

        private Rectangle descriptionRect;
        private Rectangle displayRect;
        private Rectangle displayRectFull;
        private Size currentTrueSize = new Size();
        private StringBuilder elementDescription = new StringBuilder();
        private Label label1 = new Label();
        private TreeView treeView1 = new TreeView();
        private DomainUpDown domainUpDown1 = new DomainUpDown();
        private bool drawElement = false;

        public ElementViewer()
            : base()
            this.Location = new Point(10, 10);
            this.Size = new Size(650, 500);
            this.Text = "VisualStyleElement Viewer";
            this.Font = SystemFonts.IconTitleFont;
            this.BackColor = Color.White;
            this.BorderStyle = BorderStyle.Fixed3D;
            this.AutoSize = true;
            this.Load += new EventHandler(ElementViewer_Load);

        void ElementViewer_Load(object sender, EventArgs e)
            // Make sure the visual styles are enabled before 
            // going any further.
            if (!Application.RenderWithVisualStyles)

            label1.Location = new Point(320, 10);
            label1.Size = new Size(300, 60);
            label1.Text = "Expand the element class nodes " +
                "in the tree view to access visual style elements. " +
                "Click an element name to draw the element below. To " +
                "change the size of a resizable element, use the " +
                "spin control.";

            domainUpDown1.Location = new Point(320, 80);
            domainUpDown1.Size = new Size(70, 30);
            domainUpDown1.ReadOnly = true;
            domainUpDown1.SelectedIndex = 2;
            domainUpDown1.SelectedItemChanged +=
                new EventHandler(domainUpDown1_SelectedItemChanged);

            descriptionRect = new Rectangle(320, 120, 250, 50);
            displayRect = new Rectangle(320, 160, 0, 0);
            displayRectFull = new Rectangle(320, 160, 300, 200);

            // Initialize the element and renderer to known good values.
            element = VisualStyleElement.Button.PushButton.Normal;
            renderer = new VisualStyleRenderer(element);


            this.Controls.AddRange(new Control[] { treeView1, 
                domainUpDown1, label1 });

        // Use reflection to build a Dictionary of all 
        // VisualStyleElement objects exposed in the 
        // System.Windows.Forms.VisualStyles namespace.
        private void SetupElementCollection()
            StringBuilder elementName = new StringBuilder();
            VisualStyleElement currentElement;
            int plusSignIndex = 0;

            // Get array of first-level nested types within 
            // VisualStyleElement; these are the element classes.
            Type[] elementClasses =

            foreach (Type elementClass in elementClasses)
                // Get an array of second-level nested types within
                // VisualStyleElement; these are the element parts.
                Type[] elementParts = elementClass.GetNestedTypes();

                // Get the index of the first '+' character in 
                // the full element class name.
                plusSignIndex = elementClass.FullName.IndexOf('+');

                foreach (Type elementPart in elementParts)
                    // Get an array of static property details 
                    // for  the current type. Each of these types have 
                    // properties that return VisualStyleElement objects.
                    PropertyInfo[] elementProperties =
                        elementPart.GetProperties(BindingFlags.Static |

                    // For each property, insert the unique full element   
                    // name and the element into the collection.
                    foreach (PropertyInfo elementProperty in
                        // Get the element.
                        currentElement =
                            GetValue(null, BindingFlags.Static, null,
                            null, null);

                        // Append the full element name.
                            plusSignIndex + 1,
                            elementClass.FullName.Length -
                            plusSignIndex - 1);
                        elementName.Append("." +
                            elementPart.Name.ToString() + "." +

                        // Add the element and element name to 
                        // the Dictionary.

                        // Clear the element name for the 
                        // next iteration.
                        elementName.Remove(0, elementName.Length);

        // Initialize the tree view with the element names.
        private void SetupTreeView()
            treeView1.Location = new Point(10, 10);
            treeView1.Size = new Size(300, 450);
            treeView1.BorderStyle = BorderStyle.FixedSingle;
            treeView1.BackColor = Color.WhiteSmoke;
            treeView1.SelectedNode = null;
            treeView1.AfterSelect +=
                new TreeViewEventHandler(treeView1_AfterSelect);


            // An index into the top-level tree nodes.
            int nodeIndex = 0;

            // An index into the first '.' character in an element name.
            int firstDotIndex = 0;

            // Initialize the element class name to compare 
            // with the class name of the first element  
            // in the Dictionary, and set this name to the first 
            // top-level node.
            StringBuilder compareClassName =
                new StringBuilder("Button");
                new TreeNode(compareClassName.ToString()));

            // The current element class name.
            StringBuilder currentClassName = new StringBuilder();

            // The text for each second-level node.
            StringBuilder nodeText = new StringBuilder();

            foreach (KeyValuePair<string, VisualStyleElement> entry
                in elementDictionary)
                // Isolate the class name of the current element.
                firstDotIndex = entry.Key.IndexOf('.');
                currentClassName.Append(entry.Key, 0, firstDotIndex);

                // Determine whether we need to increment to the next 
                // element class.
                if (currentClassName.ToString() !=
                    // Increment the index to the next top-level node 
                    // in the tree view.

                    // Get the new class name to compare with.
                    compareClassName.Remove(0, compareClassName.Length);
                        compareClassName.Length - firstDotIndex);

                    // Add a new top-level node to the tree view.
                        new TreeNode(compareClassName.ToString()));

                // Get the text for the new second-level node.
                nodeText.Append(entry.Key, firstDotIndex + 1,
                    entry.Key.Length - firstDotIndex - 1);

                // Create and insert the new second-level node.
                TreeNode newNode = new TreeNode(nodeText.ToString());
                newNode.Name = entry.Key;

                currentClassName.Remove(0, currentClassName.Length);
                nodeText.Remove(0, nodeText.Length);


        protected override void OnPaint(PaintEventArgs e)

            // Do nothing further if visual styles are disabled.
            if (!Application.RenderWithVisualStyles)
                this.Text = "Visual styles are disabled.";
                TextRenderer.DrawText(e.Graphics, this.Text, this.Font,
                    this.Location, this.ForeColor);

            // Draw the element description.
            TextRenderer.DrawText(e.Graphics, elementDescription.ToString(),
                this.Font, descriptionRect, this.ForeColor,

            // Draw the element, if an element is selected.
            if (drawElement)
                renderer.DrawBackground(e.Graphics, this.displayRect);

        // Set the element to draw.
        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            // Clear the element description.
            elementDescription.Remove(0, elementDescription.Length);

            // If the user clicked a first-level node, disable drawing.
            if (e.Node.Nodes.Count > 0)
                drawElement = false;
                elementDescription.Append("No element is selected");
                domainUpDown1.Enabled = false;

            // The user clicked an element node.
                // Add the element name to the description.

                // Get the element that corresponds to the selected  
                // node's name.
                String key = e.Node.Name;
                element = elementDictionary[key];

                // Disable resizing if the element is not defined.
                if (!VisualStyleRenderer.IsElementDefined(element))
                    drawElement = false;
                    elementDescription.Append(" is not defined.");
                    domainUpDown1.Enabled = false;
                    // Set the element to the renderer.
                    drawElement = true;
                    elementDescription.Append(" is defined.");

                    // Get the system-defined size of the element.
                    Graphics g = this.CreateGraphics();
                    currentTrueSize = renderer.GetPartSize(g,
                    displayRect.Size = currentTrueSize;

                    domainUpDown1.Enabled = true;
                    domainUpDown1.SelectedIndex = 2;


        // Resize the element display area.
        void domainUpDown1_SelectedItemChanged(object sender,
            EventArgs e)
            switch ((int)domainUpDown1.SelectedItem)
                case (int)elementSizes.TrueSize:
                    displayRect.Size = currentTrueSize;
                case (int)elementSizes.Medium:
                    displayRect.Size =
                        new Size(displayRectFull.Width / 2,
                        displayRectFull.Height / 2);
                case (int)elementSizes.Large:
                    displayRect.Size = displayRectFull.Size;


        // These values represent the options in the UpDown control.
        private enum elementSizes

.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top