技术开发 频道

VS2010 Extension特性实践:Feature开发

  接下来要实现工具栏的界面,这个就不多说了,建一个UserControl,里面放上ToolBar就搞定了。那么何时何地显示这个ToolBar呢?这就要依赖IWpfTextView的SelectionChanged事件了,上面提到会挂这个事件就是为这里用的。

private void MayBeAdornmentShowCondition()
{
    
if (!this._textView.Selection.IsEmpty)
     {
         SnapshotPoint startPos
= this._textView.Selection.Start.Position;
         SnapshotPoint endPos
= this._textView.Selection.End.Position;
         IWpfTextViewLine textViewLineContainingBufferPosition
= this._textView.GetTextViewLineContainingBufferPosition(startPos);
         TextBounds characterBounds
= textViewLineContainingBufferPosition.GetCharacterBounds(startPos);
         TextBounds bounds2
= this._textView.GetTextViewLineContainingBufferPosition(endPos).GetCharacterBounds(endPos);
if (this._fromMouseHover)
{
this._mustHaveAdornmentDisplayed = true;
}
else
{
PELeftButtonMouseProcessor property
= null;
try
{
property
= this._textView.Properties.GetProperty<PELeftButtonMouseProcessor>(typeof(PELeftButtonMouseProcessor));
}
catch
{
}
this._mustHaveAdornmentDisplayed = (property != null)
  
&& (property.IsLeftButtonDown
|| ((DateTime.Now - property.LastLeftButtonDownTime).TotalMilliseconds < 400.0));
}
if (this._mustHaveAdornmentDisplayed)
{
TextBounds selectionBounds
= !this._textView.Selection.IsReversed ? bounds2 : characterBounds;
int offset = 7;
double top = selectionBounds.Top + (!this._textView.Selection.IsReversed ? (offset + textViewLineContainingBufferPosition.Height) : (-offset - this._adornmentUI.ActualHeight));
if (top < 0.0)
top
= 0.0;
}
double left = characterBounds.Left + ((bounds2.Left - characterBounds.Left) / 2.0);
if ((left + this._adornmentUI.ActualWidth) > this._textView.ViewportWidth)
{
left
= this._textView.ViewportWidth - this._adornmentUI.ActualWidth;
}
Canvas.SetTop(
this._adornmentUI, top);
Canvas.SetLeft(
this._adornmentUI, left);
long chars = 0L;
try
{
chars
= this._textView.Selection.SelectedSpans[0].Span.Length;
}
catch
{
}
this._adornmentUI.SetStatus(chars);
this.RenderSelectionPopup();
}
}
else
{
this._mustHaveAdornmentDisplayed = false;
this._adornmentLayer.RemoveAdornmentsByTag(this._adornmentTag);
}
}
private void RenderSelectionPopup()
{
if (this._mustHaveAdornmentDisplayed)
{
IAdornmentLayerElement element
= null;
try
{
element
= this._adornmentLayer.Elements.First<IAdornmentLayerElement>(
(IAdornmentLayerElement ile)
=> ile.Tag.ToString() == this._adornmentTag);
}
catch (InvalidOperationException)
{
}
if (element == null)
{
this._adornmentLayer.AddAdornment(this._textView.Selection.SelectedSpans[0], this._adornmentTag, this._adornmentUI);
}
this._timer.Stop();
this._timer.Start();
}
}
private void selection_SelectionChanged(object sender, EventArgs e)
{
this._fromMouseHover = false;
this.MayBeAdornmentShowCondition();
}

  然后要注意的是IWpfTextView的Closed事件处理要记得取消所有挂这个事件等等收尾工作。

  接下来编译工程,打包VSIX就完成了,目前实现的主要Feature:

  ·当在代码编辑器中选择一段文字,并将鼠标移到文字区域时,QuickToolbar会以半透明的方式“浮”文字的旁边。

  ·当鼠标移到QuickToolbar区域,QuickToolbar会变成不透明,其上的按钮会响应鼠标动作。

  ·目前支持的操作有:

  剪切(Cut)

  复制(Copy)

  粘贴(Paste)

  删除(Delete)

  减小缩进(Decrease Indent)

  增加缩进(Increase Indent)

  注释代码(Comment)

  取消注释(Uncomment)

  等等

0
相关文章