开发者

Looping through 'Brush'

开发者 https://www.devze.com 2023-03-13 12:08 出处:网络
I want to get all of the Brushes, this is basically pseudocode which will explain what I\'m trying to do:

I want to get all of the Brushes, this is basically pseudocode which will explain what I'm trying to do:

For Each B 开发者_运维知识库in Brushes
   'Something with Brushes
End For

Brushes is a type though so how would I be able to do that?


Can you do this?

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


Not using reflection:

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)))

Edit (from Thomas comment)

To get color names (used for brushes)

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())


It depends what you want to do with the Brushes.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号