c#透明背景窗口形式

本文关键字:窗口 背景 透明 | 更新日期: 2023-09-27 17:53:00

我已经看到透明背景在winforms?

它不能解决我的问题。我使用相同的方法来尝试实现透明度

    public Form1()
    {
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        InitializeComponent();
        this.BackColor = Color.FromArgb(0, 0, 0, 0);
    }

但是这给了一个灰色的背景,而不是透明的。我怎样才能得到一个真正透明的背景(注意,透明度关键解决方案不给一个透明的背景,当我用alpha通道小于255,它与设置形式的背景颜色混合,而不是实际的背景)?我想用alpha &lt绘制屏幕的某些区域;

c#透明背景窗口形式

很久以前我做的方法是找到一个未使用的颜色作为表单背景,然后设置透明度键:

this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;

  • 创建一个背景图像,用特定的颜色绘制它的透明区域,并将其设置为backgrounimage…然后将透明度键设置为该颜色。
  • 用空方法重写OnPaintBackground方法

[EDIT]正如Mario所说,通常情况下,键的默认透明颜色是Magenta。

这是制作winform透明背景的最佳方法。

就在这后面:

public partial class frmTransparentBackcolor : Form
{
    public frmTransparentBackcolor()
    {
        InitializeComponent();
        //set the backcolor and transparencykey on same color.
        this.BackColor = Color.LimeGreen;
        this.TransparencyKey = Color.LimeGreen;
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.LimeGreen, e.ClipRectangle);
    }
}

您可以使用图片来完成这项工作。图片边界的颜色是红色,接下来使用这个代码

this.TransparencyKey = Color.Red;
public partial class TransprtScrcn : Form
    {
        public TransprtScrcn()
        {
            InitializeComponent();
            this.BackColor = Color.Red;
            this.TransparencyKey = Color.Red;
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Red, e.ClipRectangle);
        }
    }
}