窗口forms的C#透明背景
我已经在winforms上看到了透明背景?
它不能解决我的问题。 我使用相同的方法来尝试实现透明度
public Form1() { this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); InitializeComponent(); this.BackColor = Color.FromArgb(0, 0, 0, 0); }
但这会产生灰色背景,而不是透明的。 如何获得实际透明的背景(注意,透明度键解决方案不提供透明背景,当我使用小于255的alpha通道绘制时,它与设置的表单背景颜色混合,而不是实际背景)? 我想绘制到alpha <255的屏幕的某些区域并与背景(不是表单)混合。
我很久以前做的方法是为表单背景找到一个未使用的颜色,然后设置透明度键:
this.BackColor = Color.Magenta; this.TransparencyKey = Color.Magenta;
其他方式是:
- 创建背景图像,使用特定颜色绘制透明区域并将其设置为BackgroundImageforms…然后将TransparencyKey设置为该颜色。
- 使用空方法覆盖OnPaintBackground方法。
[编辑]正如马里奥所说,通常键的默认透明颜色是洋红色。
这是制作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); } } }
这段代码对我有用。 我添加了这个.TransparencyKey =(BackColor); 在构造函数中。
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.TransparencyKey = (BackColor); } }
这是完整的代码和演示:
http://mishelshaji.co.in/2017/creating-a-transparent-window-in-windows-form-application/