C# 使用十六进制代码更改表单的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2243370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:41:26  来源:igfitidea点击:

Changing background color of the form with hexadecimal code

c#.netwinforms

提问by Harikrishna

I have one method named ChangeFormBackground(Color colorName)which changes the form background with the colornamewhich is the parameter of the method.Now when I call this method I have not color name but the hexadecimal code of the color and I want to change the background color of the form with that hexadecimal code using that method then what should I do?

我有一个名为ChangeFormBackground(Color colorName) 的方法,它使用作为方法参数的颜色名称更改表单背景。现在,当我调用此方法时,我没有颜色名称而是颜色的十六进制代码,我想更改使用该方法使用该十六进制代码的表单的背景颜色,然后我该怎么办?

采纳答案by Webleeuw

This will always work because it doesn't contain alpha color (which is not supported by BackColor property):

这将始终有效,因为它不包含 alpha 颜色(BackColor 属性不支持):

Color temp = Color.FromArgb(0xFF00FF);
Color result = Color.FromArgb(temp.R, temp.G, temp.B);

回答by ZombieSheep

using System.Windows.Media;
Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

(this assumes an ARGB value)

(这里假设一个 ARGB 值)

or

或者

Color color = System.Drawing.ColorTranslator.FromHtml("#FFCC66");

回答by Giorgi

You can use ColorConverter Classfor manipulating color representations.

您可以使用ColorConverter 类来处理颜色表示。

回答by Darin Dimitrov

You could use the FromArgbmethod:

您可以使用FromArgb方法:

Color.FromArgb(0x78FF0000);