Brush 色谱

运行效果如下:

image

程序源代码如下:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Collections.Generic;
 
namespace ShowColor
{
    /// <summary>
    /// 显示标准颜色
    /// </summary>
    public class ShowColor : Form
    {
        public static void Main(string[] args)
        {
            Application.Run(new ShowColor());
        }
 
        /// <summary>
        /// 初始化窗体设置
        /// </summary>
        public ShowColor()
        {
            this.Text = "ShowColor";
            this.ClientSize = new Size(800, 630);
            this.Paint += new PaintEventHandler(this.PaintForm);
            this.SizeChanged += new EventHandler(this.RefreshForm);
        }
 
        /// <summary>
        /// 窗体尺寸改变,重绘窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshForm(object sender, EventArgs e)
        {
            this.Invalidate();
        }
 
        /// <summary>
        /// 重绘窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PaintForm(object sender, PaintEventArgs e)
        {
            // 获取 Brushes 所有公有属性
            PropertyInfo[] props = typeof(Brushes).GetProperties();
 
            // 创建 brushes 集合
            List<BrushWithName> brushes = new List<BrushWithName>();
            foreach (var prop in props)
            {
                BrushWithName entity = new BrushWithName();
                entity.Name = prop.Name;
                entity.Value = (Brush)prop.GetValue(null, null);
                brushes.Add(entity);
            }
 
            // 画出这个色彩图
            Size clientSize = this.Size;
            // 矩形条尺寸
            Size blockSize = new Size(clientSize.Width / 4, clientSize.Height / (brushes.Count / 4));
            // 绘画起始点
            Point location = new Point();
            // 列数
            int cols = 0;
 
            foreach (BrushWithName entity in brushes)
            {
                Graphics g = e.Graphics;
                g.FillRectangle(entity.Value, new Rectangle(location, blockSize));
                g.DrawString(entity.Name, new Font("宋体", 10), Brushes.Black, location);
                location.Offset(blockSize.Width, 0);// X 坐标横移
                cols++;
                if (cols == 4)
                {
                    location = new Point(0, location.Y + blockSize.Height);// X 归零,Y 横移
                    cols = 0;
                }
            }
        }
    }
 
    /// <summary>
    /// 色谱实体类
    /// </summary>
    class BrushWithName
    {
        public Brush Value;
        public String Name;
    }
}
原文地址:https://www.cnblogs.com/SkySoot/p/2342978.html