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

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

问题描述

有连续三层,结果 picturebox1(.JPG) - GT; LABEL1 - > picturebox2(png格式透明)我想是让label1和pictrurebox2透明的picturebox1使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索引)在同一容器中。

  • 然后画上你的控制显卡的位图。

  • 在你的控制最后绘制的内容。

  • 另外,背景色你的控制应该是 Col​​or.Transparent

  • Draw all controls in the same container which are under your control (based on z-index) on a bitmap.
  • Then draw that bitmap on graphics of your control.
  • At last draw content of your control.
  • Also the BackColor of your control should be Color.Transparent.

下面是创建 TransparentLabel TransparentPictureBox 控制的结果。在下面的图片,有标签,图像,标签,图像,然后标签,以便和你所看到的图片框和标签已经呈现有透明的背景和尊重的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);
    }
}

透明的PictureBox

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天全站免登陆