有没有“笔”?补充“画笔”的类和“颜色”在 WPF 中?

发布于 2024-12-23 08:11:48 字数 190 浏览 4 评论 0原文

确实没有一个叫做 Pens 的东西,但是找不到它就太奇怪了。是真的没有,还是我眼瞎了?

(我只是在寻找一个方便的类 - 例如,Colors.RedBrushes.Red 就在那里,但对于钢笔来说,最短的似乎是 new笔(画笔.红色,1)

There isn't one called Pens, that's for sure, but it's too odd not to find it. Is it really absent, or am I being blind?

(I'm just looking for a convenience class - for example, Colors.Red and Brushes.Red is there, but for pens the shortest appears to be new Pen(Brushes.Red, 1))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2024-12-30 08:11:48

没有关联的 Pens 类,因为 Pen 是由 BrushThickness 而不仅仅是一个属性。

如果您需要一套标准笔(基于一定的厚度),那么您将必须创建自己的笔。另一种方法是创建一个代码生成器来提供适当的笔。

下面是一个为每个默认 Brush 添加一个 Pen 的类示例:

public class DefaultPens
{
    private Dictionary<Brush, Pen> pens;

    public double Thickness
    {
        get;
        private set;
    }

    // used like defaultPens[Brushes.Red]
    public Pen this[Brush brush]
    {
        get { return this.pens[brush]; }
    }

    public DefaultPens(double thickness)
    {
        this.pens = typeof(Brushes).GetProperties(BindingFlags.Static)
                                   .Where(pi => pi.PropertyType == typeof(Brush))
                                   .Select(pi => pi.GetValue(null, null))
                                   .ToDictionary(
                                       b => b,
                                       b => new Pen(b, thickness));
        this.Thickness = thickness;
    }
}

There is not the associated Pens class because a Pen is identified by both a Brush and a Thickness rather than just one property.

If you have a need for a set of standard pens (based on some thickness) then you will have to create your own. Another approach would be to create a code generator to supply the appropriate pens.

Here is an example of a class which adds a Pen for each default Brush:

public class DefaultPens
{
    private Dictionary<Brush, Pen> pens;

    public double Thickness
    {
        get;
        private set;
    }

    // used like defaultPens[Brushes.Red]
    public Pen this[Brush brush]
    {
        get { return this.pens[brush]; }
    }

    public DefaultPens(double thickness)
    {
        this.pens = typeof(Brushes).GetProperties(BindingFlags.Static)
                                   .Where(pi => pi.PropertyType == typeof(Brush))
                                   .Select(pi => pi.GetValue(null, null))
                                   .ToDictionary(
                                       b => b,
                                       b => new Pen(b, thickness));
        this.Thickness = thickness;
    }
}
守望孤独 2024-12-30 08:11:48

没有 Pens 类,因为它是多余的。(尽管你是对的:Colors画笔也有些多余。)

为什么?好吧,想想笔是什么:它的属性可以通过笔画(或:粗细)和颜色(或:画笔)来描述。因此,有一个画笔构造函数(Brush, double)

There is no Pens class since it would be redundant. (Although you're right: Colors and Brushes are somewhat redundant, too.)

Why? Well, just think about waht a pen is: its properties can be described by a stroke with (or: thickness) and a color (or: brush). Therefore, there is a Pen Constructor(Brush, double).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文