C# 的滚动条装饰?

cub*_*man 2 c# adornment visual-studio

在编写 Visual Studio 扩展时,有什么方法可以影响它如何呈现 c# 的地图模式滚动条?

在此输入图像描述

cub*_*man 6

我没有时间给出完整的答案,但我会写一个简短的答案,因为网上关于它的信息几乎为零:

  1. 创建 VSIX 解决方案。
  2. 添加项目并在“扩展性”类别中选择“编辑器边距”
  3. 在执行步骤 2 后与边距文件一起创建的“[yourEditorMarginName]Factory.cs”文件中,设置以下行:

        [MarginContainer(PredefinedMarginNames.VerticalScrollBar)]
        [Order(Before = PredefinedMarginNames.LineNumber)]
    
    Run Code Online (Sandbox Code Playgroud)
  4. 返回“[yourEditorMarginName].cs”文件。确保在构造函数中删除以下行:

        this.Height = 20;
        this.ClipToBounds = true;
        this.Width = 200;
    
    Run Code Online (Sandbox Code Playgroud)
  5. 现在您在构造函数内收到了对 IWpfTextView 的引用,注册其 OnLayoutChanged 事件(或使用适合您的其他一些事件):

        TextView.LayoutChanged += OnLayoutChanged;
    
    Run Code Online (Sandbox Code Playgroud)
  6. 在OnLayoutChanged中,您可以执行以下操作来添加矩形装饰:

        var rect = new Rectangle();
        double bottom;
        double firstLineTop;
        MapLineToPixels([someLineYouNeedToHighlight], out firstLineTop, out bottom);
        SetTop(rect, firstLineTop);
        SetLeft(rect, 0);
        rect.Height = bottom - firstLineTop;
        rect.Width = [yourWidth];
        Color color = [your Color];
        rect.Fill = new SolidColorBrush(color);
    
        Children.Add(rect);
    
    Run Code Online (Sandbox Code Playgroud)
  7. 这是 MapLineToPixels():

        private void MapLineToPixels(ITextSnapshotLine line, out double top, out double bottom)
        {
        double mapTop = ScrollBar.Map.GetCoordinateAtBufferPosition(line.Start) - 0.5;
        double mapBottom = ScrollBar.Map.GetCoordinateAtBufferPosition(line.End) + 0.5;
        top = Math.Round(ScrollBar.GetYCoordinateOfScrollMapPosition(mapTop)) - 2.0;
        bottom = Math.Round(ScrollBar.GetYCoordinateOfScrollMapPosition(mapBottom)) + 2.0;
        }
    
    Run Code Online (Sandbox Code Playgroud)
  8. 是的,ScrollBar变量可以通过以下方式获得:

    public ScrollbarMargin(IWpfTextView textView, IWpfTextViewMargin marginContainer/*, MarginCore marginCore*/)
    {
        ITextViewMargin scrollBarMargin = marginContainer.GetTextViewMargin(PredefinedMarginNames.VerticalScrollBar);
        ScrollBar = (IVerticalScrollBar)scrollBarMargin;
    
    Run Code Online (Sandbox Code Playgroud)