Rendering Controls with Visual Styles

The .NET Framework provides support for rendering controls and other Windows user interface (UI) elements using visual styles in operating systems that support them. This topic discusses the several levels of support in the .NET Framework for rendering controls and other UI elements with the current visual style of the operating system.

Rendering Classes for Common Controls

Rendering a control refers to drawing the user interface of a control. The System.Windows.Forms namespace provides the ControlPaint class for rendering some common Windows Forms controls. However, this class draws controls in the classic Windows style, which can make it difficult to maintain a consistent UI experience when drawing custom controls in applications with visual styles enabled.

The .NET Framework 2.0 includes classes in the System.Windows.Forms namespace that render the parts and states of common controls with visual styles. Each of these classes includes static methods for drawing the control or parts of the control in a particular state with the current visual style of the operating system.

Some of these classes are designed to draw the related control regardless of whether visual styles are available. If visual styles are enabled, then the class members will draw the related control with visual styles; if visual styles are disabled, then the class members will draw the control in the classic Windows style. These classes include:

Other classes can only draw the related control when visual styles are available, and their members will throw an exception if visual styles are disabled. These classes include:

For more information on using these classes to draw a control, see How to: Use a Control Rendering Class.

Visual Style Element and Rendering Classes

The System.Windows.Forms.VisualStyles namespace includes classes that can be used to draw and get information about any control or UI element that is supported by visual styles. Supported controls include common controls that have a rendering class in the System.Windows.Forms namespace (see the previous section), as well as other controls, such as tab controls and rebar controls. Other supported UI elements include the parts of the Start menu, the taskbar, and the nonclient area of windows.

The main classes of the System.Windows.Forms.VisualStyles namespace are VisualStyleElement and VisualStyleRenderer. VisualStyleElement is a foundation class for identifying any control or user interface element supported by visual styles. In addition to VisualStyleElement itself, the System.Windows.Forms.VisualStyles namespace includes many nested classes of VisualStyleElement with static properties that return a VisualStyleElement for every state of a control, control part, or other UI element supported by visual styles.

VisualStyleRenderer provides the methods that draw and get information about each VisualStyleElement defined by the current visual style of the operating system. Information that can be retrieved about an element includes its default size, background type, and color definitions. VisualStyleRenderer wraps the functionality of the visual styles (UxTheme) API from the Windows Shell portion of the Windows Platform SDK. For more information, see Enabling Visual Styles.

For more information about using VisualStyleRenderer and VisualStyleElement, see How to: Render a Visual Style Element.

Enabling Visual Styles

To enable visual styles for an application written for the .NET Framework version 1.0, programmers must include an application manifest that specifies that ComCtl32.dll version 6 or later will be used to draw controls. Applications built with the .NET Framework version 1.1 or later can use the Application.EnableVisualStyles method of the Application class.

Checking for Visual Styles Support

The RenderWithVisualStyles property of the Application class indicates whether the current application is drawing controls with visual styles. When painting a custom control, you can check the value of RenderWithVisualStyles to determine whether you should render your control with or without visual styles. The following table lists the four conditions that must exist for RenderWithVisualStyles to return true.

Condition Notes
The operating system supports visual styles. To verify this condition separately, use the IsSupportedByOS property of the VisualStyleInformation class.
The user has enabled visual styles in the operating system. To verify this condition separately, use the IsEnabledByUser property of the VisualStyleInformation class.
Visual styles are enabled in the application. Visual styles can be enabled in an application by calling the Application.EnableVisualStyles method or by using an application manifest that specifies that ComCtl32.dll version 6 or later will be used to draw controls.
Visual styles are being used to draw the client area of application windows. To verify this condition separately, use the VisualStyleState property of the Application class and verify that it has the value VisualStyleState.ClientAreaEnabled or VisualStyleState.ClientAndNonClientAreasEnabled.

To determine when a user enables or disables visual styles, or switches from one visual style to another, check for the UserPreferenceCategory.VisualStyle value in the handlers for the SystemEvents.UserPreferenceChanging or SystemEvents.UserPreferenceChanged events.

Important

If you want to use VisualStyleRenderer to render a control or UI element when the user enables or switches visual styles, make sure that you do this when handling the UserPreferenceChanged event instead of the UserPreferenceChanging event. An exception will be thrown if you use the VisualStyleRenderer class when handling UserPreferenceChanging.

See also