如何制作具有真正透明背景的面板

How to make a panel with a truly transparent background

我有一张我在 Photoshop 中制作的透明图像,它的不透明度为 30%。

现在我想把它作为面板的背景图片。但是一个普通的面板即使将背景颜色设置为透明,也会显示 white/control 背景。面板没有变透明。

所以我找到了 TransparentPanel class。但是当我使用那个面板时,我看不到我在代码中输入的图像?

我知道我可以将面板的背景色设置为与父级相同。但这在这种情况下不起作用,因为面板位于视频控件的顶部,而图像在下方移动。

所以我需要一个可以显示图像的完全透明的控件。不知道这个TransparentPanel能不能用?

void addpanel()
{
    TransparentPanel tp = new TransparentPanel();
    //Panel tp = new Panel();
    tp.BackColor = Color.Transparent; //This doesn't work?
    tp.BackgroundImage = Properties.Resources.arrowup; //This image is a 30% transparent image (opacity 30%)
    tp.Size = new System.Drawing.Size(54, 54);
    tp.Location = new Point(20, 20);
    panel219.Controls.Add(tp);
    tp.BringToFront();
}
public class TransparentPanel : Panel
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
        }
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        //base.OnPaintBackground(e);
    }
}

编辑

我也试过这种方法。但是面板中的图像仍然具有几乎是白色的 CONTROL 颜色。有人应该看穿我的图像,因为它是 30% 的不透明度?

void addpanel()
{
    TransparentPanel tp = new TransparentPanel(Properties.Resources.arrowup);
    tp.BackColor = Color.Transparent; //This doesn't work?
    tp.Size = new System.Drawing.Size(54, 54);
    tp.Location = new Point(20, 20);
    panel219.Controls.Add(tp);
    tp.BringToFront();
}

class TransparentPanel : Panel
{
    public Image image { get; set; }

    public TransparentPanel(Image img)
    {
        image = img;
        SetStyle(ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.ResizeRedraw |
            ControlStyles.SupportsTransparentBackColor |
            ControlStyles.UserPaint, true);
        UpdateStyles();
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        //base.OnPaintBackground(pevent);
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);

        var g = pevent.Graphics;

        if (Parent != null)
        {
            Rectangle rect = new Rectangle(Left, Top, Width, Height);

            g.TranslateTransform(-rect.X, -rect.Y);

            try
            {
                using (PaintEventArgs pea =
                            new PaintEventArgs(g, rect))
                {
                    pea.Graphics.SetClip(rect);
                    InvokePaintBackground(Parent, pea);
                    InvokePaint(Parent, pea);
                }
            }
            finally
            {
                g.TranslateTransform(rect.X, rect.Y);
            }
        }

        if (image != null)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            var rectSrc = new Rectangle(0, 0, image.Width, image.Height);
            var rectDes = new Rectangle(0, 0, Width, Height);

            //if (State == MouseState.Over)
                rectDes.Inflate(2, 2);

            g.DrawImage(image, rectDes, rectSrc, GraphicsUnit.Pixel);
        }
    }
}

应该是这样的:

public class TransparentPanel : Panel
{

    public TransparentPanel()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.ResizeRedraw |
            ControlStyles.SupportsTransparentBackColor |
            ControlStyles.UserPaint, true);
        UpdateStyles();
    }

    public TransparentPanel(Image img) : this()
    {
        image = img;
    }

    public Image image { get; set; }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        //base.OnPaintBackground(pevent);
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);

        var g = pevent.Graphics;

        if (Parent != null)
        {
            Rectangle rect = new Rectangle(Left, Top, Width, Height);

            g.TranslateTransform(-rect.X, -rect.Y);

            try
            {
                using (PaintEventArgs pea =
                            new PaintEventArgs(g, rect))
                {
                    pea.Graphics.SetClip(rect);
                    InvokePaintBackground(Parent, pea);
                    InvokePaint(Parent, pea);
                }
            }
            finally
            {
                g.TranslateTransform(rect.X, rect.Y);
            }
        }

        if (image != null)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            var rectSrc = new Rectangle(0, 0, image.Width, image.Height);
            var rectDes = new Rectangle(0, 0, Width, Height);

            //if (State == MouseState.Over)
            rectDes.Inflate(2, 2);

            g.DrawImage(image, rectDes, rectSrc, GraphicsUnit.Pixel);
        }
    }
}

您可以使用:

TableLayoutPanel tlp = new TableLayoutPanel(); 
tlp.BackColor = Color.FromArgb(150,0,0,0);

这应该可以解决问题。这有效并且看起来透明。