6

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

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

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

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

4

4 回答 4

19

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

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

其他方式有:

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

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

于 2013-01-14T00:09:37.883 回答
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);
    }
}

希望这会有所帮助。

于 2015-08-04T05:58:05.633 回答
0

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

this.TransparencyKey = Color.Red;
于 2013-01-14T01:22:55.793 回答
-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);
        }
    }
}
于 2016-10-18T06:06:13.920 回答