如何在 C# 中更改窗体背景颜色

How to Change Form background color in C#

我在 form_load 事件中使用以下代码来更改 Form.BackgroundColor,但它给我一个错误。

Control does not support transparent background colors.

这就是我正在尝试的...

private void Form1_Load(object sender, EventArgs e)
{
  string sColor = "#ACE1AF";// Hex value of any color
  Int32 iColorInt = Convert.ToInt32(sColor.Substring(1), 16);
  Color curveColor = System.Drawing.Color.FromArgb(iColorInt);
  this.BackColor = curveColor;
 }

我发现了同样的问题 (Why am I getting "Control does not support transparent background colors"?),但不符合我的要求,因为 Color class 使用的是默认值。

问题是,您试图使背景透明。您在 ARGB 中指定的颜色是 100% 透明的。因此错误。

你应该使用:

void Form1_Load(object sender, EventArgs e)
{
    string sColor = "#FFACE1AF";// Hex value of any color
    Int32 iColorInt = Convert.ToInt32(sColor.Substring(1), 16);
    Color curveColor = System.Drawing.Color.FromArgb(iColorInt);
    this.BackColor = curveColor;
}

正在将 Alpha 通道设置为 FF

ColorTranslator.FromHtml("#00FEF2D4");

编辑: ColorTranslator.FromHtml 将 HTML 颜色表示转换为 GDI+ Color 结构。

Parameters

htmlColor

Type: System.String

The string representation of the Html color to translate.

Return Value

Type: System.Drawing.Color

The Color structure that represents the translated HTML color or Empty if htmlColor is null.