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

本文关键字:画笔 一个 获取 方式 编程 Brushes | 更新日期: 2023-09-27 18:01:44

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

  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中的静态画笔类中选择一个现有的画笔,就像这样

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类中的方法

以编程方式从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);
    }
}
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);

反射方法

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;

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

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

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

其他的答案很复杂。下面是将字符串"purple"转换为纯色画笔的一行代码:

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

记忆using System.Windows.Media;