如何用c#制作两个透明层? [英] How to make two transparent layer with c#?

查看:54
本文介绍了如何用c#制作两个透明层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连续三层,
picturebox1(.jpg) ->标签 1 ->picturebox2(.png透明)我想要的是使label1和pictrurebox2对pict​​urebox1透明,以便label1可以通过picturebox2看到,但它不起作用..

There are three consecutive layers,
picturebox1(.jpg) -> label1 -> picturebox2(.png transparent) what i want is to make the label1 and pictrurebox2 transparent to the picturebox1 so that label1 can be see through the picturebox2 but its not working..

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

所以请帮帮我

推荐答案

如果你需要一个控件支持透明度,你应该覆盖控件的绘制并按这个顺序绘制控件:

If you need a control support transparency, you should override painting of the control and draw the control in this order:

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

这是创建TransparentLabelTransparentPictureBox 控件的结果.在下图中,按顺序有标签、图像、标签、图像和标签,如您所见,图片框和标签已呈现透明背景并尊重 z-index:

Here is the result of creating TransparentLabel and TransparentPictureBox controls. In the below image, there is label, image, label, image and then label in order and as you can see picture boxes and labels has been rendered having transparent background and respecting the 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);
    }
}  

这篇关于如何用c#制作两个透明层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