C#窗口窗体透明背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14309770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:24:22  来源:igfitidea点击:

C# transparent background for window form

c#winformstransparent

提问by user1207217

I've already seen Transparent background on winforms?

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

it doesnt offer solution to my problem. I am using the same method to try to achieve transparency

它没有为我的问题提供解决方案。我正在使用相同的方法来尝试实现透明度

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

But this gives a grey background, not transparent. How can I get an actually transparent background (note, transparency key solutions do not give a transparent background, and when I paint with alpha channel less than 255, it blends with the set form background colour, and not the actual background)? I want to paint to certain regions of the screen with alpha < 255 and blend with the background (not the form).

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

回答by Tommaso Belluzzo

The way I did it long time ago was to find an unused color for the form background and then set the transparency key to it:

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

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

Other ways are:

其他方式有:

  • Creating a background image, painting the transparent area of it with a specific color and setting it as the form BackgroundImage... then setting the TransparencyKey to that color.
  • Overriding OnPaintBackground method with an empty method.
  • 创建背景图像,用特定颜色绘制它的透明区域并将其设置为表格 BackgroundImage... 然后将 TransparencyKey 设置为该颜色。
  • 使用空方法覆盖 OnPaintBackground 方法。

[EDIT] As Mario states, normally the default transparent color for the key is Magenta.

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

回答by a d

You can use a picture for this work. The color of bound of picture is Red , Next use this code

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

this.TransparencyKey = Color.Red;

回答by ahmad mirza

This is the best way to make the transparent background of winform.

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

right after this:

在此之后:

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

hope this will help.

希望这会有所帮助。

回答by Pameela

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