在设计窗体过程当中,常常须要设置各类颜色。在Color枚举中,已经预约义了不少种颜色类型,可以知足咱们平常的大多数需求。然而,对于新手来讲,对于各类颜色的效果并无概念,在设置过程当中不断的设置、运行查看效果是很是低效的。所以,这里使用WinForm实现了一个展现器,可以把预约义的各类颜色的前景色、背景色展现出来。编辑器
代码很是的简单,首先获取Color枚举的全部取值,然而遍历并添加到ListView,并设置其SubItem的颜色。ide
代码以下:this
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ColorShower { public partial class Form1 : Form { public Form1() { InitializeComponent(); initListView(); } void initListView() { Array colorArray = Enum.GetValues(typeof(KnownColor)); int i = 0; foreach(Object obj in colorArray) { i++; String colorName = obj.ToString(); Color color = Color.FromName(colorName); ListViewItem item = listView1.Items.Add(i.ToString()); item.UseItemStyleForSubItems = false; //添加颜色名称 ListViewItem.ListViewSubItem sub = item.SubItems.Add(colorName); //添加前景色 sub = item.SubItems.Add(colorName); sub.ForeColor = color; //添加背景色 sub = item.SubItems.Add(colorName); sub.BackColor = color; } } } }
WinForm自动生成代码以下:spa
namespace ColorShower { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理全部正在使用的资源。 /// </summary> /// <param name="disposing">若是应释放托管资源,为 true;不然为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.listView1 = new System.Windows.Forms.ListView(); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.SuspendLayout(); // // listView1 // this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1, this.columnHeader2, this.columnHeader3, this.columnHeader4}); this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; this.listView1.GridLines = true; this.listView1.Location = new System.Drawing.Point(0, 0); this.listView1.MultiSelect = false; this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(619, 479); this.listView1.TabIndex = 1; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.View = System.Windows.Forms.View.Details; // // columnHeader1 // this.columnHeader1.Text = "index"; // // columnHeader2 // this.columnHeader2.Text = "ColorName"; this.columnHeader2.Width = 160; // // columnHeader3 // this.columnHeader3.Text = "ForeColor"; this.columnHeader3.Width = 184; // // columnHeader4 // this.columnHeader4.Text = "BackColor"; this.columnHeader4.Width = 210; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(619, 479); this.Controls.Add(this.listView1); this.Name = "Form1"; this.Text = "颜色展现器"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.ListView listView1; private System.Windows.Forms.ColumnHeader columnHeader1; private System.Windows.Forms.ColumnHeader columnHeader2; private System.Windows.Forms.ColumnHeader columnHeader3; private System.Windows.Forms.ColumnHeader columnHeader4; } }
实现效果以下:设计