我写的代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//将三个HScrollBar的最大值都设置为255
this.hsRed.Maximum = 255;
this.hsGreen.Maximum = 255;
this.hsBlue.Maximum = 255;
}
//当HScrollBar的值改变时触发此事件
private void hsRGB_ValueChanged(object sender, EventArgs e)
{
int iR = this.hsRed.Value;
int iG = this.hsGreen.Value;
int iB = this.hsBlue.Value;
//用R、G、B创建Color结构实例
Color RGB = Color.FromArgb(iR, iG, iB);
this.txtNum.Text = "R=" + iR + ", G=" + iG + ", B=" + iB;
//设置TextBox的背景色
this.txtColor.BackColor = RGB;
}
}
运行结果如下图:
|