C#編寫簡單的調色盤及設定窗體的透明度
用C#編寫調色盤用到的主要控制元件是trackBar。列舉型別Color的變數就是通過列舉類
型Color的FromArgb()方法獲得三顏色的值的,而這些值是從trackBar.value中獲得的。
事先要設定trackBar的value的範圍(0-255),最好設定一下其沒值間距。然後分別對三
個trackBar(分別代表紅,綠,藍)設定滑動事件。大體程式碼如下:
private void trackBar1_Scroll(object sender, EventArgs e)
{
Color yanse = Color.FromArgb
(this.trackBar1.Value,this.trackBar2.Value,this.trackBar3.Value);
this.BackColor = yanse;
this.textBox1.Text = this.trackBar1.Value.ToString();
this.textBox2.Text = this.trackBar2.Value.ToString();
this.textBox3.Text = this.trackBar3.Value.ToString();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
Color yanse = Color.FromArgb(this.trackBar1.Value,
this.trackBar2.Value, this.trackBar3.Value);
this.BackColor = yanse;
this.textBox1.Text = this.trackBar1.Value.ToString();
this.textBox2.Text = this.trackBar2.Value.ToString();
this.textBox3.Text = this.trackBar3.Value.ToString();
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
Color yanse = Color.FromArgb(this.trackBar1.Value,
this.trackBar2.Value, this.trackBar3.Value);
this.BackColor = yanse;
this.textBox1.Text = this.trackBar1.Value.ToString();
this.textBox2.Text = this.trackBar2.Value.ToString();
this.textBox3.Text = this.trackBar3.Value.ToString();
}
//滑動trackBar使視窗透明的程式碼,值得一提的是最後的除數最好設為你所設的
trackbar的最大值的大小(這裡設的最大值為100,間距(TickFrequency)為1),否則效果
不好。
private void trackBar4_Scroll(object sender, EventArgs e)
{
this.Opacity =this.trackBar4.Value/100.0; 100/100 = 100%
99/100 = 99%
10/100 = 10%
}