C# - 如何将Fore Color更改为自定义RGB值
问题描述:
我创建了一个标签,并且需要将它的颜色以编程方式更改为特定的RGB颜色代码(如果某个事件触发)。C# - 如何将Fore Color更改为自定义RGB值
我想它是这样的:
statusStripTestLabel.ForeColor = new Color(128, 64, 64);
奖金:
Set it to a RGBA color.
,但我得到Color does not have a constructor which accepts 3 arguments.
那么,如何解决这个问题?
答
statusStripTestLabel.ForeColor = Color.FromArgb(128, 64, 64); //Alpha is implicitly 255.
statusStripTestLabel.ForeColor = Color.FromArgb(255, 128, 64, 64); //Alpha is explicitly 255. You can change the value of the first parameter to specify the alpha you want.
答
你正在尝试的是什么在JAVA中有点正确。 在c#中,它是,
yourComponent。前景色=颜色。 FromArgb(theARGB_Code);
*注意并非所有组件都支持透明度
在那里错过了一个参数。 – marsze
实际上,只需为A(或Alpha指示不透明度)添加另一个值为255,以便读取为Color.FromArgb(255,128,64,64); – RoguePlanetoid
其实我没有。 FromArgb()方法有4个重载。如果你想指定alpha,你可以使用接受4个参数的重载。由于该问题没有指定alpha,所以他可以使用接受上述3个参数(int red,int green,int blue)的重载。 –