c#遍历System.drawing.Color下面的所有颜色以及名称以查看
时间:2022-07-08 10:26:46|栏目:.NET代码|点击: 次
面试的时候被问到,如何遍历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();
}
}
上一篇:Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
栏 目:.NET代码
本文标题:c#遍历System.drawing.Color下面的所有颜色以及名称以查看
本文地址:http://www.codeinn.net/misctech/207156.html