Skip to main content
 首页 » 编程设计

c#之列出所有系统颜色

2024年02月20日12insus

在 Windows.Forms 中,我需要创建一个接受任何颜色并尝试找到与之对应的系统颜色的程序。

我无法弄清楚如何遍历 System.Drawing.SystemColors 类的所有颜色 - 它是一个类,而不是枚举或列表。

我该怎么做(某种反射?)?

请您参考如下方法:

怎么样

public static Dictionary<string,object> GetStaticPropertyBag(Type t) 
{ 
    const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; 
 
    var map = new Dictionary<string, object>(); 
    foreach (var prop in t.GetProperties(flags)) 
    { 
        map[prop.Name] = prop.GetValue(null, null); 
    } 
    return map; 
} 

或

foreach (System.Reflection.PropertyInfo prop in typeof(SystemColors).GetProperties()) 
{ 
     if (prop.PropertyType.FullName == "System.Drawing.Color") 
         ColorComboBox.Items.Add(prop.Name); 
}