0

我正在尝试在 WPF 中绘制许多类似的线条,如问题所示,但不同之处在于我需要在 DrawingVisual 对象的 DrawingContext 上绘制它。如何做呢?

4

1 回答 1

0

您必须在彼此顶部的相同点之间绘制两条线,较低的用较粗的笔,较高的用较细的笔。

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

当您还定义笔线帽时,这条线看起来好多了:

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

使用这样的扩展方法

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

您可以在一个电话中完成绘图:

drawingContext.DrawLine(background, foreground, new Point(100, 100), new Point(200, 200));
于 2012-04-20T16:52:42.177 回答