我用了3个图片盒。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);,但它使程序在执行时不停止缓冲
发布于 2020-01-09 08:03:41
使用WinForm是不可能的: WinForms不支持alpha混合(甚至索引透明)控件的z排序,而您可以使用PictureBox或HTML+CSS。它唯一允许的是控件在绘制自己之前重新呈现其父Control的背景(请注意,父控件也必须裁剪它们的子控件,因为WinForms中的所有Control子类都封装了一个User32 hWnd。唯一的解决办法是创建一个没有非工作区的新的顶级窗口,这可能会很痛苦)。
唯一的解决办法是拥有一个自定义绘制的控件,以便在其重写的OnPaint事件内重新绘制堆叠图像,或者在每次您希望更改外观时重新生成内存中的Bitmap,并使用单个PictureBox请参见此处:Make overlapping picturebox transparent in C#.net
https://stackoverflow.com/questions/59640764
复制相似问题