Reputation: 946
When writing a Visual Studio extension, is there any way to influence how it renders map-mode scroll bar for c#?
Upvotes: 2
Views: 1127
Reputation: 946
I don't have time for the full answer, but I will write the short one because there is next to zero info about it on the net:
In the "[yourEditorMarginName]Factory.cs" file that was created alonside the margin file after you did step 2 set the following lines:
[MarginContainer(PredefinedMarginNames.VerticalScrollBar)]
[Order(Before = PredefinedMarginNames.LineNumber)]
Go back to the "[yourEditorMarginName].cs" file. Make sure that you remove the following lines in the constructor:
this.Height = 20;
this.ClipToBounds = true;
this.Width = 200;
Now you received a reference to IWpfTextView inside the constructor, sign up to its OnLayoutChanged event (or use some other event that suits you):
TextView.LayoutChanged += OnLayoutChanged;
In the OnLayoutChanged, you can do the following to add a rectangle adornment:
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);
And here is 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;
}
Yeah the ScrollBar
variable is something you can get this way:
public ScrollbarMargin(IWpfTextView textView, IWpfTextViewMargin marginContainer/*, MarginCore marginCore*/)
{
ITextViewMargin scrollBarMargin = marginContainer.GetTextViewMargin(PredefinedMarginNames.VerticalScrollBar);
ScrollBar = (IVerticalScrollBar)scrollBarMargin;
Upvotes: 6