ControlStyles 枚举


指定控件的样式和行为。Specifies the style and behavior of a control.

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

public enum class ControlStyles
public enum ControlStyles
type ControlStyles = 
Public Enum ControlStyles


AllPaintingInWmPaint 8192

如果为 true,则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁。If true, the control ignores the window message WM_ERASEBKGND to reduce flicker. 仅当将 UserPaint 位设置为 true 时,才应用此样式。This style should only be applied if the UserPaint bit is set to true.

CacheText 16384

如果为 true,则控件将保留文本的副本,而不是每次必需时从 Handle 中获取。If true, the control keeps a copy of the text rather than getting it from the Handle each time it is needed. 此样式默认为 falseThis style defaults to false. 此行为可提高性能,但很难保持文本同步。This behavior improves performance, but makes it difficult to keep the text synchronized.

ContainerControl 1

如果为 true,则控件是类容器控件。If true, the control is a container-like control.

DoubleBuffer 65536

如果为 true,则在缓冲区中进行绘制,并且完成后将结果输出到屏幕。If true, drawing is performed in a buffer, and after it completes, the result is output to the screen. 双缓冲可以防止因重绘控件而引起的闪烁。Double-buffering prevents flicker caused by the redrawing of the control. 如果将 DoubleBuffer 设置为 true,则还应将 UserPaintAllPaintingInWmPaint 设置为 trueIf you set DoubleBuffer to true, you should also set UserPaint and AllPaintingInWmPaint to true.

EnableNotifyMessage 32768

如果为 true,则将对发送到控件的 WndProc(Message) 的每个消息调用 OnNotifyMessage(Message) 方法。If true, the OnNotifyMessage(Message) method is called for every message sent to the control's WndProc(Message). 此样式默认为 falseThis style defaults to false. EnableNotifyMessage 在部分信任中不起作用。EnableNotifyMessage does not work in partial trust.

FixedHeight 64

如果为 true,则控件在自动缩放时具有固定高度。If true, the control has a fixed height when auto-scaled. 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Height 保持不变。For example, if a layout operation attempts to rescale the control to accommodate a new Font, the control's Height remains unchanged.

FixedWidth 32

如果为 true,则控件在自动缩放时具有固定宽度。If true, the control has a fixed width when auto-scaled. 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Width 保持不变。For example, if a layout operation attempts to rescale the control to accommodate a new Font, the control's Width remains unchanged.

Opaque 4

如果为 true,则控件会绘制为不透明,且不绘制背景。If true, the control is drawn opaque and the background is not painted.

OptimizedDoubleBuffer 131072

如果为 true,则控件将首先绘制到缓冲区而不是直接绘制到屏幕,这可以减少闪烁。If true, the control is first drawn to a buffer rather than directly to the screen, which can reduce flicker. 如果将此属性设置为 true,则还应将 AllPaintingInWmPaint 设置为 trueIf you set this property to true, you should also set the AllPaintingInWmPaint to true.

ResizeRedraw 16

如果为 true,则控件会在调整大小时进行重绘。If true, the control is redrawn when it is resized.

Selectable 512

如果为 true,则控件可以接收焦点。If true, the control can receive focus.

StandardClick 256

如果为 true,则控件实现标准 Click 行为。If true, the control implements the standard Click behavior.

StandardDoubleClick 4096

如果为 true,则控件实现标准 DoubleClick 行为。If true, the control implements the standard DoubleClick behavior. 如果未将 StandardClick 位设置为 true,则忽略此样式。This style is ignored if the StandardClick bit is not set to true.

SupportsTransparentBackColor 2048

如果为 true,则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度。If true, the control accepts a BackColor with an alpha component of less than 255 to simulate transparency. 仅当将 UserPaint 位设置为 true 且父控件从 Control 派生时,才会模拟透明度。Transparency will be simulated only if the UserPaint bit is set to true and the parent control is derived from Control.

UserMouse 1024

如果为 true,则将由控件而不是操作系统处理其自身的鼠标事件。If true, the control does its own mouse processing, and mouse events are not handled by the operating system.

UserPaint 2

如果为 true,则会由控件而不是由操作系统来绘制控件自身。If true, the control paints itself rather than the operating system doing so. 如果 false,则不会引发 Paint 事件。If false, the Paint event is not raised. 此样式仅适用于从 Control 派生的类。This style only applies to classes derived from Control.

UseTextForAccessibility 262144

指定控件的 Text 属性的值,若设置,则确定控件的默认 Active Accessibility 名称和快捷键。Specifies that the value of the control's Text property, if set, determines the control's default Active Accessibility name and shortcut key.


下面的示例演示的一种用法ControlStylesStyleChanged事件。The following example demonstrates a use of ControlStyles with the StyleChanged event.

   // 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" );
// 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");
' 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

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

' 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


控件使用此枚举中各种属性和方法指定功能。Controls use this enumeration in various properties and methods to specify functionality. 控件可以通过调用启用一种样式SetStyle方法并传入相应ControlStyles位 (或 bits) 和要设置该位的布尔值。A control can enable a style by calling the SetStyle method and passing in the appropriate ControlStyles bit (or bits) and the Boolean value to set the bit(s) to. 例如,以下 Visual Basic 代码行将启用双缓冲处理。For example, the following line of Visual Basic code would enable double-buffering.

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

如果 AllPaintingInWmPaint 位设置为true,窗口消息 WM_ERASEBKGND 将被忽略,且两OnPaintBackgroundOnPaint直接从窗口消息 WM_PAINT 调用方法。If the AllPaintingInWmPaint bit is set to true, the window message WM_ERASEBKGND is ignored, and both OnPaintBackground and OnPaint methods are called directly from the window message WM_PAINT. 这通常会减少闪烁,除非其他控件的窗口消息 WM_ERASEBKGND 发送到控件。This generally reduces flicker unless other controls send the window message WM_ERASEBKGND to the control. 可能会发送要实现类似于 SupportsTransparentBackColor; 伪透明效果的窗口消息 WM_ERASEBKGRND例如,ToolBar具有平面外观就是如此。You might send the window message WM_ERASEBKGRND to achieve a pseudo-transparent effect similar to SupportsTransparentBackColor; for example, a ToolBar with flat appearance does this.

若要完全启用双缓冲处理,可以将 OptimizedDoubleBuffer 和 AllPaintingInWmPaint 位设置为trueTo fully enable double-buffering, you can set the OptimizedDoubleBuffer and AllPaintingInWmPaint bits to true. 但是这将产生相同的结果,启用双缓冲的首选的方法是设置DoubleBuffered属性来控制对trueHowever the preferred method for enabling double buffering, which yields the same result, is to set the DoubleBuffered property for the control to true.

如果 SupportsTransparentBackColor 位设置为true,并BackColor设置为一种颜色的 alpha 分量为小于 255,OnPaintBackground将通过询问其父控件来绘制背景来模拟透明度。If the SupportsTransparentBackColor bit is set to true, and the BackColor is set to a color whose alpha component is less than 255, OnPaintBackground will simulate transparency by asking its parent control to paint the background. 这不是真正的透明。This is not true transparency.


如果没有控件及其父级之间的另一个控件,当前控件将不显示在中间的控件。If there is another control between the control and its parent, the current control will not show the control in the middle.

当 UserMouse 位设置为true,仍调用以下方法: Control.OnMouseDownControl.OnMouseUpControl.OnMouseEnterControl.OnMouseMoveControl.OnMouseHoverControl.OnMouseLeave,和Control.OnMouseWheelWhen the UserMouse bit is set to true, the following methods are still called: Control.OnMouseDown, Control.OnMouseUp, Control.OnMouseEnter, Control.OnMouseMove, Control.OnMouseHover, Control.OnMouseLeave, and Control.OnMouseWheel.

单击控件时,如果 StandardClick 位设置为trueControl.OnClick调用方法,它将引发Control.Click事件。When the control is clicked, if the StandardClick bit is set to true the Control.OnClick method is called and it raises the Control.Click event. 当双击控件,并且 StandardClick 和 StandardDoubleClick 位将设置为true,单击传递给DoubleClick事件。When the control is double-clicked, and both the StandardClick and StandardDoubleClick bits are set to true, the click is passed on to the DoubleClick event. 然后Control.OnDoubleClick调用方法,它将引发Control.DoubleClick事件。Then the Control.OnDoubleClick method is called and it raises the Control.DoubleClick event. 但是,可以调用该控件OnClickOnDoubleClick直接而不考虑 StandardClick 和 StandardDoubleClick 位的值。However, the control can call OnClick or OnDoubleClick directly regardless of the StandardClick and StandardDoubleClick bit values. 有关详细信息控件上单击和双击行为,请参阅Control.ClickControl.DoubleClick主题。For more information on control click and double click behaviors, see the Control.Click and Control.DoubleClick topics.

当设置 UseTextForAccessibility 位并且没有在该控件的值Text属性中,该控件的值Text属性确定控件的默认 Active Accessibility 名称和快捷键。When the UseTextForAccessibility bit is set and there is a value in the control's Text property, the value of that control's Text property determines the control's default Active Accessibility name and shortcut key. 否则为前面的文本Label控件将改为使用。Otherwise, the text of the preceding Label control will be used instead. 默认情况下设置了此样式。This style is set by default. 某些内置控件类型,如TextBoxComboBox,重置此样式,以便TextActive Accessibility 将不使用这些控件的属性。Certain built-in control types, such as TextBox and ComboBox, reset this style so that the Text property of those controls will not be used by Active Accessibility.