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,控制項可接受 BackColor 與小於 255 的 Alpha 元件以模擬透明度。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 位 (或位) 和布林值來設定) 的位 (。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,並且 OnPaintBackground OnPaint 會直接從視窗訊息 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. 您可以傳送視窗訊息 WM_ERASEBKGRND,以達成類似 SupportsTransparentBackColor 的虛擬透明效果;例如,具有一般 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.OnMouseEnter 、、、 Control.OnMouseMove Control.OnMouseHover Control.OnMouseLeaveControl.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 ,就會將 click 傳遞給 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. 但是, OnClick OnDoubleClick 不論 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. 否則會改用上述控制項的文字 LabelOtherwise, the text of the preceding Label control will be used instead. 預設會設定這個樣式。This style is set by default. 某些內建的控制項類型(例如 TextBoxComboBox )會重設此樣式,以便 Text Active 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.