如何:设置 Windows 窗体面板的背景

Windows 窗体 Panel 控件可以同时显示背景色和背景图像。 该 BackColor 属性设置所包含的控件的背景色,如标签和单选按钮。 如果未设置该 BackgroundImage 属性,则 BackColor 所选内容将填充整个面板。 如果设置了该 BackgroundImage 属性,图像将显示在包含的控件后面。

以编程方式设置背景

  1. 将面板 BackColor 的属性设置为类型的 System.Drawing.Color值。

    Panel1.BackColor = Color.AliceBlue
    
    panel1.BackColor = Color.AliceBlue;
    
    panel1->BackColor = Color::AliceBlue;
    
  2. 使用System.Drawing.Image类的FromFile方法设置面板BackgroundImage的属性。

    ' You should replace the bolded image
    ' in the sample below with an image of your own choosing.
    Panel1.BackgroundImage = Image.FromFile _
        (System.Environment.GetFolderPath _
        (System.Environment.SpecialFolder.Personal) _
        & "\Image.gif")
    
    // You should replace the bolded image
    // in the sample below with an image of your own choosing.
    // Note the escape character used (@) when specifying the path.
    panel1.BackgroundImage = Image.FromFile
       (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\Image.gif");
    
    // You should replace the bolded image
    // in the sample below with an image of your own choosing.
    panel1->BackgroundImage = Image::FromFile(String::Concat(
       System::Environment::GetFolderPath
       (System::Environment::SpecialFolder::Personal),
       "\\Image.gif"));
    

另请参阅