如何在C# Windows窗体应用程序中制作半透明表单
我尝试了TransparentKey,但它将其变为完全透明。我还尝试了Opacity,但它会影响整个带有控件的窗体。
我只想让表单部分是半透明的,而不是控件。
我尝试了TransparentKey,但它将其变为完全透明。我还尝试了Opacity,但它会影响整个带有控件的窗体。
我只想让表单部分是半透明的,而不是控件。
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
var hb = new HatchBrush(HatchStyle.Percent50, this.TransparencyKey);
e.Graphics.FillRectangle(hb,this.DisplayRectangle);
}
我发现Hatch Brush很丑陋,
改为:
protected override void OnPaintBackground(PaintEventArgs e) {
var hb = new HatchBrush(HatchStyle.Percent80, this.TransparencyKey);
e.Graphics.FillRectangle(hb, this.DisplayRectangle);
}
我使用了:
protected override void OnPaintBackground(PaintEventArgs e) {
var sb = new SolidBrush(Color.FromArgb(100, 100, 100, 100));
e.Graphics.FillRectangle(sb, this.DisplayRectangle);
}
有一个解决方案可以将半透明效果添加到一个控件(而不是窗体)中:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Apply opacity (0 to 255)
panel1.BackColor = Color.FromArgb(25, panel1.BackColor);
}
在 Visual Studio 中:(仅在执行期间激活的 alpha 版本)
在 Windows 7 上执行:
在一台旧的Windows 2003服务器上执行:
ArgumentException
异常,提示该控件不支持透明背景颜色。 - Adam L. S.