如何设置 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"));  
    

另请参阅