Control.BackColor Control.BackColor Control.BackColor Control.BackColor Property

定义

获取或设置控件的背景色。Gets or sets the background color for the control.

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

属性值

表示控件背景色的 ColorA Color that represents the background color of the control. 默认为 DefaultBackColor 属性的值。The default is the value of the DefaultBackColor property.

示例

下面的代码示例将控件BackColorForeColor和设置为默认系统颜色。The following code example sets the BackColor and ForeColor of the controls to the default system colors. 如果控件有任何子控件,则代码会以递归方式调用自身。The code recursively calls itself if the control has any child controls. 此代码示例要求你至少具有Form一个子控件; 但是,子容器控件( Panel如或GroupBox)具有其自己的子控件可以更好地演示递归。This code example requires that you have a Form with at least one child control; however, a child container control, like a Panel or GroupBox, with its own child control(s) would better demonstrate the recursion.

   // 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颜色, SupportsTransparentBackColor除非的值设置为trueBackColorThe BackColor property does not support transparent colors unless the SupportsTransparentBackColor value of System.Windows.Forms.ControlStyles is set to true.

BackColor属性为环境属性。The BackColor property is an ambient property. 环境属性是一个控件属性,如果未设置,则将从父控件中检索该属性。An ambient property is a control property that, if not set, is retrieved from the parent control. 例如,默认情况Button下,将与其BackColor父级Form相同。For example, a Button will have the same BackColor as its parent Form by default. 有关环境属性的详细信息,请参阅AmbientPropertiesControl或类概述。For more information about ambient properties, see the AmbientProperties class or the Control class overview.

继承者说明

当在派生BackColor类中重写属性时, 使用基类的BackColor属性扩展基实现。When overriding the BackColor property in a derived class, use the base class's BackColor property to extend the base implementation. 否则, 必须提供所有实现。Otherwise, you must provide all the implementation. 不需要重写get BackColor属性的和set访问器; 如果需要,只能重写一个。You are not required to override both the get and set accessors of the BackColor property; you can override only one if needed.

适用于

另请参阅