简体   繁体   中英

How to Change Form background color in C#

I am using following code in form_load event to change Form.BackgroundColor , but it is giving me an error.

Control does not support transparent background colors.

Here is what I am trying...

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

I found the same question ( Why am I getting "Control does not support transparent background colors"? ), but is not met my requirement due to in this Color class is using its default values.

The problem is, you are trying to make the background transparent. The color you specify in ARGB is 100% transparent. Hence the error.

You should use:

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

Setting the alpha channel to FF .

ColorTranslator.FromHtml("#00FEF2D4");

Edit : ColorTranslator.FromHtml Translates an HTML color representation to a GDI+ Color structure.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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