【问题标题】:How to make two transparent layer with c#?如何用c#制作两个透明层?
【发布时间】:2017-09-11 03:13:14
【问题描述】:

有三个连续的层,
picturebox1(.jpg) -> label1 -> picturebox2(.png transparent) 我想要的是让 label1 和 pictrurebox2 对 picturebox1 透明,这样 label1 可以通过 picturebox2 看到但它不起作用..

public Form1()
{
    InitializeComponent();
    label1.Parent = pictureBox1;
    label1.BackColor = Color.Transparent;
    pictureBox2.Parent = pictureBox1;
    pictureBox2.BackColor = Color.Transparent;
    picturebox2.BringToFront(); 
}

所以请帮帮我

【问题讨论】:

  • 不确定定位,但您可能需要使标签成为pb2 的子标签,即所有控件必须一一嵌套。这意味着:没有重叠!
  • 只能看到Parent,堆叠效果不起作用。最简单的解决方法是在pictureBox1 的Paint 事件中使用TextRenderer.DrawText() 而不是Label。也便宜很多,但你必须写代码。顺便说一句,无需停在那里,现在您也可以在该事件中使用 Graphics.DrawImage() 并且不再需要 pictureBox2。

标签: c# .net winforms custom-controls gdi+


【解决方案1】:

如果您需要控件支持透明度,则应覆盖控件的绘制并按此顺序绘制控件:

  • 在位图上绘制同一容器中受您控制(基于 z-index)的所有控件。
  • 然后在控件的图形上绘制该位图。
  • 最后绘制控件的内容。
  • 您的控件的BackColor 也应为Color.Transparent

这是创建TransparentLabelTransparentPictureBox 控件的结果。在下图中,依次是标签、图像、标签、图像和标签,如您所见,图片框和标签已被渲染为具有透明背景并尊重 z-index:

透明标签

class TransparentLabel : Label
{
    public TransparentLabel()
    {
        this.BackColor = Color.Transparent;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (Parent != null && this.BackColor == Color.Transparent)
        {
            using (var bmp = new Bitmap(Parent.Width, Parent.Height))
            {
                Parent.Controls.Cast<Control>()
                      .Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
                      .Where(c => c.Bounds.IntersectsWith(this.Bounds))
                      .OrderByDescending(c => Parent.Controls.GetChildIndex(c))
                      .ToList()
                      .ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

                e.Graphics.DrawImage(bmp, -Left, -Top);

            }
        }
        base.OnPaint(e);
    }
}

透明图片框

class TransparentPictureBox : PictureBox
{
    public TransparentPictureBox()
    {
        this.BackColor = Color.Transparent;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (Parent != null && this.BackColor==Color.Transparent)
        {
            using (var bmp = new Bitmap(Parent.Width, Parent.Height))
            {
                Parent.Controls.Cast<Control>()
                      .Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
                      .Where(c => c.Bounds.IntersectsWith(this.Bounds))
                      .OrderByDescending(c => Parent.Controls.GetChildIndex(c))
                      .ToList()
                      .ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

                e.Graphics.DrawImage(bmp, -Left, -Top);

            }
        }
        base.OnPaint(e);
    }
}  

【讨论】:

  • 是的,我需要这个作为图片,但是有什么方法可以做到这一点吗?
  • Windows 窗体控件中没有对此的内置支持。您可以像这样自定义控件或创建新控件。如果您只需要文本和图像,作为另一种选择,您可以使用 GDI+ 在控件中创建多个图层,而无需使用多个透明控件。
猜你喜欢
  • 2013-05-22
  • 1970-01-01
  • 2010-09-28
  • 2021-04-28
  • 1970-01-01
  • 2017-08-31
  • 2012-09-26
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多