以编程方式从Brushes类中获取画笔?

时间:2013-02-28 11:17:01

标签: c# brush brushes

我有一个属性,允许将已知颜色的字符串名称发送到我的控件。该属性仅接受适当的已知颜色名称,如“红色”或“蓝色”

  private KnownColor _UseColor = KnownColor.Red;

    /// <summary>
    /// Gets or sets the name of the colour
    /// </summary>
    public string ColorName
    {
        get
        {
            return this._UseColor.ToString();
        }
        set
        {
            if (Enum.IsDefined(typeof(KnownColor), value))
                this._UseColour = (KnownColor)Enum.Parse(typeof(KnownColor), value);
        }
    }

我想要做的是使用这个_UseColour枚举从.NET中的静态Brushes类中选择一个现有的画笔

Brush sysBrush = Brushes.FromKnownColor(this._UseColor);
e.Graphics.FillRectangle(sysBrush, 0, 0, 10, 10);

而不是在控件被绘制时创建新的画笔

using (SolidBrush brsh = new SolidBrush(Color.FromKnownColor(this._UseColor)))
    e.Graphics.FillRectangle(brsh, 0, 0, 10, 10);

有没有人知道这是否可行,或者我每次都要创建一个新画笔?

Brushes.FromKnownColor不是Brushes

中的方法

4 个答案:

答案 0 :(得分:4)

为什么不创建一次刷子并将其缓存以供以后使用?

在你的主要课程中:

private KnownColor _UseColor = KnownColor.Red;

/// <summary>
/// Gets or sets the name of the colour
/// </summary>
public string ColorName
{
    get
    {
        return this._UseColor.ToString();
    }
    set
    {
        if (Enum.IsDefined(typeof(KnownColor), value))
            this._UseColour = (KnownColor)Enum.Parse(typeof(KnownColor), value);
    }
}

private Dictionary<string, Brush> _knownBrushes = new Dictionary<string, Brush>();

public Brush ColorBrush
{
    get
    {
        if (!_knownBrushes.ContainsKey(_UseColor)) {
            _knownBrushes[_UseColor] = new SolidBrush(Color.FromKnownColor(this._UseColor));
        }

        return _knownBrushes[_UseColor];
    }
}

然后像...一样使用它。

e.Graphics.FillRectangle(ColorBrush, 0, 0, 10, 10);

答案 1 :(得分:2)

反思方法

var properties = typeof (Brushes).GetProperties();
var property = properties.FirstOrDefault(p => p.Name == "Red");
var brush = property.GetValue(null, null); // Contains Brushes.Red

您的案例

字段:

PropertyInfo[] _properties = typeof (Brushes).GetProperties();

静态方法

static Brush GetKnownBrush(string knownColorName)
{
    var property = _properties.FirstOrDefault(p => p.Name == knownColorName);
    var brush = property.GetValue(null, null);
    return brush;
}

用法:

var knownBrush = GetKnownBrush(ColorName);

实例属性

Brush KnownBrush
{
    get
    {
        var property = _properties.FirstOrDefault(p => p.Name == ColorName);
        var brush = property.GetValue(null, null);
        return brush;
    }
}

用法:

var knownBrush = KnownBrush;

您还可以将经常使用的画笔存储在字典中,以避免反射操作。

答案 2 :(得分:1)

如果您想要一种可以通过颜色查找画笔的解决方案,即使颜色可能没有已知名称,您也可以创建一个使用该颜色的字典:

void Main()
{
    var brush = KnownBrush(Color.FromArgb(255, 0, 0));
    brush.Dump();
}

private static Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush> _KnownBrushes;
public static SolidBrush KnownBrush(Color color)
{
    if (_KnownBrushes == null)
    {
        _KnownBrushes = new Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush>();
        foreach (var propertyInfo in typeof(Brushes).GetProperties())
        {
            if (propertyInfo.PropertyType == typeof(Brush))
            {
                var brush = propertyInfo.GetValue(null) as SolidBrush; // not a typo
                if (brush != null)
                    _KnownBrushes[Tuple.Create(brush.Color.R, brush.Color.G, brush.Color.B, brush.Color.A)] = brush;
            }
        }
    }

    SolidBrush result;
    _KnownBrushes.TryGetValue(Tuple.Create(color.R, color.G, color.B, color.A), out result);
    return result;
}

答案 3 :(得分:1)

其他答案很复杂。这是一个单行将字符串“purple”转换为纯色画笔:

new SolidColorBrush((Color)ColorConverter.ConvertFromString("purple"))

记住using System.Windows.Media;