Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparency issue by Overlapped PictureBox's at C#

I'm displaying in a panel PictureBox overlapped, because each PictureBox is used as a layer. First time a PictureBox is defined and added to panel it's background color is transparent, and it's images are empty.

The problem is, the bottom layer can not be seen, the transparent image, shows the panel's ground. Excepted is that the bottom PictureBox's image is seen.

I have tried to it with other controls like label. The problem could not be solved :(

Thanks.

like image 948
Cmptrb Avatar asked Dec 01 '22 11:12

Cmptrb


1 Answers

This is because, if I remember correctly, setting a background color of Transparent (its actual value is null, right?) isn't really transparent. What Windows does is it looks at the control's parent container's background color and sets the controls background color to that.

You can see this happen especially with panels. Without contents, panels set to Transparent should let you see behind them, right? Wrong. If you put a panel on top of a bunch of, say, textbox controls and set the panel to Transparent, you won't be able to see the textboxes behind it.

Instead, to get real transparency, you have to overload OnPaintBackground for the control in question and, essentially, do absolutely nothing (DONT'T call the base.OnPainBackground either!)... There's more to it than that, probably, but here is an example of a working TransparentPanel control we use here:

public class TransparentPanel : System.Windows.Forms.Panel
{
    [Browsable(false)]
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do Nothing
    }
}

We've used this class successfully to create truly transparent panels in past Windows Forms apps. We used it as a hack to fix the "right-click context menu appears on top of button controls" problem.

like image 152
Sean Hanley Avatar answered Dec 05 '22 01:12

Sean Hanley