5

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

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

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

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

4 回答 4

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);
于 2013-02-28T11:21:39.200 回答
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;

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

于 2013-02-28T11:25:05.503 回答
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;
}
于 2013-02-28T11:25:10.217 回答
1

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

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

记住using System.Windows.Media;

于 2013-02-28T12:19:44.120 回答