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%
        }
     

你可能感兴趣的:(C#编程杂谈)