窗口形式的C#透明背景

时间:2013-01-14 00:07:56

标签: c# winforms transparent

我已经看过Transparent background on winforms?

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

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

但这会产生灰色背景,而不是透明的。如何获得实际透明背景(注意,透明度键解决方案不提供透明背景,当我使用小于255的alpha通道绘制时,它与设置的表单背景颜色混合,而不是实际背景)?我想用alpha<屏幕绘制到屏幕的某些区域。 255并与背景混合(不是表格)。

4 个答案:

答案 0 :(得分:18)

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

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

其他方式是:

  • 创建背景图像,使用特定颜色绘制透明区域并将其设置为BackgroundImage形式...然后将TransparencyKey设置为该颜色。
  • 使用空方法覆盖OnPaintBackground方法。

[编辑]正如马里奥所说,通常键的默认透明色是洋红色。

答案 1 :(得分:2)

这是制作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);
    }
}
希望这会有所帮助。

答案 2 :(得分:0)

您可以使用图片进行此项工作。图片边界的颜色为红色,接下来使用此代码

this.TransparencyKey = Color.Red;

答案 3 :(得分:-1)

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);
        }
    }
}