我在表单中添加了两个自定义控件,因为一个控件放在另一个控件上。将backcolor设置为control1的trasparent,但它将背面颜色显示为表单颜色而不是底层控件(control2)颜色。请分享您的想法。提前谢谢。
注意:例如我提到的图片框,但是对于任何控件(例如richtextbox或放置自定义控件)都会引发同样的问题。
图片链接: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);
}
}
答案 0 :(得分:0)
Windows窗体中的透明度遵循Windows窗口(ugh)的规则。这意味着默认情况下,在处理父子关系时,您只能获得“正确”的透明度。如果它们是所述透明控件的父项,则透明控件后面的控件将仅被绘制。
如果由于某种原因这对您不可行,您始终可以覆盖OnPaint
或OnPaintBackground
方法,并显式呈现您需要呈现的任何控件,而不管父子关系如何。当然,如果你不能做一些简化的假设,你很快就会发现为什么默认不这样做:)
作为不可能的任何简化时您需要做的快速列表: