简体   繁体   English

窗口形式的C#透明背景

[英]C# transparent background for window form

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)? 如何获得实际透明背景(注意,透明度键解决方案不提供透明背景,当我使用小于255的alpha通道绘制时,它与设置的表单背景颜色混合,而不是实际背景)? I want to paint to certain regions of the screen with alpha < 255 and blend with the background (not the form). 我想绘制到alpha <255的屏幕的某些区域并与背景(不是表单)混合。

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. 创建背景图像,使用特定颜色绘制透明区域并将其设置为BackgroundImage形式...然后将TransparencyKey设置为该颜色。
  • Overriding OnPaintBackground method with an empty method. 使用空方法覆盖OnPaintBackground方法。

[EDIT] As Mario states, normally the default transparent color for the key is Magenta. [编辑]正如马里奥所说,通常键的默认透明颜色是洋红色。

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. 希望这会有所帮助。

You can use a picture for this work. 您可以使用图片进行此项工作。 The color of bound of picture is Red , Next use this code 图片边界的颜色为红色,接下来使用此代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM