2008-11-10 60 views

回答

19

所以,你會怎麼做:

string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor)); 

...把​​所有的collors的數組。

或...您可以使用反射來獲取顏色。 KnownColors包括諸如「菜單」,系統菜單的顏色等項目,這可能不是你想要的。因此,要獲得的System.Drawing.Color顏色的只是名字,你可以使用反射:

Type colorType = typeof(System.Drawing.Color); 

PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public); 

foreach (System.Reflection.PropertyInfo c in propInfoList) { 
    Console.WriteLine(c.Name); 
} 

此寫出所有的顏色,但你可以很容易地調整它的顏色名稱添加到列表。

看看這個代碼項目項目building a color chart

1

在System.Drawing中有一個Enum KnownColor,它指定已知的系統顏色。

List <>: List allColors = new List(Enum.GetNames(typeof(KnownColor)));

Array [] string [] allColors = Enum.GetNames(typeof(KnownColor));

6

試試這個:

foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor))) 
{ 
    Trace.WriteLine(string.Format("{0}", knownColor)); 
} 
4

除了什麼jons911說,如果你只希望「命名」的顏色,而不是系統的顏色,如「ActiveBorder」,該Color類有一個IsSystemColor屬性,你可以用來過濾掉。

1

Here是一個在線頁面,顯示每種顏色的方便色板及其名稱。

1

您必須使用反射才能從System.Drawing.Color結構中獲取顏色。

System.Collections.Generic.List<string> colors = 
     new System.Collections.Generic.List<string>(); 
Type t = typeof(System.Drawing.Color); 
System.Reflection.PropertyInfo[] infos = t.GetProperties(); 
foreach (System.Reflection.PropertyInfo info in infos) 
    if (info.PropertyType == typeof(System.Drawing.Color)) 
     colors.Add(info.Name); 
4

這裏的大多數答案都會導致顏色名稱(字符串)而不是System.Drawing.Color對象的集合。如果您需要實際系統顏色的集合,請使用:

using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
... 
static IEnumerable<Color> GetSystemColors() { 
    Type type = typeof(Color); 
    return type.GetProperties().Where(info => info.PropertyType == type).Select(info => (Color)info.GetValue(null, null)); 
} 
相關問題