ControlStyles 枚举

指定控件的样式和行为。

此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。

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

语法

声明
<FlagsAttribute> _
Public Enumeration ControlStyles
用法
Dim instance As ControlStyles
[FlagsAttribute] 
public enum ControlStyles
[FlagsAttribute] 
public enum class ControlStyles
/** @attribute FlagsAttribute() */ 
public enum ControlStyles
FlagsAttribute 
public enum ControlStyles

成员

  成员名称 说明
AllPaintingInWmPaint 如果为 true,控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁。仅当 UserPaint 位设置为 true 时,才应当应用该样式。 
CacheText 如果为 true,控件保留文本的副本,而不是在每次需要时从 Handle 获取文本副本。此样式默认为 false。此行为提高了性能,但使保持文本同步变得困难。 
ContainerControl 如果为 true,则控件是类似容器的控件。 
DoubleBuffer 如果为 true,则绘制在缓冲区中进行,完成后将结果输出到屏幕上。双重缓冲区可防止由控件重绘引起的闪烁。如果将 DoubleBuffer 设置为 true,则还应当将 UserPaintAllPaintingInWmPaint 设置为 true。 
EnableNotifyMessage 如果为 true,则为发送到控件的 WndProc 的每条消息调用 OnNotifyMessage 方法。此样式默认为 falseEnableNotifyMessage 在部分可信的情况下不工作。 
FixedHeight 如果为 true,则自动缩放时,控件具有固定高度。例如,如果布局操作试图重新缩放控件以适应新的 Font,则控件的 Height 将保持不变。 
FixedWidth 如果为 true,则自动缩放时,控件具有固定宽度。例如,如果布局操作试图重新缩放控件以适应新的 Font,则控件的 Width 将保持不变。 
Opaque 如果为 true,则控件被绘制为不透明的,不绘制背景。 
OptimizedDoubleBuffer 如果为 true,则该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁。如果将此属性设置为 true,则还应当将 AllPaintingInWmPaint 设置为 true。 
ResizeRedraw 如果为 true,则在调整控件大小时重绘控件。 
Selectable 如果为 true,则控件可以接收焦点。 
StandardClick 如果为 true,则控件将实现标准 Click 行为。 
StandardDoubleClick 如果为 true,则控件将实现标准 DoubleClick 行为。如果 StandardClick 位未设置为 true,则忽略此样式。 
SupportsTransparentBackColor 如果为 true,控件接受 alpha 组件小于 255 的 BackColor 以模拟透明。仅在 UserPaint 位设置为 true 并且父控件派生自 Control 时才模拟透明。 
UserMouse 如果为 true,则控件完成自己的鼠标处理,因而鼠标事件不由操作系统处理。 
UserPaint 如果为 true,控件将自行绘制,而不是通过操作系统来绘制。如果为 false,将不会引发 Paint 事件。此样式仅适用于派生自 Control 的类。 
UseTextForAccessibility 指定该控件的 Text 属性的值,如果已设置,则可确定该控件的默认 Active Accessibility 名称和快捷键。 

备注

控件在各种属性和方法中使用此枚举指定功能。控件可以通过调用 SetStyle 方法并传入适当的 ControlStyles 位以及设置该位的 Boolean 值来启用样式。例如,下面的一行 Visual Basic 代码将会启用双重缓冲。

 myControl.SetStyle(UserPaint Or AllPaintingInWmPaint Or DoubleBuffer, True)

如果将 AllPaintingInWmPaint 位设置为 true,则将忽略 WM_ERASEBKGND 窗口消息,而直接从 WM_PAINT 窗口消息调用 OnPaintBackgroundOnPaint 方法。这通常可减少闪烁,除非其他控件将 WM_ERASEBKGND 窗口消息发送到该控件。可以发送 WM_ERASEBKGRND 窗口消息以达到与 SupportsTransparentBackColor 相似的假透明效果;例如,具有平面外观的 ToolBar 就采用这种方法。

