0

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);
    }       
}
krish bala
  • 49
  • 8
  • Why not set the visibility of the controls instead of making it transparent? – Stephen Wilson Apr 13 '16 at 11:22
  • Possible duplicate of [Transparency of picture box](http://stackoverflow.com/questions/32241014/transparency-of-picture-box) – Zohar Peled Apr 13 '16 at 11:38
  • No i have to show the controls partially . please refer the attached image – krish bala Apr 13 '16 at 11:41
  • 1
    [Take a look at this article.](http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=26878&av=611567) – Zohar Peled Apr 13 '16 at 11:43
  • @zohar Peled - Thanks for your update and it works fine if the picturebox over the other picture but it's not working if the picture box over the other control such as richtextbox – krish bala Apr 13 '16 at 12:50
  • https://support.microsoft.com/en-us/kb/943454 – Hans Passant Apr 13 '16 at 13:28
  • Unfortunately this really isn't possible in WinForms without messing with some seriously low level drawing routines. These types of limitations are one of the reasons that WPF was created, as it can handle it easily. – Bradley Uffner Apr 13 '16 at 13:54

1 Answers1

0

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
Luaan
  • 62,244
  • 7
  • 97
  • 116