如何在C#中制作多个透明图片框图层?

问题描述 投票:0回答:1

我正在使用3个图片框。 1作为背景,2作为背景上的透明层。都一样大小。第1层用于绘制线条,第2层用于绘制形状。我正在使用选项卡控件来控制可见的图层和隐藏的图层。但是,尽管它们都是透明的,但无论如何都无法使这两个图层同时可见。

我正在使用的代码

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;
            }
        }

任何人都知道如何使两个透明层同时可见?标签控件0都可见,1仅是picturebox1,2是仅picturebox3。选项卡控件1和2正常工作,但0仅显示图层picturebox1。

尝试添加lineLayer.Controls.Add(squareLayer);,但使程序缓冲区在执行时不停止

c# picturebox
1个回答
-2
投票

这是无法使用WinForm的PictureBox:WinForms不支持使用WPF或HTML + CSS的z混合字母(甚至索引透明)控件的z顺序。它唯一允许的控件是控件在绘制其父控件之前重新渲染其父控件Control的背景(请注意,父控件也必须也剪切其子控件,因为WinForms中的所有Control子类都封装了User32 hWnd。唯一的解决方法是创建一个没有非客户区的新顶层窗口,这可能很麻烦。]

唯一的解决方法是具有一个自定义绘制的控件,以便在您每次更改外观并使用单个OnPaint时重新绘制其覆盖的Bitmap事件中的堆叠图像或重新生成内存PictureBox。 ]参见此处:Make overlapping picturebox transparent in C#.net

© www.soinside.com 2019 - 2024. All rights reserved.