Control 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义控件的基类,这些控件是具有可视表示形式的组件。
public ref class Control : System::ComponentModel::Component, System::ComponentModel::ISynchronizeInvoke, System::Windows::Forms::IWin32Window
public ref class Control : System::ComponentModel::Component, IDisposable, System::ComponentModel::ISynchronizeInvoke, System::Windows::Forms::IBindableComponent, System::Windows::Forms::IDropTarget, System::Windows::Forms::IWin32Window
public class Control : System.ComponentModel.Component, System.ComponentModel.ISynchronizeInvoke, System.Windows.Forms.IWin32Window
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class Control : System.ComponentModel.Component, IDisposable, System.ComponentModel.ISynchronizeInvoke, System.Windows.Forms.IBindableComponent, System.Windows.Forms.IDropTarget, System.Windows.Forms.IWin32Window
public class Control : System.ComponentModel.Component, IDisposable, System.ComponentModel.ISynchronizeInvoke, System.Windows.Forms.IBindableComponent, System.Windows.Forms.IDropTarget, System.Windows.Forms.IWin32Window
type Control = class
inherit Component
interface UnsafeNativeMethods.IOleControl
interface UnsafeNativeMethods.IOleObject
interface UnsafeNativeMethods.IOleInPlaceObject
interface UnsafeNativeMethods.IOleInPlaceActiveObject
interface UnsafeNativeMethods.IOleWindow
interface UnsafeNativeMethods.IViewObject
interface UnsafeNativeMethods.IViewObject2
interface UnsafeNativeMethods.IPersist
interface UnsafeNativeMethods.IPersistStreamInit
interface UnsafeNativeMethods.IPersistPropertyBag
interface UnsafeNativeMethods.IPersistStorage
interface UnsafeNativeMethods.IQuickActivate
interface ISynchronizeInvoke
interface IWin32Window
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Control = class
inherit Component
interface UnsafeNativeMethods.IOleControl
interface UnsafeNativeMethods.IOleObject
interface UnsafeNativeMethods.IOleInPlaceObject
interface UnsafeNativeMethods.IOleInPlaceActiveObject
interface UnsafeNativeMethods.IOleWindow
interface UnsafeNativeMethods.IViewObject
interface UnsafeNativeMethods.IViewObject2
interface UnsafeNativeMethods.IPersist
interface UnsafeNativeMethods.IPersistStreamInit
interface UnsafeNativeMethods.IPersistPropertyBag
interface UnsafeNativeMethods.IPersistStorage
interface UnsafeNativeMethods.IQuickActivate
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IBindableComponent
interface IComponent
interface IDisposable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Control = class
inherit Component
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IBindableComponent
interface IComponent
interface IDisposable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Control = class
inherit Component
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IComponent
interface IDisposable
interface IBindableComponent
type Control = class
inherit Component
interface IOleControl.Interface
interface IOleObject.Interface
interface IOleInPlaceObject.Interface
interface IOleWindow.Interface
interface IOleInPlaceActiveObject.Interface
interface IViewObject.Interface
interface IViewObject2.Interface
interface IPersist.Interface
interface IPersistStreamInit.Interface
interface IPersistPropertyBag.Interface
interface IPersistStorage.Interface
interface IQuickActivate.Interface
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IComponent
interface IDisposable
interface IBindableComponent
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Control = class
inherit Component
interface UnsafeNativeMethods.IOleControl
interface UnsafeNativeMethods.IOleObject
interface UnsafeNativeMethods.IOleInPlaceObject
interface UnsafeNativeMethods.IOleInPlaceActiveObject
interface UnsafeNativeMethods.IOleWindow
interface UnsafeNativeMethods.IViewObject
interface UnsafeNativeMethods.IViewObject2
interface UnsafeNativeMethods.IPersist
interface Interop.Ole32.IPersistStreamInit
interface UnsafeNativeMethods.IPersistPropertyBag
interface Interop.Ole32.IPersistStorage
interface UnsafeNativeMethods.IQuickActivate
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IComponent
interface IDisposable
interface IBindableComponent
type Control = class
inherit Component
interface Interop.Ole32.IOleControl
interface Interop.Ole32.IOleObject
interface Interop.Ole32.IOleInPlaceObject
interface Interop.Ole32.IOleInPlaceActiveObject
interface Interop.Ole32.IOleWindow
interface Interop.Ole32.IViewObject
interface Interop.Ole32.IViewObject2
interface Interop.Ole32.IPersist
interface Interop.Ole32.IPersistStreamInit
interface Interop.Oleaut32.IPersistPropertyBag
interface Interop.Ole32.IPersistStorage
interface Interop.Ole32.IQuickActivate
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IComponent
interface IDisposable
interface IBindableComponent
type Control = class
inherit Component
interface IDropTarget
interface ISynchronizeInvoke
interface IWin32Window
interface IComponent
interface IDisposable
interface IBindableComponent
interface IOleControl.Interface
interface IOleObject.Interface
interface IOleInPlaceObject.Interface
interface IOleWindow.Interface
interface IOleInPlaceActiveObject.Interface
interface IViewObject.Interface
interface IViewObject2.Interface
interface IPersist.Interface
interface IPersistStreamInit.Interface
interface IPersistPropertyBag.Interface
interface IPersistStorage.Interface
interface IQuickActivate.Interface
Public Class Control
Inherits Component
Implements ISynchronizeInvoke, IWin32Window
Public Class Control
Inherits Component
Implements IBindableComponent, IDisposable, IDropTarget, ISynchronizeInvoke, IWin32Window
- 继承
- 派生
- 属性
- 实现
注解
若要创建自己的控件类,请从 UserControl、 Control 类或从其他 Windows 窗体提供的控件继承。 有关创作自定义控件的详细信息,请参阅 使用 .NET Framework 开发自定义 Windows 窗体控件。
该 Control 类实现了向用户显示信息的类所需的非常基本的功能。 它通过键盘和指向设备处理用户输入。 它处理消息路由和安全性。 它定义控件(其位置和大小)的边界,尽管它不实现绘制。 它提供窗口句柄 (hWnd)。
Windows 窗体控件使用环境属性,以便子控件可以像周围的环境一样显示。 环境属性是一个控件属性,如果未设置,则从父控件中检索。 如果控件没有 Parent属性,并且未设置属性,则控件会尝试通过 Site 属性确定环境属性的值。 如果未对控件进行站点设置,如果站点不支持环境属性,或者未设置 AmbientProperties该属性,则控件使用自己的默认值。 通常,环境属性表示与子控件通信的控件的特征,例如 BackColor。 例如,默认情况下,a Button 将具有与其父级Form相同的BackColor属性。 类提供Control的环境属性包括:Cursor、、FontBackColor、ForeColor和RightToLeft。
注释
若要使 Windows 窗体应用程序支持视觉样式,请确保将 FlatStyle 属性 System 设置为可执行文件并包含清单。 清单是一个 XML 文件,该文件作为应用程序可执行文件中的资源或驻留在可执行文件所在的同一目录中的单独文件包含。 有关清单的示例,请参阅枚举的 FlatStyle “示例”部分。 有关使用视觉样式的详细信息,请参阅 视觉样式。
Windows 窗体内置了辅助功能支持,并提供有关应用程序的信息,使应用程序能够与辅助功能客户端应用程序(如屏幕放大器和审阅者实用工具、语音输入实用工具、屏幕键盘、备用输入设备和键盘增强实用工具)配合使用。 有时,需要向辅助功能客户端应用程序提供其他信息。 可通过两种方式提供此附加信息。 可以设置将报告给辅助功能客户端应用程序的 AccessibleName、 AccessibleDescription、 AccessibleDefaultActionDescription和 AccessibleRole 属性值。 此方法通常用于为现有控件提供有限的辅助功能信息。 或者,可以编写派生自 AccessibleObject 或 Control.ControlAccessibleObject 类的自己的类,根据需要提供尽可能多的辅助功能信息。
注释
若要保持更好的性能,请不要在其构造函数中设置控件的大小。 首选方法是重写 DefaultSize 属性。
注释
不要在其 Control 构造函数中添加数据绑定。 这样做会导致代码生成错误,并可能导致不需要的行为。
命名空间中的 System.Windows.Forms 大多数控件使用基础 Windows 公共控件作为基础来构建。 有关 Windows 通用控件的详细信息,请参阅 “常规控件参考”。
若要从单独的进程中识别 Windows 窗体控件,请使用标准 SendMessage 调用传递WM_GETCONTROLNAME消息。 WM_GETCONTROLNAME独立于语言和 Windows 层次结构。 有关详细信息,请参阅 自动化 Windows 窗体中的“适用于 Windows 窗体的建议解决方案”主题。
使用 InvokeRequired 属性从多个线程同步对控件的访问。 有关多线程 Windows 窗体控件的详细信息,请参阅 “如何:对 Windows 窗体控件进行 Thread-Safe 调用”。
构造函数
| 名称 | 说明 |
|---|---|
| Control() |
使用默认设置初始化类的新实例 Control 。 |
| Control(Control, String, Int32, Int32, Int32, Int32) |
将类的新实例初始化为具有特定文本、大小和位置的 Control 子控件。 |
| Control(Control, String) |
使用特定文本初始化类的新实例 Control 作为子控件。 |
| Control(String, Int32, Int32, Int32, Int32) |
使用特定文本、大小和位置初始化类的新实例 Control 。 |
| Control(String) |
使用特定文本初始化类的新实例 Control 。 |
属性
| 名称 | 说明 |
|---|---|
| AccessibilityObject |
AccessibleObject获取分配给控件的控件。 |
| AccessibleDefaultActionDescription |
获取或设置控件的默认操作说明,以供辅助功能客户端应用程序使用。 |
| AccessibleDescription |
获取或设置辅助功能客户端应用程序使用的控件的说明。 |
| AccessibleName |
获取或设置辅助功能客户端应用程序使用的控件的名称。 |
| AccessibleRole |
获取或设置控件的可访问角色。 |
| AllowDrop |
获取或设置一个值,该值指示控件是否可以接受用户拖动到其中的数据。 |
| Anchor |
获取或设置控件绑定到的容器的边缘,并确定控件的父级如何调整其大小。 |
| AutoScrollOffset |
获取或设置此控件滚动到的位置 ScrollControlIntoView(Control)。 |
| AutoSize |
此属性与此类无关。 |
| BackColor |
获取或设置控件的背景色。 |
| BackgroundImage |
获取或设置控件中显示的背景图像。 |
| BackgroundImageLayout |
获取或设置枚举中 ImageLayout 定义的背景图像布局。 |
| BindingContext |
获取或设置 BindingContext 控件。 |
| Bottom |
获取控件的下边缘与其容器工作区的上边缘之间的距离(以像素为单位)。 |
| Bounds |
获取或设置控件的大小和位置,包括其相对于父控件的非client 元素(以像素为单位)。 |
| CanEnableIme |
获取一个值,该值指示属性是否可以 ImeMode 设置为活动值,以启用 IME 支持。 |
| CanFocus |
获取一个值,该值指示控件是否可以接收焦点。 |
| CanRaiseEvents |
确定是否可以在控件上引发事件。 |
| CanSelect |
获取一个值,该值指示是否可以选择控件。 |
| Capture |
获取或设置一个值,该值指示控件是否已捕获鼠标。 |
| CausesValidation |
获取或设置一个值,该值指示控件是否导致验证在收到焦点时需要验证的任何控件上执行。 |
| CheckForIllegalCrossThreadCalls |
获取或设置一个值,该值指示在调试应用程序时是否捕获访问控件 Handle 属性的错误线程上的调用。 |
| ClientRectangle |
获取表示控件工作区的矩形。 |
| ClientSize |
获取或设置控件工作区的高度和宽度。 |
| CompanyName |
获取包含控件的应用程序的公司或创建者的名称。 |
| Container |
IContainer获取包含 .Component (继承自 Component) |
| ContainsFocus |
获取一个值,该值指示控件或其子控件之一当前是否具有输入焦点。 |
| ContextMenu |
已过时.
获取或设置与控件关联的快捷菜单。 |
| ContextMenuStrip |
获取或设置 ContextMenuStrip 与此控件关联的值。 |
| Controls |
获取控件中包含的控件的集合。 |
| Created |
获取一个值,该值指示是否已创建控件。 |
| CreateParams |
获取创建控件句柄时所需的创建参数。 |
| Cursor |
获取或设置鼠标指针位于控件上时显示的光标。 |
| DataBindings |
获取控件的数据绑定。 |
| DataContext |
获取或设置用于数据绑定的数据上下文。 这是一个环境属性。 |
| DefaultBackColor |
获取控件的默认背景色。 |
| DefaultCursor |
获取或设置控件的默认游标。 |
| DefaultFont |
获取控件的默认字体。 |
| DefaultForeColor |
获取控件的默认前景色。 |
| DefaultImeMode |
获取控件支持的默认输入法编辑器 (IME) 模式。 |
| DefaultMargin |
获取默认情况下在控件之间指定的空间(以像素为单位)。 |
| DefaultMaximumSize |
获取指定为控件的默认最大大小的长度和高度(以像素为单位)。 |
| DefaultMinimumSize |
获取指定为控件的默认最小大小的长度和高度(以像素为单位)。 |
| DefaultPadding |
获取控件内容的默认内部间距(以像素为单位)。 |
| DefaultSize |
获取控件的默认大小。 |
| DesignMode |
获取一个值,该值指示当前是否 Component 处于设计模式。 (继承自 Component) |
| DeviceDpi |
获取当前显示控件的显示设备的 DPI 值。 |
| DisplayRectangle |
获取表示控件的显示区域的矩形。 |
| Disposing |
获取一个值,该值指示基 Control 类是否正在处理。 |
| Dock |
获取或设置哪些控件边框停靠到其父控件,并确定控件如何调整其父级的大小。 |
| DoubleBuffered |
获取或设置一个值,该值指示此控件是否应使用辅助缓冲区重新绘制其表面以减少或防止闪烁。 |
| Enabled |
获取或设置一个值,该值指示控件是否可以响应用户交互。 |
| Events |
获取附加到此 Component对象的事件处理程序的列表。 (继承自 Component) |
| Focused |
获取一个值,该值指示控件是否具有输入焦点。 |
| Font |
获取或设置控件显示的文本的字体。 |
| FontHeight |
获取或设置控件字体的高度。 |
| ForeColor |
获取或设置控件的前景色。 |
| Handle |
获取控件绑定到的窗口句柄。 |
| HasChildren |
获取一个值,该值指示控件是否包含一个或多个子控件。 |
| Height |
获取或设置控件的高度。 |
| ImeMode |
获取或设置控件的输入法编辑器 (IME) 模式。 |
| ImeModeBase |
获取或设置控件的 IME 模式。 |
| InvokeRequired |
获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用调用方法,因为调用方与创建控件的线程不同。 |
| IsAccessible |
获取或设置一个值,该值指示控件是否对辅助功能应用程序可见。 |
| IsAncestorSiteInDesignMode |
指示此控件的上级位置之一是否位于 DesignMode 中。 此属性为只读。 |
| IsDisposed |
获取一个值,该值指示控件是否已释放。 |
| IsHandleCreated |
获取一个值,该值指示控件是否具有与之关联的句柄。 |
| IsMirrored |
获取一个值,该值指示控件是否镜像。 |
| LayoutEngine |
获取控件布局引擎的缓存实例。 |
| Left |
获取或设置控件左边缘与其容器工作区的左边缘之间的距离(以像素为单位)。 |
| Location |
获取或设置控件左上角相对于其容器左上角的坐标。 |
| Margin |
获取或设置控件之间的间距。 |
| MaximumSize |
获取或设置可指定上限 GetPreferredSize(Size) 的大小。 |
| MinimumSize |
获取或设置可以指定的下限 GetPreferredSize(Size) 的大小。 |
| ModifierKeys |
获取一个值,该值指示哪个修饰键(SHIFT、Ctrl 和 ALT)处于按下状态。 |
| MouseButtons |
获取一个值,该值指示哪些鼠标按钮处于按下状态。 |
| MousePosition |
获取鼠标光标在屏幕坐标中的位置。 |
| Name |
获取或设置控件的名称。 |
| Padding |
获取或设置控件中的填充。 |
| Parent |
获取或设置控件的父容器。 |
| PreferredSize |
获取控件可以容纳到的矩形区域的大小。 |
| ProductName |
获取包含控件的程序集的产品名称。 |
| ProductVersion |
获取包含控件的程序集的版本。 |
| PropagatingImeMode |
获取表示传播 IME 模式的对象。 |
| RecreatingHandle |
获取一个值,该值指示控件当前是否正在重新创建其句柄。 |
| Region |
获取或设置与控件关联的窗口区域。 |
| RenderRightToLeft |
已过时.
已过时.
此属性现已过时。 |
| ResizeRedraw |
获取或设置一个值,该值指示控件在调整大小时是否重新绘制自身。 |
| Right |
获取控件右边缘与其容器工作区的左边缘之间的距离(以像素为单位)。 |
| RightToLeft |
获取或设置一个值,该值指示控件的元素是否对齐以支持使用从右到左字体的区域设置。 |
| ScaleChildren |
获取一个值,该值确定子控件的缩放。 |
| ShowFocusCues |
获取一个值,该值指示控件是否应显示焦点矩形。 |
| ShowKeyboardCues |
获取一个值,该值指示用户界面是否处于适当的状态以显示或隐藏键盘加速器。 |
| Site |
获取或设置控件的站点。 |
| Size |
获取或设置控件的高度和宽度。 |
| TabIndex |
获取或设置控件在其容器中的 Tab 键顺序。 |
| TabStop |
获取或设置一个值,该值指示用户是否可以使用 TAB 键向此控件提供焦点。 |
| Tag |
获取或设置包含有关控件的数据的对象。 |
| Text |
获取或设置与此控件关联的文本。 |
| Top |
获取或设置控件上边缘与其容器工作区上边缘之间的距离(以像素为单位)。 |
| TopLevelControl |
获取其他 Windows 窗体控件未父控件的父控件。 通常,这是控件包含在的最外层 Form 。 |
| UseWaitCursor |
获取或设置一个值,该值指示是否对当前控件和所有子控件使用等待游标。 |
| Visible |
获取或设置一个值,该值指示是否显示控件及其所有子控件。 |
| Width |
获取或设置控件的宽度。 |
| WindowTarget |
此属性与此类无关。 |
方法
活动
显式接口实现
适用于
线程安全性
只有以下成员是线程安全的: BeginInvoke(Delegate)、 EndInvoke(IAsyncResult)、 Invoke(Delegate), InvokeRequired以及 CreateGraphics() 控件的句柄是否已创建。 在后台线程上创建控件的句柄之前调用 CreateGraphics() 可能会导致非法跨线程调用。