C#窗体程序trackBar控件制作调色板
程序设计:
程序代码:
1、Form1.Designer.cs
namespace color
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.tkbRed = new System.Windows.Forms.TrackBar();
this.tkbGreen = new System.Windows.Forms.TrackBar();
this.tkbBlue = new System.Windows.Forms.TrackBar();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.picShow = new System.Windows.Forms.PictureBox();
this.lblShow = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.lblRed = new System.Windows.Forms.Label();
this.lblGreen = new System.Windows.Forms.Label();
this.lblBlue = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.tkbRed)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.tkbGreen)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.tkbBlue)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.picShow)).BeginInit();
this.SuspendLayout();
//
// tkbRed
//
this.tkbRed.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.tkbRed.Location = new System.Drawing.Point(256, 57);
this.tkbRed.Maximum = 255;
this.tkbRed.Name = "tkbRed";
this.tkbRed.Size = new System.Drawing.Size(262, 45);
this.tkbRed.TabIndex = 0;
this.tkbRed.TickFrequency = 10;
this.tkbRed.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
this.tkbRed.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// tkbGreen
//
this.tkbGreen.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.tkbGreen.Location = new System.Drawing.Point(256, 127);
this.tkbGreen.Maximum = 255;
this.tkbGreen.Name = "tkbGreen";
this.tkbGreen.Size = new System.Drawing.Size(262, 45);
this.tkbGreen.TabIndex = 1;
this.tkbGreen.TickFrequency = 10;
this.tkbGreen.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
this.tkbGreen.Scroll += new System.EventHandler(this.tkbGreen_Scroll);
//
// tkbBlue
//
this.tkbBlue.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.tkbBlue.Location = new System.Drawing.Point(256, 197);
this.tkbBlue.Maximum = 255;
this.tkbBlue.Name = "tkbBlue";
this.tkbBlue.Size = new System.Drawing.Size(262, 45);
this.tkbBlue.TabIndex = 2;
this.tkbBlue.TickFrequency = 10;
this.tkbBlue.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
this.tkbBlue.Scroll += new System.EventHandler(this.tkbBlue_Scroll);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(39, 39);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.label2.Font = new System.Drawing.Font("楷体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ForeColor = System.Drawing.SystemColors.Desktop;
this.label2.Location = new System.Drawing.Point(54, 39);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(110, 24);
this.label2.TabIndex = 4;
this.label2.Text = "混色结果";
//
// picShow
//
this.picShow.BackColor = System.Drawing.Color.White;
this.picShow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.picShow.Location = new System.Drawing.Point(41, 75);
this.picShow.Name = "picShow";
this.picShow.Size = new System.Drawing.Size(132, 132);
this.picShow.TabIndex = 19;
this.picShow.TabStop = false;
//
// lblShow
//
this.lblShow.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.lblShow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblShow.Font = new System.Drawing.Font("Arial", 18F);
this.lblShow.Location = new System.Drawing.Point(256, 265);
this.lblShow.Name = "lblShow";
this.lblShow.Size = new System.Drawing.Size(262, 54);
this.lblShow.TabIndex = 5;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(264, 42);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(11, 12);
this.label3.TabIndex = 6;
this.label3.Text = "0";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(487, 42);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(23, 12);
this.label4.TabIndex = 7;
this.label4.Text = "255";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(264, 111);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(11, 12);
this.label5.TabIndex = 8;
this.label5.Text = "0";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(487, 111);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(23, 12);
this.label6.TabIndex = 9;
this.label6.Text = "255";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(264, 181);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(11, 12);
this.label7.TabIndex = 10;
this.label7.Text = "0";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(488, 180);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(23, 12);
this.label8.TabIndex = 11;
this.label8.Text = "255";
//
// lblRed
//
this.lblRed.BackColor = System.Drawing.Color.Red;
this.lblRed.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblRed.ForeColor = System.Drawing.Color.White;
this.lblRed.Location = new System.Drawing.Point(356, 36);
this.lblRed.Name = "lblRed";
this.lblRed.Size = new System.Drawing.Size(68, 15);
this.lblRed.TabIndex = 4;
//
// lblGreen
//
this.lblGreen.BackColor = System.Drawing.Color.Lime;
this.lblGreen.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblGreen.ForeColor = System.Drawing.Color.White;
this.lblGreen.Location = new System.Drawing.Point(356, 107);
this.lblGreen.Name = "lblGreen";
this.lblGreen.Size = new System.Drawing.Size(68, 15);
this.lblGreen.TabIndex = 13;
//
// lblBlue
//
this.lblBlue.BackColor = System.Drawing.Color.Blue;
this.lblBlue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblBlue.ForeColor = System.Drawing.Color.White;
this.lblBlue.Location = new System.Drawing.Point(356, 178);
this.lblBlue.Name = "lblBlue";
this.lblBlue.Size = new System.Drawing.Size(68, 15);
this.lblBlue.TabIndex = 14;
//
// label11
//
this.label11.AutoSize = true;
this.label11.Location = new System.Drawing.Point(201, 60);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(0, 12);
this.label11.TabIndex = 15;
//
// label12
//
this.label12.AutoSize = true;
this.label12.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label12.ForeColor = System.Drawing.Color.Red;
this.label12.Location = new System.Drawing.Point(196, 56);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(60, 19);
this.label12.TabIndex = 16;
this.label12.Text = "红色:";
//
// label13
//
this.label13.AutoSize = true;
this.label13.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label13.ForeColor = System.Drawing.Color.Lime;
this.label13.Location = new System.Drawing.Point(196, 125);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(60, 19);
this.label13.TabIndex = 17;
this.label13.Text = "绿色:";
//
// label14
//
this.label14.AutoSize = true;
this.label14.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label14.ForeColor = System.Drawing.Color.Blue;
this.label14.Location = new System.Drawing.Point(196, 196);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(60, 19);
this.label14.TabIndex = 18;
this.label14.Text = "蓝色:";
//
// label9
//
this.label9.AutoSize = true;
this.label9.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.label9.Font = new System.Drawing.Font("楷体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label9.ForeColor = System.Drawing.SystemColors.Desktop;
this.label9.Location = new System.Drawing.Point(54, 283);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(135, 24);
this.label9.TabIndex = 20;
this.label9.Text = "当前颜色:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(560, 346);
this.Controls.Add(this.label9);
this.Controls.Add(this.label14);
this.Controls.Add(this.label13);
this.Controls.Add(this.label12);
this.Controls.Add(this.label11);
this.Controls.Add(this.lblBlue);
this.Controls.Add(this.lblGreen);
this.Controls.Add(this.lblRed);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.lblShow);
this.Controls.Add(this.picShow);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.tkbBlue);
this.Controls.Add(this.tkbGreen);
this.Controls.Add(this.tkbRed);
this.Name = "Form1";
this.Text = "41512221丁洁";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.tkbRed)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.tkbGreen)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.tkbBlue)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.picShow)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TrackBar tkbRed;
private System.Windows.Forms.TrackBar tkbGreen;
private System.Windows.Forms.TrackBar tkbBlue;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
internal System.Windows.Forms.PictureBox picShow;
internal System.Windows.Forms.Label lblShow;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
internal System.Windows.Forms.Label lblRed;
internal System.Windows.Forms.Label lblGreen;
internal System.Windows.Forms.Label lblBlue;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label9;
}
}
2、Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace color
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load(objectsender, EventArgs e)
{
tkbRed.Value = 255;
tkbGreen.Value = 255;
tkbBlue.Value = 255;
lblRed.Text =tkbRed.Value.ToString();
lblGreen.Text =tkbGreen.Value.ToString();
lblBlue.Text =tkbBlue.Value.ToString();
picShow.BackColor = Color.FromArgb(tkbRed.Value, tkbGreen.Value,tkbBlue.Value);
lblShow.Text="RGB("+tkbRed.Value.ToString()+","+tkbGreen.Value.ToString() + " , "+ tkbBlue.Value.ToString() + ")";
}
privatevoid trackBar1_Scroll(objectsender, EventArgs e)
{
picShow.BackColor = Color.FromArgb(tkbRed.Value,tkbGreen.Value,tkbBlue.Value);
lblRed.Text =tkbRed.Value.ToString();
lblShow.Text = "RGB(" + tkbRed.Value.ToString() + "," + tkbGreen.Value.ToString() + "," + tkbBlue.Value.ToString() + ")";
}
privatevoid tkbGreen_Scroll(objectsender, EventArgs e)
{
picShow.BackColor = Color.FromArgb(tkbRed.Value, tkbGreen.Value,tkbBlue.Value);
lblGreen.Text =tkbGreen.Value.ToString();
lblShow.Text = "RGB(" + tkbRed.Value.ToString() + "," + tkbGreen.Value.ToString() + "," + tkbBlue.Value.ToString() + ")";
}
privatevoid tkbBlue_Scroll(objectsender, EventArgs e)
{
picShow.BackColor = Color.FromArgb(tkbRed.Value, tkbGreen.Value,tkbBlue.Value);
lblBlue.Text =tkbBlue.Value.ToString();
lblShow.Text = "RGB(" + tkbRed.Value.ToString() + "," + tkbGreen.Value.ToString() + "," + tkbBlue.Value.ToString() + ")";
}
}
}
程序运行:
哪里看不懂可以留言,欢迎光临!