System.Drawing.Color 구조체의 색 목록을 컬렉션 또는 배열로 추출하는 방법은 무엇입니까?System.Drawing.Color에서 모든 색상 컬렉션을 얻으려면 어떻게해야합니까?
이 구조체를 기반으로 사용하는 것보다 색상 컬렉션을 얻는 더 효율적인 방법이 있습니까?
System.Drawing.Color 구조체의 색 목록을 컬렉션 또는 배열로 추출하는 방법은 무엇입니까?System.Drawing.Color에서 모든 색상 컬렉션을 얻으려면 어떻게해야합니까?
이 구조체를 기반으로 사용하는 것보다 색상 컬렉션을 얻는 더 효율적인 방법이 있습니까?
그래서 당신은 할 줄 :
string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));
는 ... 모든 collors를 배열을 얻을 수 있습니다.
또는 반사를 사용하면 색상을 얻을 수 있습니다. KnownColors에는 "메뉴"와 같은 항목, 시스템 메뉴의 색상 등이 포함됩니다. 이는 원하는 것이 아닐 수도 있습니다.
이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);
}
이 모든 색상을 기록,하지만 당신은 쉽게 목록에 색상 이름을 추가 할을 조정할 수 : 그래서, System.Drawing.Color의 색상의 단지 이름을 얻으려면, 당신은 반사를 사용할 수 있습니다 .
이 코드 프로젝트 프로젝트를 building a color chart에서 확인하십시오.
System.Drawing에는 알려진 시스템 색을 지정하는 Enum KnownColor가 있습니다.
목록 <> : List allColors = new List (Enum.GetNames (typeof (KnownColor))));
배열 [] 문자열 [] allColors = Enum.GetNames (typeof (KnownColor));
이 시도 :
jons911 당신은 "ActiveBorder"처럼 "이라는"색상이 아닌 시스템 색상을 원하는 경우Color
클래스는 IsSystemColor 속성이, 말뿐만 아니라
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
Trace.WriteLine(string.Format("{0}", knownColor));
}
당신이 할 수있는 그 (것)들을 걸러 내기 위하여 이용하십시오.
Here은 이름과 함께 각 색상의 간단한 견본을 보여주는 온라인 페이지입니다.
리플렉션을 사용하여 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);
여기 대부분의 대답은 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));
}