Control 类

定义控件的基类,控件是带有可视化表示形式的组件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class Control
    Inherits Component
    Implements IDropTarget, ISynchronizeInvoke, IWin32Window, IBindableComponent, _
    IComponent, IDisposable
用法
Dim instance As Control
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class Control : Component, IDropTarget, ISynchronizeInvoke, IWin32Window, 
    IBindableComponent, IComponent, IDisposable
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
[ComVisibleAttribute(true)] 
public ref class Control : public Component, IDropTarget, ISynchronizeInvoke, IWin32Window, 
    IBindableComponent, IComponent, IDisposable
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class Control extends Component implements IDropTarget, ISynchronizeInvoke, 
    IWin32Window, IBindableComponent, IComponent, IDisposable
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class Control extends Component implements IDropTarget, ISynchronizeInvoke, 
    IWin32Window, IBindableComponent, IComponent, IDisposable

备注

若要创建自己的控件类,请从 UserControlControl 类或从 Windows 窗体提供的其他控件类继承。有关创作自定义控件的更多信息,请参见 使用 .NET Framework 开发自定义 Windows 窗体控件

Control 类实现向用户显示信息的类所需的最基本功能。它处理用户通过键盘和指针设备所进行的输入。它还处理消息路由和安全。虽然它并不实现绘制,但是它定义控件的边界(其位置和大小)。它提供窗口句柄 (hWnd)。

Windows 窗体控件使用环境属性,以便使子控件的外观接近于它们周围的环境。环境属性是一种控件属性,如果不设置,就会从父控件中检索该属性。如果控件没有 Parent 且该属性未设置,则控件尝试通过 Site 属性来确定环境属性的值。如果未放置控件,放置位置不支持环境属性,或者未在 AmbientProperties 上设置该属性,则控件使用其自己的默认值。通常,环境属性 (Property) 表示一个控件的属性 (Attribute),比如 BackColor,该属性 (Attribute) 传递给子控件。例如,默认情况下,Button 与其父级 Form 一样具有相同的 BackColorControl 类提供的环境属性包括:CursorFontBackColorForeColorRightToLeft

提示

若要使 Windows 窗体应用程序支持 Windows XP 可视样式,请确保将 FlatStyle 属性设置为 System,并在可执行文件中包括清单。清单是一个 XML 文件,或者作为资源包括在应用程序可执行文件内,或者作为单独文件驻留在该可执行文件所在的目录中。有关清单的示例,请参见 FlatStyle 的“示例”部分。有关使用 Windows XP 中可用的视觉样式的更多信息,请参见位于 https://www.microsoft.com/china/msdn/library. 上的 MSDN Library 中的“Using Windows XP Visual Styles”(使用 Windows XP 视觉样式)。

Windows 窗体具有内置的辅助功能支持,并提供有关您的应用程序的信息,这使得您的应用程序能够与具有辅助功能的客户端应用程序(如,屏幕放大器和查看器实用工具、语音输入实用工具、屏幕键盘、备用输入设备以及键盘增强实用工具)一起工作。有时您会希望为具有辅助功能的客户端应用程序提供其他信息。提供此类附加信息有两种方法。可以设置 AccessibleNameAccessibleDescriptionAccessibleDefaultActionDescriptionAccessibleRole 属性值,这些属性值将报告给辅助功能客户端应用程序。此方法通常用于为现有控件提供有限的辅助功能信息。或者,可以通过从 AccessibleObjectControl.ControlAccessibleObject 类中派生,编写自己的类,从而按照需要提供尽可能多的辅助功能信息。

提示

若要保持更好的性能,请不要在控件构造函数中设置控件的大小。首选方法是重写 DefaultSize 属性。

System.Windows.Forms 命名空间中的大多数控件都将基础的 Windows 公共控件用作生成基础。有关 Windows 公共控件的更多信息,请参见位于 https://www.microsoft.com/china/msdn/library. 上的 MSDN Library 中的“General Control Reference”(常规控件参考)主题。

若要从单独的进程中标识 Windows 窗体控件,请使用标准的 SendMessage 调用来传递 WM_GETCONTROLNAME 消息。WM_GETCONTROLNAME 与语言和 Windows 层次结构无关。有关更多信息,请参见位于 https://www.microsoft.com/china/msdn/library. 上的 MSDN library 中的“Automating Windows Forms”(自动化 Windows 窗体)部分的“Recommended Solution for Windows Forms”(Windows 窗体的建议解决方案)主题。

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Windows.Forms.Control
         派生类

线程安全

只有下列成员是线程安全的:BeginInvokeEndInvokeInvokeInvokeRequiredCreateGraphics

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control 成员
System.Windows.Forms 命名空间
Form
ScrollableControl
ContainerControl 类
Component