//<summary> //Instantiates a ViewportAdornment1 manager when a textView is created. //</summary> //<param name="textView">The <see cref="IWpfTextView"/> upon which the adornment should be placed</param> public void TextViewCreated(IWpfTextView textView) { IComponentModel componentModel = ServiceProvider.GetService(typeof(SComponentModel)) as IComponentModel; IVsEditorAdaptersFactoryService editorFactory = componentModel.GetService <IVsEditorAdaptersFactoryService>(); IServiceProvider sp = Package.GetGlobalService( typeof(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)) as Microsoft.VisualStudio.OLE.Interop.IServiceProvider; IVsTextView vsTextView = editorFactory.CreateVsTextViewAdapter(sp); var tve = new TextViewExtensions(editorFactory); new ViewportAdornment1(textView, tve); }
/// <summary> /// Creates a square image and attaches an event handler to the layout changed event that /// adds the the square in the upper right-hand corner of the TextView via the adornment layer /// </summary> /// <param name="view">The <see cref="IWpfTextView"/> upon which the adornment will be drawn</param> public ViewportAdornment1(IWpfTextView view, TextViewExtensions tve) { _wpfTextView = view; _textView = view; _tve = tve; Brush brush = new SolidColorBrush(Colors.Red); brush.Freeze(); Brush penBrush = new SolidColorBrush(Colors.Red); penBrush.Freeze(); Pen pen = new Pen(penBrush, 0.5); pen.Freeze(); //draw a square with the created brush and pen System.Windows.Rect r = new System.Windows.Rect(0, 0, 30, 30); Geometry g = new RectangleGeometry(r); GeometryDrawing drawing = new GeometryDrawing(brush, pen, g); drawing.Freeze(); DrawingImage drawingImage = new DrawingImage(drawing); drawingImage.Freeze(); _image = new Image(); _image.Source = drawingImage; //Grab a reference to the adornment layer that this adornment should be added to _adornmentLayer = view.GetAdornmentLayer("ViewportAdornment1"); _wpfTextView.ViewportHeightChanged += delegate { this.onSizeChange(); }; _wpfTextView.ViewportWidthChanged += delegate { this.onSizeChange(); }; _wpfTextView.MouseHover += _view_MouseHover; }