简体   繁体   中英

How to make more than 1 transparent picturebox layer in C#?

I'm using 3 pictureboxes. 1 as background & 2 as transparent layer over the background. all with same size. Layer 1 is used to draw lines & layer 2 to draw shapes. I'm using tab control to control which layer is visible and which is hidden. but somehow cant make both layer visible at the same time eventhough they both transparent.

The code I'm using

public Form1()
        {
            InitializeComponent();
            bgLayer.Image = bmp;
            bgLayer.Controls.Add(lineLayer);
            bgLayer.Controls.Add(squareLayer);
            lineLayer.Location = new Point(0, 0);
            squareLayer.Location = new Point(0, 0);
            lineLayer.BackColor = Color.Transparent;
            squareLayer.BackColor = Color.Transparent;
        }

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                lineLayer.Visible = true;
                squareLayer.Visible = true;
                lineLayer.Enabled = false;
                squareLayer.Enabled = false;
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                lineLayer.Visible = true;
                squareLayer.Visible = false;
                lineLayer.Enabled = true;
                squareLayer.Enabled = false;
            }
            else if (tabControl1.SelectedIndex == 2)
            {
                lineLayer.Visible = false;
                squareLayer.Visible = true;
                lineLayer.Enabled = false;
                squareLayer.Enabled = true;
            }
        }

Anyone know how to make both transparent layer visible at the same time? tab control 0 is both visible, 1 is picturebox1 only & 2 is picturebox3 only. Tab control 1 & 2 works fine but 0 only shows layer picturebox1.

tried adding lineLayer.Controls.Add(squareLayer); but it makes the program buffers non stop when executed

This is not possible using WinForm's PictureBox : WinForms does not support z-ordering of alpha-blended (or even index-transparency) controls the way you can using WPF or HTML+CSS. The only thing it does allow is for controls to re-render their parent Control 's background before they paint themselves (note that parent controls also necessarily clip their children too, as all Control subclasses in WinForms encapsulate a User32 hWnd. The only workaround is to create a new top-level window without a non-client area, which can be painful).

The only workaround is to have a single control that's custom painted to redraw the stacked images inside its overridden OnPaint event or to regenerate an in-memory Bitmap every time you want the appearance to change and use a single PictureBox See here: Make overlapping picturebox transparent in C#.net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM