通过


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 表示控件的背景色。 默认值为属性的值 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

注解

除非值System.Windows.Forms.ControlStyles设置为 trueSupportsTransparentBackColor否则该BackColor属性不支持透明颜色。

BackColor 属性是环境属性。 环境属性是一个控件属性,如果未设置,则从父控件中检索。 例如,默认情况下,a Button 将具有与其父级Form相同的BackColor属性。 有关环境属性的详细信息,请参阅 AmbientProperties 类或 Control 类概述。

继承者说明

重写 BackColor 派生类中的属性时,使用基类 BackColor 的属性扩展基实现。 否则,必须提供所有实现。 无需同时替代 get 属性和 set 访问器 BackColor ;仅可根据需要替代一个。

适用于

另请参阅