使用视觉样式呈现控件Rendering Controls with Visual Styles

.NET Framework 在支持使用视觉样式的操作系统中使用视觉样式为呈现控件和其他 Windows 用户界面 (UI) 元素提供支持。The .NET Framework provides support for rendering controls and other Windows user interface (UI) elements using visual styles in operating systems that support them. 本主题讨论了在 .NET Framework 中的几个级别的支持, 这些控件和其他 UI 元素都具有操作系统的当前视觉样式。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. System.Windows.Forms 命名空间提供了用来呈现某些公共 Windows 窗体控件的 ControlPaint 类。The System.Windows.Forms namespace provides the ControlPaint class for rendering some common Windows Forms controls. 但是,此类以经典 Windows 样式绘制的控件,当在启用了视觉样式的应用程序中绘制自定义控件时,难以维护 UI 体验的一致性。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.

.NET Framework 2.0 包括System.Windows.Forms命名空间中的类, 这些类可呈现具有视觉样式的公共控件的部件和状态。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. 每个这样的类都包括使用操作系统当前视觉样式绘制控件和特定状态控件部件的 static 方法。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. 如果启用了视觉样式,类成员将使用视觉样式绘制相关控件;如果禁用了视觉样式,类成员将以经典 Windows 样式绘制控件。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

System.Windows.Forms.VisualStyles 命名空间包含用于绘制和获取视觉样式支持的任何控件或 UI 元素信息的类。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. 支持的控件包括在 System.Windows.Forms 命名空间中具有呈现类的公共控件(请参阅上一节)以及诸如选项卡控件和 rebar 控件的其他控件。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. 其他受支持的 UI 元素包括“开始” 菜单、任务栏和 Windows 非工作区的各部分。Other supported UI elements include the parts of the Start menu, the taskbar, and the nonclient area of windows.

System.Windows.Forms.VisualStyles 命名空间的主要类为 VisualStyleElementVisualStyleRendererThe main classes of the System.Windows.Forms.VisualStyles namespace are VisualStyleElement and VisualStyleRenderer. VisualStyleElement 是一个基础类,用于标识视觉样式支持的任何控件或用户界面元素。VisualStyleElement is a foundation class for identifying any control or user interface element supported by visual styles. 除了 VisualStyleElement 本身, System.Windows.Forms.VisualStyles 命名空间包含许多 VisualStyleElement 嵌套类,这些类具有为视觉样式支持的控件、控件部件或其他 UI 元素的状态返回 staticVisualStyleElement 属性。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 提供一些方法,这些方法可以绘制和获取由操作系统当前视觉样式定义的每个 VisualStyleElement 的信息。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 包装来自 Windows Platform SDK 的 Windows Shell 部分的视觉样式 (UxTheme) API 的功能。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.

有关使用VisualStyleRendererVisualStyleElement的详细信息, 请参阅如何:呈现视觉样式元素For more information about using VisualStyleRenderer and VisualStyleElement, see How to: Render a Visual Style Element.

启用视觉样式Enabling Visual Styles

若要为 .NET Framework 版本1.0 编写的应用程序启用视觉样式, 程序员必须包括一个应用程序清单, 该清单指定 Comctl32.dll 版本6或更高版本将用于绘制控件。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. 使用 .NET Framework 版本1.1 或更高版本生成的应用程序Application.EnableVisualStyles可以使用Application类的方法。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

RenderWithVisualStyles 类的 Application 属性指示当前应用程序是否正在使用视觉样式绘制控件。The RenderWithVisualStyles property of the Application class indicates whether the current application is drawing controls with visual styles. 绘制自定义控件时,可以检查 RenderWithVisualStyles 的值来确定是否应使用视觉样式呈现控件。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. 下表列出了 RenderWithVisualStyles 返回 true必须存在的四个条件。The following table lists the four conditions that must exist for RenderWithVisualStyles to return true.

条件Condition 说明Notes
操作系统支持视觉样式。The operating system supports visual styles. 若要单独验证这种情况,请使用 IsSupportedByOS 类的 VisualStyleInformation 属性。To verify this condition separately, use the IsSupportedByOS property of the VisualStyleInformation class.
用户已在操作系统中启用视觉样式。The user has enabled visual styles in the operating system. 若要单独验证这种情况,请使用 IsEnabledByUser 类的 VisualStyleInformation 属性。To verify this condition separately, use the IsEnabledByUser property of the VisualStyleInformation class.
应用程序中已启用视觉样式。Visual styles are enabled in the application. 可以通过调用 Application.EnableVisualStyles 方法或使用指定用 ComCtl32.dll 版本 6 或更高版本绘制控件的应用程序清单来启用应用程序中的视觉样式。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. 若要单独验证这种情况,请使用 VisualStyleState 类的 Application 属性,验证它是否具有 VisualStyleState.ClientAreaEnabledVisualStyleState.ClientAndNonClientAreasEnabled值。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.

若要确定用户何时启用或禁用视觉样式,或何时从一种视觉样式切换到另种,请检查 UserPreferenceCategory.VisualStyleSystemEvents.UserPreferenceChanging 事件处理程序中的 SystemEvents.UserPreferenceChanged 值。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.

重要

如果想要在用户启用或切换视觉样式时使用 VisualStyleRenderer 来呈现控件或 UI 元素,请确保在处理 UserPreferenceChanged 事件而非 UserPreferenceChanging 事件时这样做。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. 处理 VisualStyleRenderer 时如果使用 UserPreferenceChanging类,则会引发异常。An exception will be thrown if you use the VisualStyleRenderer class when handling UserPreferenceChanging.

请参阅See also