通过'刷'循环

时间:2011-06-14 21:26:32

标签: vb.net

我想获得所有的Brushes,这基本上是伪代码,它将解释我正在尝试做什么:

For Each B in Brushes
   'Something with Brushes
End For

画笔虽然是一种类型,但我怎么能这样做呢?

3 个答案:

答案 0 :(得分:4)

你能这样做吗?

dim brush as new Brush() 'needs a proper brush instance, not sure where there is one, so this line won't work
Dim type As Type = GetType(System.Drawing.Brushes)
Dim properties As PropertyInfo() = type.GetProperties(BindingFlags.Static)
For Each [property] As PropertyInfo In properties
    Console.WriteLine("{0} = {1}", [property].Name, [property].GetValue(brush, Nothing))
Next

答案 1 :(得分:3)

不使用反射:

Dim brushes = [Enum].GetValues(GetType(KnownColor)) _
    .Cast(Of KnownColor)() _
    .Where(Function(k) k >= KnownColor.Transparent AndAlso k < KnownColor.ButtonFace) _ '//Exclude system colors
    .Select(Function(k) New SolidBrush(Color.FromKnownColor(k)))

修改(来自Thomas评论)

获取颜色名称(用于画笔)

Dim brushColorNames = [Enum].GetValues(GetType(KnownColor)) _
    .Cast(Of KnownColor)() _
    .Where(Function(k) k >= KnownColor.Transparent AndAlso k < KnownColor.ButtonFace) _ '//Exclude system colors
    .Select(Function(k) k.ToString())

答案 2 :(得分:2)

这取决于你想要用刷子做什么。

For Each b in GetType(Brushes).GetProperties
  Dim colorName = b.Name ' If you want color names (AliceBlue through YellowGreen)
  Dim brushValue = b.GetValue(Nothing, Nothing) ' Gives you a Brush
  Dim brushColor = brushValue.Color ' Gives you the hex color of the brush (AliceBlue = #FFF0F8FF)
Next