zoukankan      html  css  js  c++  java
  • 一个已命名颜色的展示器的实现

    在设计窗体过程中,经常需要设置各种颜色。在Color枚举中,已经预定义了很多种颜色类型,能够满足我们日常的大多数需求。然而,对于新手来说,对于各种颜色的效果并没有概念,在设置过程中不断的设置、运行查看效果是非常低效的。因此,这里使用WinForm实现了一个展示器,能够把预定义的各种颜色的前景色、背景色展示出来。

    代码非常的简单,首先获取Color枚举的所有取值,然而遍历并添加到ListView,并设置其SubItem的颜色。

    代码如下:

    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自动生成代码如下:

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

    实现效果如下:

  • 相关阅读:
    python idea 利用树莓派做家庭报警系统
    Browserslist: caniuse-lite is outdated. Please run next command `npm update`
    maven项目最外层有个红x,项目其他没有x
    要取消:根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引
    MySQL的DATE_FORMAT()用法
    maven项目的java和resources等文件夹不在Java Resources的文件夹里,并且缺少Deployment...
    小程序开发demo及资源
    wx.requestSubscribeMessage 订阅消息
    java获取年月日
    goland 文件头自动注释
  • 原文地址:https://www.cnblogs.com/pzy4447/p/8476062.html
Copyright © 2011-2022 走看看