一個已命名顏色的展現器的實現

在設計窗體過程當中,常常須要設置各類顏色。在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;
    }
}

 

 

實現效果以下:設計

相關文章
相關標籤/搜索