krish bala
krish bala

Reputation: 49

How to make the underlying control visible instead of form when set the trasparent backcolor

I have added two custom controls in the form as one control placed over the another control. Set the backcolor as trasparent for control1 but it shows the back color as form color instead of underlying control(control2) color. Please share your ideas . Thanks in advance.

Note : For example i have mentioned as picturebox but same problem raises for any controls such as richtextbox or placing the custom controls.

Image link : IssueImage

private void InitializeComponent()
{
    #region picturebox

    this.BackColor = Color.Aquamarine;
    this.WindowState = FormWindowState.Normal;            

    var selectBtn = new Button();
    selectBtn.Size = new Size(100, 30);
    selectBtn.Location = new Point(10, 10);
    selectBtn.Text = "Click";
    //selectBtn.Click += selectBtn_Click;

    var picturebox = new PictureBox();
    picturebox.Size = new Size(140, 110);
    picturebox.Location = new Point(4, 4);
    picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
    picturebox.Image = new Bitmap(@"..\..\Data\graphic1.png");            

    var picturebox2 = new PictureBox();
    picturebox2.Size = new Size(140, 110);
    picturebox2.Location = new Point(4, 4);
    picturebox2.SizeMode = PictureBoxSizeMode.StretchImage;
    picturebox2.Image = new Bitmap(@"..\..\Data\graphic1.png");

    graphiccell = new GraphicCellControl();
    graphiccell.Location = new Point(50, 200);
    graphiccell.BackColor = Color.Transparent;
    graphiccell.Size = new Size(160, 130);

    var graphiccell2 = new GraphicCellControl();
    graphiccell2.Location = new Point(100, 250);
    graphiccell2.BackColor = Color.Red;
    graphiccell2.Size = new Size(160, 130);
    // graphiccell2.BackColor = Color.Transparent;
    graphiccell.Controls.Add(picturebox);
    graphiccell2.Controls.Add(picturebox2);
    this.Controls.Add(graphiccell);
    this.Controls.Add(graphiccell2);
    this.Controls.Add(selectBtn);

    #endregion
}

public class GraphicCellControl : Control
{
    public GraphicCellControl()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    }       
}

Upvotes: 0

Views: 55

Answers (1)

Luaan
Luaan

Reputation: 63772

Transparency in Windows Forms follows the rules for Windows windows (ugh). This means that by default, you only get "proper" transparency when you're dealing with the parent-child relationship. Controls behind a transparent control will only be drawn if they are parents of said transparent control.

If this isn't feasible for you for some reason, you can always override the OnPaint or OnPaintBackground methods, and explicitly render whatever control you need to render regardless of the parent-child relationship. Of course, you'll quickly find why this isn't done by default if you can't do a few simplifying assumptions :)

As a quick list of what you need to do when there aren't any simplifications possible:

  1. Find controls that are behind the transparent control - this is especially tricky when there isn't any spatial partitioning (like the mentioned parent-child relationship)
  2. Render the relevant surfaces of the controls from 1. in back to front order on the current control's surface
  3. Optimally, ensure that all the overlapping transparent controls avoid rendering the same controls multiple times
  4. Ensure that all operations that can potentially change the background of the transparent control cause an invalidation (and thus re-render) of the transparent control's background

Upvotes: 0

Related Questions