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

Judy ·
更新时间:2024-11-01
· 529 次阅读

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

代码如下:
View Code
     public partial class Form1 : Form
     {
         FlowLayoutPanel newPanel = new FlowLayoutPanel();
         public Form1()
         {
             InitializeComponent();
             newPanel.AutoScroll = true;
             //newPanel.FlowDirection = FlowDirection.BottomUp;
             //newPanel.WrapContents = false;
             newPanel.Dock = DockStyle.Fill;
             newPanel.BackColor = Color.White;
             button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
         }
         private void button1_Click(object sender, EventArgs e)
         {
             newPanel.Controls.Clear();
             int i = 1;
             foreach (var item in typeof(Color).GetMembers())
             {
                 if (item.MemberType == System.Reflection.MemberTypes.Property && System.Drawing.Color.FromName(item.Name).IsKnownColor == true)//只取属性且为属性中的已知Color,剔除byte属性以及一些布尔属性等(A B G R IsKnownColor Name等)
                 {
                     Label myLable = new Label();
                     myLable.AutoSize = true;
                     myLable.BackColor = System.Drawing.Color.FromName(item.Name);
                     myLable.Text = System.Drawing.Color.FromName(item.Name).Name;
                     newPanel.Controls.Add(myLable);
                     //newPanel.GetFlowBreak(myLable);
                     i++;
                 }
             }
 
             this.Controls.Add(newPanel);
             button1.Text = i.ToString();
         }
     }

您可能感兴趣的文章:c#使用filesystemwatcher实时监控文件目录的添加和删除C#利用System.Uri转URL为绝对地址的方法C#难点逐个击破(8):可空类型System.NullableC#关于System.Collections空间详解c#实现数据同步的方法(使用文件监控对象filesystemwatcher)C#利用System.Threading.Thread.Sleep即时输出信息的详解C#使用System.Environment获取电脑的相关属性



drawing C# system

需要 登录 后方可回复, 如果你还没有账号请 注册新账号