在编辑器中根据字符绘制一个正方形

时间:2017-10-23 07:41:02

标签: c# drawing visual-studio-extensions

我想通过点击其中一个来绘制匹配大括号的边框轮廓。就像ReSharpers Brace Matching option

代码本身有点有用,但我希望边框是一个简单的方块,我完全不知道如何实现它。

现在的样子:

way to complex border

我想要的是什么:

enter image description here

这是守则。它基于微软的例子,用于标记特定字符的Visual Studio Extentions。

private void Draw(int from, int to)
{
    var fromTo = new List<int> { from, to }.OrderBy(x => x).ToList();
    from = fromTo.First();
    to = fromTo.Last();

    var textViewLines = this.view.TextViewLines;

    var span = new SnapshotSpan(this.view.TextSnapshot, Span.FromBounds(from, to + 1));
    var geometry = (textViewLines as IWpfTextViewLineCollection).GetMarkerGeometry(span);
    if (geometry != null)
    {
        this.Draw(span, geometry);
    }
}

private void Draw(SnapshotSpan span, Geometry geometry)
{
    var drawing = new GeometryDrawing(new SolidColorBrush(), this.pen, geometry);
    drawing.Freeze();

    var drawingImage = new DrawingImage(drawing);
    drawingImage.Freeze();

    var image = new Image {
        Source = drawingImage
    };

    Canvas.SetLeft(image, geometry.Bounds.Left);
    Canvas.SetTop(image, geometry.Bounds.Top);

    this.layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
}

0 个答案:

没有答案