Udostępnij za pośrednictwem


How to: Set the Background of a Windows Forms Panel

 

A Windows Forms Panel control can display both a background color and a background image. The BackColor property sets the background color for the contained controls, such as labels and radio buttons. If the BackgroundImage property is not set, the BackColor selection will fill the entire panel. If the BackgroundImage property is set, the image will be displayed behind the contained controls.

To set the background programmatically

  1. Set the panel's BackColor property to a value of type System.Drawing.Color.

    Panel1.BackColor = Color.AliceBlue  
    
    panel1.BackColor = Color.AliceBlue;  
    
    panel1->BackColor = Color::AliceBlue;  
    
  2. Set the panel's BackgroundImage property using the FromFile method of the System.Drawing.Image class.

    ' 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"));  
    

See Also

BackColor
BackgroundImage
Panel Control
Panel Control Overview