简体   繁体   English

在WPF中使用外部线在DrawingContext上绘制线

[英]Drawing lines in WPF on DrawingContext with outer lines

I'm trying to draw a lot of similar kind of lines in WPF as seen in this question, but the difference is that I need to draw it on a DrawingContext of a DrawingVisual object. 我正在尝试在WPF中绘制很多类似的线条,如问题所示,但不同之处在于我需要在DrawingVisual对象的DrawingContext上绘制它。 How can it be done? 如何做呢?

You would have to draw two lines between identical points on top of each other, the lower with a thicker pen, the upper with a thinner pen. 您将必须在彼此顶部的相同点之间绘制两条线,下部使用较粗的笔,上部使用较细的笔。

Pen background = new Pen(Brushes.Black, 5);
Pen foreground = new Pen(Brushes.White, 3);

drawingContext.DrawLine(background, new Point(100, 100), new Point(200, 200));
drawingContext.DrawLine(foreground, new Point(100, 100), new Point(200, 200));

This lines look at lot better when you also define the pen line caps: 当您还定义笔线帽时,这些线看起来更好:

Pen background = new Pen
{
    Brush = Brushes.Black,
    Thickness = 5,
    StartLineCap = PenLineCap.Round,
    EndLineCap = PenLineCap.Round
};

Pen foreground = new Pen
{
    Brush = Brushes.White,
    Thickness = 3,
    StartLineCap = PenLineCap.Round,
    EndLineCap = PenLineCap.Round
};

With an extension method like this 使用这样的扩展方法

public static class DrawingContextExtensions
{
    public static void DrawLine(this DrawingContext drawingContext,
        Pen background, Pen foreground, Point start, Point end)
    {
        drawingContext.DrawLine(background, start, end);
        drawingContext.DrawLine(foreground, start, end);
    }
}

you could do the drawing in one call: 您可以在一个电话中绘制图纸:

drawingContext.DrawLine(background, foreground, new Point(100, 100), new Point(200, 200));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM