Control.BackColor 屬性

定義

取得或設定控制項的背景色彩。

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

Color ,代表控制項的背景色彩。 預設值為 DefaultBackColor 屬性的值。

範例

下列程式碼範例會將 BackColor 控制項的 和 ForeColor 設定為預設系統色彩。 如果控制項有任何子控制項,程式碼會以遞迴方式呼叫本身。 此程式碼範例需要您具有 Form 至少一個子控制項的 ;不過,子容器控制項,例如 PanelGroupBox ,其本身的子控制項 () 會更能示範遞迴。

   // 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 屬性的 BackColorset 存取子;您可以視需要只覆寫一個。

適用於

另請參閱