C#:按钮颜色设置
private void button1_Click(object sender, EventArgs e)
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
Button button = (Button)sender;
int r, g, b;
r = color.Color.R;
g = color.Color.G;
b = color.Color.B;
// RGB 色值
string rgbString = r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
//颜色字符串 Alpha + RGB
string colorString = color.Color.A.ToString("X2") + rgbString;
//按钮显示文本
button.Text = "#" + rgbString;
//按钮背景色
button.BackColor = color.Color;
//按钮字体颜色(ARGB),为按钮背景色的反色
string foreColorString
= "FF" + (255 - r).ToString("X2")
+ (255 - g).ToString("X2")
+ (255 - b).ToString("X2");
//按钮字体颜色
button.ForeColor =
System.Drawing.Color.FromArgb(int.Parse(
foreColorString, System.Globalization.NumberStyles.HexNumber));
}
}
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
Button button = (Button)sender;
int r, g, b;
r = color.Color.R;
g = color.Color.G;
b = color.Color.B;
// RGB 色值
string rgbString = r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
//颜色字符串 Alpha + RGB
string colorString = color.Color.A.ToString("X2") + rgbString;
//按钮显示文本
button.Text = "#" + rgbString;
//按钮背景色
button.BackColor = color.Color;
//按钮字体颜色(ARGB),为按钮背景色的反色
string foreColorString
= "FF" + (255 - r).ToString("X2")
+ (255 - g).ToString("X2")
+ (255 - b).ToString("X2");
//按钮字体颜色
button.ForeColor =
System.Drawing.Color.FromArgb(int.Parse(
foreColorString, System.Globalization.NumberStyles.HexNumber));
}
}