颜色选择器的设计与实现

    通过颜色选择器可以简单的获得喜欢颜色的RGB值。

    实现步骤:

1、         拖拽picturebox,HScrollBar控件和TextBox;

2、         为HScrollBar的滚动事件添加事件处理程序如下:

private void hScrollBar3_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)

         {

              this.textBox1.Text = this.hScrollBar1.Value.ToString();

              this.textBox2.Text = this.hScrollBar2.Value.ToString();

              this.textBox3.Text = this.hScrollBar3.Value.ToString();

              this.pictureBox1.BackColor = Color.FromArgb(this.hScrollBar1.Value,this.hScrollBar2.Value,this.hScrollBar3.Value);

      }

   3、其它代码见清单

//完整的实例代码如下:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace ColorSelect

{

   /// <summary>

   /// Form1 的摘要说明。

   /// </summary>

   public class Form1 : System.Windows.Forms.Form

   {

      private System.Windows.Forms.PictureBox pictureBox1;

      private System.Windows.Forms.HScrollBar hScrollBar1;

      private System.Windows.Forms.HScrollBar hScrollBar2;

      private System.Windows.Forms.HScrollBar hScrollBar3;

      private System.Windows.Forms.Label label1;

      private System.Windows.Forms.Label label2;

      private System.Windows.Forms.Label label3;

      private System.Windows.Forms.TextBox textBox1;

      private System.Windows.Forms.TextBox textBox2;

      private System.Windows.Forms.TextBox textBox3;

      /// <summary>

      /// 必需的设计器变量。

      /// </summary>

      private System.ComponentModel.Container components = null;

      public Form1()

      {

        //

        // Windows 窗体设计器支持所必需的

        //

        InitializeComponent();

        //

        // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

        //

      }

      /// <summary>

      /// 清理所有正在使用的资源。

      /// </summary>

      protected override void Dispose( bool disposing )

      {

        if( disposing )

        {

           if (components != null)

           {

              components.Dispose();

           }

        }

        base.Dispose( disposing );

      }

      #region Windows 窗体设计器生成的代码

      /// <summary>

      /// 设计器支持所需的方法 - 不要使用代码编辑器修改

      /// 此方法的内容。

      /// </summary>

      private void InitializeComponent()

      {

        this.pictureBox1 = new System.Windows.Forms.PictureBox();

        this.hScrollBar1 = new System.Windows.Forms.HScrollBar();

        this.hScrollBar2 = new System.Windows.Forms.HScrollBar();

        this.hScrollBar3 = new System.Windows.Forms.HScrollBar();

        this.label1 = new System.Windows.Forms.Label();

        this.label2 = new System.Windows.Forms.Label();

        this.label3 = new System.Windows.Forms.Label();

        this.textBox1 = new System.Windows.Forms.TextBox();

        this.textBox2 = new System.Windows.Forms.TextBox();

        this.textBox3 = new System.Windows.Forms.TextBox();

        this.SuspendLayout();

        //

        // pictureBox1

        //

        this.pictureBox1.Location = new System.Drawing.Point(32, 48);

        this.pictureBox1.Name = "pictureBox1";

        this.pictureBox1.Size = new System.Drawing.Size(224, 208);

        this.pictureBox1.TabIndex = 0;

        this.pictureBox1.TabStop = false;

        //

        // hScrollBar1

        //

        this.hScrollBar1.Location = new System.Drawing.Point(304, 80);

        this.hScrollBar1.Maximum = 264;

        this.hScrollBar1.Name = "hScrollBar1";

        this.hScrollBar1.Size = new System.Drawing.Size(408, 17);

        this.hScrollBar1.TabIndex = 1;

        this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);

        //

        // hScrollBar2

        //

        this.hScrollBar2.Location = new System.Drawing.Point(304, 112);

        this.hScrollBar2.Maximum = 264;

        this.hScrollBar2.Name = "hScrollBar2";

        this.hScrollBar2.Size = new System.Drawing.Size(408, 17);

        this.hScrollBar2.TabIndex = 2;

        this.hScrollBar2.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar2_Scroll);

        //

        // hScrollBar3

        //

        this.hScrollBar3.Location = new System.Drawing.Point(304, 144);

        this.hScrollBar3.Maximum = 264;

        this.hScrollBar3.Name = "hScrollBar3";

        this.hScrollBar3.Size = new System.Drawing.Size(408, 17);

        this.hScrollBar3.TabIndex = 3;

        this.hScrollBar3.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar3_Scroll);

        //

        // label1

        //

        this.label1.Location = new System.Drawing.Point(280, 80);

        this.label1.Name = "label1";

        this.label1.Size = new System.Drawing.Size(24, 24);

        this.label1.TabIndex = 4;

        this.label1.Text = "红";

        //

        // label2

        //

        this.label2.Location = new System.Drawing.Point(280, 144);

        this.label2.Name = "label2";

        this.label2.Size = new System.Drawing.Size(24, 16);

        this.label2.TabIndex = 5;

        this.label2.Text = "蓝";

        //

        // label3

        //

        this.label3.Location = new System.Drawing.Point(280, 112);

        this.label3.Name = "label3";

        this.label3.Size = new System.Drawing.Size(24, 24);

        this.label3.TabIndex = 6;

        this.label3.Text = "绿";

        //

        // textBox1

        //

        this.textBox1.Location = new System.Drawing.Point(720, 80);

        this.textBox1.Name = "textBox1";

        this.textBox1.Size = new System.Drawing.Size(64, 21);

        this.textBox1.TabIndex = 7;

        this.textBox1.Text = "textBox1";

        //

        // textBox2

        //

        this.textBox2.Location = new System.Drawing.Point(720, 112);

        this.textBox2.Name = "textBox2";

        this.textBox2.Size = new System.Drawing.Size(64, 21);

        this.textBox2.TabIndex = 8;

        this.textBox2.Text = "textBox2";

        //

        // textBox3

        //

        this.textBox3.Location = new System.Drawing.Point(720, 144);

        this.textBox3.Name = "textBox3";

        this.textBox3.Size = new System.Drawing.Size(64, 21);

        this.textBox3.TabIndex = 9;

        this.textBox3.Text = "textBox3";

        //

        // Form1

        //

        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

        this.ClientSize = new System.Drawing.Size(840, 334);

        this.Controls.Add(this.textBox3);

        this.Controls.Add(this.textBox2);

        this.Controls.Add(this.textBox1);

        this.Controls.Add(this.label3);

        this.Controls.Add(this.label2);

        this.Controls.Add(this.label1);

        this.Controls.Add(this.hScrollBar3);

        this.Controls.Add(this.hScrollBar2);

        this.Controls.Add(this.hScrollBar1);

        this.Controls.Add(this.pictureBox1);

        this.Name = "Form1";

        this.Text = "颜色选择器";

        this.Load += new System.EventHandler(this.Form1_Load);

        this.ResumeLayout(false);

      }

      #endregion

      /// <summary>

      /// 应用程序的主入口点。

      /// </summary>

      [STAThread]

      static void Main()

      {

        Application.Run(new Form1());

      }

      private void hScrollBar3_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)

      {

        this.textBox1.Text = this.hScrollBar1.Value.ToString();

        this.textBox2.Text = this.hScrollBar2.Value.ToString();

        this.textBox3.Text = this.hScrollBar3.Value.ToString();

        this.pictureBox1.BackColor = Color.FromArgb(this.hScrollBar1.Value,this.hScrollBar2.Value,this.hScrollBar3.Value);

      }

      private void Form1_Load(object sender, System.EventArgs e)

      {

        hScrollBar3_Scroll(null,null);

     

      }

      private void hScrollBar2_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)

      {

        hScrollBar3_Scroll(null,null);

      }

      private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)

      {

        hScrollBar3_Scroll(null,null);

      }

   }

}

posted on 2007-10-09 12:03  段静迪  阅读(407)  评论(2编辑  收藏  举报