当前位置:编程学习 > C#/ASP.NET >>

[C#]生成预定义全颜色表

生成Color类所有static预定义成员的颜色表
 
 
const long CELLS_PER_LINE = 10;  
  
const float MARGIN = 12;  
const float CELL_WIDTH = 160;  
const float CELL_HEIGHT = 64;  
const float COLOR_LEFT_MARGIN = 8;  
const float COLOR_TOP_MARGIN = 8;  
const float COLOR_CELL_WIDTH = 48;  
const float COLOR_CELL_HEIGHT = 32;  
const float TEXT_TOP_MARGIN = COLOR_TOP_MARGIN + COLOR_CELL_HEIGHT + 2;  
  
List<Color> vColors = new List<Color>();  
Type t = typeof(Color);  
PropertyInfo[] vProps = t.GetProperties();  
foreach (PropertyInfo propInfo in vProps)  
{  
    if (MemberTypes.Property == propInfo.MemberType &&  
        typeof(Color) == propInfo.PropertyType)  
    {  
        Color tmpColor = (Color)propInfo.GetValue(null, null);  
        vColors.Add(tmpColor);  
    }  
}  
  
Bitmap bmpColor = new Bitmap((int)(CELLS_PER_LINE * CELL_WIDTH + MARGIN * 2), (int)((vColors.Count / CELLS_PER_LINE + 1) * CELL_HEIGHT + MARGIN * 2));  
using (Graphics grp = Graphics.FromImage(bmpColor))  
{  
    grp.Clear(Color.Black);  
  
    for (int i = 0; i < vColors.Count; i++)  
    {  
        float nLeftBase = MARGIN + i % CELLS_PER_LINE * CELL_WIDTH;  
        float nTopBase = MARGIN + i / CELLS_PER_LINE * CELL_HEIGHT;  
  
        grp.DrawRectangle(new Pen(Color.White), nLeftBase, nTopBase, CELL_WIDTH, CELL_HEIGHT);  
  
        grp.FillRectangle(new SolidBrush(vColors[i]),  
                          nLeftBase + COLOR_LEFT_MARGIN, nTopBase + COLOR_TOP_MARGIN,  
                          COLOR_CELL_WIDTH, COLOR_CELL_HEIGHT);  
  
        grp.DrawString(vColors[i].Name,  
                       new Font("宋体", 9, FontStyle.Regular),  
                       new SolidBrush(Color.White),  
                       nLeftBase + COLOR_LEFT_MARGIN, nTopBase + TEXT_TOP_MARGIN);  
    }  
}  
  
bmpColor.Save(@"D:\AllColor.bmp");  

 

 
 
生成效果如图:
 
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,