导航

遍历System.drawing.Color下面的所有颜色以及名称以查看

Posted on 2013-02-22 08:52  ccjvl  阅读(392)  评论(0编辑  收藏  举报

面试的时候被问到,如何遍历System.drawing.Color下面的所有颜色以及名称以查看,当时答得不好,现将方案记录如下:

转载请注明出处:http://www.cnblogs.com/CCJVL/archive/2013/02/22/2921679.html

View Code
 1     public partial class Form1 : Form
 2     {
 3         FlowLayoutPanel newPanel = new FlowLayoutPanel();
 4 
 5         public Form1()
 6         {
 7             InitializeComponent();
 8             newPanel.AutoScroll = true;
 9             //newPanel.FlowDirection = FlowDirection.BottomUp;
10             //newPanel.WrapContents = false;
11             newPanel.Dock = DockStyle.Fill;
12             newPanel.BackColor = Color.White;
13             button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
14           
15         }
16 
17         private void button1_Click(object sender, EventArgs e)
18         {
19             newPanel.Controls.Clear();
20             int i = 1;
21 
22             foreach (var item in typeof(Color).GetMembers())
23             {
24                 if (item.MemberType == System.Reflection.MemberTypes.Property && System.Drawing.Color.FromName(item.Name).IsKnownColor == true)//只取属性且为属性中的已知Color,剔除byte属性以及一些布尔属性等(A B G R IsKnownColor Name等)
25                 {
26                     Label myLable = new Label();
27                     myLable.AutoSize = true;
28 
29                     myLable.BackColor = System.Drawing.Color.FromName(item.Name);
30                     myLable.Text = System.Drawing.Color.FromName(item.Name).Name;
31                     newPanel.Controls.Add(myLable);
32                     //newPanel.GetFlowBreak(myLable);
33 
34                     i++;
35                 }
36             }
37 
38 
39             this.Controls.Add(newPanel);
40             button1.Text = i.ToString();
41         }
42     }

 

参考了下面这篇博文,在此感谢。

Coding4Fun:遍历System.Drawing.Color结构

http://www.cnblogs.com/JimmyZhang/archive/2008/03/18/1110711.html