若要完全启用双缓冲,可以将 OptimizedDoubleBufferAllPaintingInWmPaint 位设置为 true。但是,启用双缓冲的首选方法是将该控件的 DoubleBuffered 属性设置为 true,这会产生同样的结果。

如果 SupportsTransparentBackColor 位设置为 true,并且 BackColor 被设置为 alpha 组件小于 255 的颜色,则 OnPaintBackground 将通过请求其父控件绘制背景来模拟透明。但这不是真正的透明。

提示

如果在控件与其父控件之间还有另一个控件,则当前控件不会显示中间的控件。

UserMouse 位设置为 true 时,仍将调用以下方法:Control.OnMouseDownControl.OnMouseUpControl.OnMouseEnterControl.OnMouseMoveControl.OnMouseHoverControl.OnMouseLeaveControl.OnMouseWheel

单击控件时,如果 StandardClick 位设置为 true,则 Control.OnClick 方法被调用,它将引发 Control.Click 事件。双击控件并且 StandardClickStandardDoubleClick 位都设置为 true 时,会将此次单击传递给 DoubleClick 事件。随后,Control.OnDoubleClick 方法被调用,此方法将引发 Control.DoubleClick 事件。然而,无论 StandardClickStandardDoubleClick 位为何值,控件都可直接调用 OnClickOnDoubleClick。有关控件单击和双击行为的更多信息,请参见 Control.ClickControl.DoubleClick 这两个主题。

当已设置 UseTextForAccessibility 位和该控件的 Text 属性值时,该控件的 Text 属性值可确定控件的默认的 Active Accessibility 名称和快捷键。否则,将改用前面的 Label 控件的文本。此样式为默认设置。某些内置控件类型(如 TextBoxComboBox)会重置此样式,因此 Active Accessibility 不会使用那些控件的 Text 属性。

给继承者的说明 如果该控件不支持 ClickDoubleClick 事件,那么从标准 Windows 窗体控件继承并将 StandardClickStandardDoubleClick 位值更改为 true 会导致意外的行为,或者根本不会产生任何效果。

示例

下面的示例演示如何通过 StyleChanged 事件使用 ControlStyles

' Set the 'FixedHeight' and 'FixedWidth' styles to false.
Private Sub MyForm_Load(sender As Object, e As EventArgs)
   Me.SetStyle(ControlStyles.FixedHeight, False)
   Me.SetStyle(ControlStyles.FixedWidth, False)
End Sub 'MyForm_Load

Private Sub RegisterEventHandler()
   AddHandler Me.StyleChanged, AddressOf MyForm_StyleChanged
End Sub 'RegisterEventHandler

' Handle the 'StyleChanged' event for the 'Form'.
Private Sub MyForm_StyleChanged(sender As Object, e As EventArgs)
   MessageBox.Show("The style releated to the 'Form' has been changed")
End Sub 'MyForm_StyleChanged
// Set the 'FixedHeight' and 'FixedWidth' styles to false.
private void MyForm_Load(object sender, EventArgs e)
{
   this.SetStyle(ControlStyles.FixedHeight, false);
   this.SetStyle(ControlStyles.FixedWidth, false);
}

private void RegisterEventHandler()
{
   this.StyleChanged += new EventHandler(MyForm_StyleChanged);
}

// Handle the 'StyleChanged' event for the 'Form'.
private void MyForm_StyleChanged(object sender, EventArgs e)
{
   MessageBox.Show("The style releated to the 'Form' has been changed");
}
private:
   // Set the 'FixedHeight' and 'FixedWidth' styles to false.
   void MyForm_Load( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      this->SetStyle( ControlStyles::FixedHeight, false );
      this->SetStyle( ControlStyles::FixedWidth, false );
   }

   void RegisterEventHandler()
   {
      this->StyleChanged += gcnew EventHandler( this, &MyForm::MyForm_StyleChanged );
   }

   // Handle the 'StyleChanged' event for the 'Form'.
   void MyForm_StyleChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      MessageBox::Show( "The style releated to the 'Form' has been changed" );
   }

平台

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

请参见

参考

System.Windows.Forms 命名空间
Control.SetStyle
Control.GetStyle