Control.BackColor 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定控制項的背景色彩。
public:
virtual property System::Drawing::Color BackColor { System::Drawing::Color get(); void set(System::Drawing::Color value); };
public virtual System.Drawing.Color BackColor { get; set; }
member this.BackColor : System.Drawing.Color with get, set
Public Overridable Property BackColor As Color
Color ,代表控制項的背景色彩。 預設值為 DefaultBackColor 屬性的值。
下列程式碼範例會將 BackColor 控制項的 和 ForeColor 設定為預設系統色彩。 如果控制項有任何子控制項,程式碼會以遞迴方式呼叫本身。 此程式碼範例需要您具有 Form 至少一個子控制項的 ;不過,子容器控制項,例如 Panel 或 GroupBox ,其本身的子控制項 () 會更能示範遞迴。
// Reset all the controls to the user's default Control color.
private:
void ResetAllControlsBackColor( Control^ control )
{
control->BackColor = SystemColors::Control;
control->ForeColor = SystemColors::ControlText;
if ( control->HasChildren )
{
// Recursively call this method for each child control.
IEnumerator^ myEnum = control->Controls->GetEnumerator();
while ( myEnum->MoveNext() )
{
Control^ childControl = safe_cast<Control^>(myEnum->Current);
ResetAllControlsBackColor( childControl );
}
}
}
// Reset all the controls to the user's default Control color.
private void ResetAllControlsBackColor(Control control)
{
control.BackColor = SystemColors.Control;
control.ForeColor = SystemColors.ControlText;
if(control.HasChildren)
{
// Recursively call this method for each child control.
foreach(Control childControl in control.Controls)
{
ResetAllControlsBackColor(childControl);
}
}
}
' Reset all the controls to the user's default Control color.
Private Sub ResetAllControlsBackColor(control As Control)
control.BackColor = SystemColors.Control
control.ForeColor = SystemColors.ControlText
If control.HasChildren Then
' Recursively call this method for each child control.
Dim childControl As Control
For Each childControl In control.Controls
ResetAllControlsBackColor(childControl)
Next childControl
End If
End Sub
除非 BackColor 的值 System.Windows.Forms.ControlStyles 設定 true
為 ,否則 SupportsTransparentBackColor
屬性不支援透明色彩。
屬性 BackColor 是環境屬性。 環境屬性是控制項屬性,如果未設定,則會從父控制項擷取。 例如, Button 預設會有與其父系 Form 相同的 BackColor 。 如需環境屬性的詳細資訊,請參閱 AmbientProperties 類別或 Control 類別概觀。
在衍生類別中覆 BackColor 寫屬性時,請使用基類的 BackColor 屬性來擴充基底實作。 否則,您必須提供所有實作。 您不需要同時覆寫 get
屬性的 BackColor 和 set
存取子;您可以視需要只覆寫一個。
產品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
Windows Desktop | 3.0, 3.1, 5, 6, 7 |