VSPackge插件系列:常用IDE功能的封装

时间:2022-05-09 14:40:05

  继上一篇VSPackge插件系列简单介绍如何正确的获取DTE之后,就一直没发VSPackge插件系列的文章了,最近同事也想了解如何在代码中与VS交互,特发一篇文章示例一些简单功能是如何调用,也以备以后查阅之用。

  为了方便的调用vs的提供的服务,我这里写了一个类对vs的一些常用功能进行封装了,像什么当前选择项,当前编辑器,当前窗口,输出窗口的调用等等。还有解决方案里的选择改变事件的监听等等,更多功能还待挖掘中。

    public class Ide
{ private Ide()
{
_dte2 = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
_dte2.Events.SelectionEvents.OnChange += new _dispSelectionEvents_OnChangeEventHandler(SelectionEvents_OnChange);
} #region Singlton private static Ide _Ide = new Ide(); public static Ide Instance
{
get
{
return _Ide;
}
}
#endregion #region Propertys private DTE2 _dte2 = null; public DTE2 DTE2
{
get
{
return _dte2;
}
} public Window ActiveWindow
{
get
{
CheckDTE();
return _dte2.ActiveWindow;
}
} public Document ActiveDocument
{
get
{
CheckDTE();
return _dte2.ActiveDocument;
}
} public IEnumerable<Project> ActiveProjects
{
get
{
CheckDTE();
Array prjs = (Array)_dte2.ActiveSolutionProjects;
foreach (var prj in prjs)
{
yield return prj as Project;
} }
} public Project SelectedProject
{
get
{
if (ActiveProjects.Count() > )
{
throw new Exception("Project is Multi-Selection");
}
return ActiveProjects.FirstOrDefault();
}
} public ToolWindows ToolWindows
{
get
{
CheckDTE();
return _dte2.ToolWindows;
}
} /// <summary>
/// 解决方案管理器中显示层次结构树数据的项
/// </summary>
public IEnumerable<UIHierarchyItem> ExplorerSelectedItems
{
get
{
CheckDTE();
Array selectedItems = (Array)_dte2.ToolWindows.SolutionExplorer.SelectedItems;
foreach (var item in selectedItems)
{
yield return item as UIHierarchyItem;
} }
} /// <summary>
/// 返回选择的工程或工程中的项
/// </summary>
public IEnumerable<SelectedItem> SelectedItems
{
get
{
CheckDTE();
SelectedItems selectedItems = _dte2.SelectedItems;
foreach (var item in selectedItems)
{
yield return item as SelectedItem;
} }
} public StatusBar StatusBar
{
get
{
CheckDTE();
return _dte2.StatusBar;
}
} public OutputWindowPane OutputWindow
{
get
{
var outpanes = ToolWindows.OutputWindow.OutputWindowPanes;
foreach (OutputWindowPane pane in outpanes)
{
if (pane.Name.Equals("GLIde"))
{
return pane;
}
}
return ToolWindows.OutputWindow.OutputWindowPanes.Add("GLIde");
}
} public ErrorList ErrorWindow
{
get
{
return ToolWindows.ErrorList;
}
} private void CheckDTE()
{
if (_dte2 == null)
{
throw new Exception("DTE service not found");
}
}
#endregion #region Events public event Action<object, EventArgs> SelectionChanged; void SelectionEvents_OnChange()
{
if (SelectionChanged != null)
{
SelectionChanged(this, new EventArgs());
}
} #endregion } public static class Extentions
{
public static void WriteLine(this OutputWindowPane outputPane, string context)
{
outputPane.OutputString(context + Environment.NewLine);
}
}

这其中也参考了一篇比较旧的文章,看来研究VS的人不是甚多,不知道是何原因,还是各位大侠私藏了自己的技术。

http://www.cnblogs.com/dragon/archive/2005/04/01/130204.html

以下是调用示例代码,状态栏的文本提示,处理动画,获取选中项名称,获取当前选中工程,当前激活窗口等等。

        private void button1_Click(object sender, RoutedEventArgs e)
{
Ide.Instance.StatusBar.Progress(true, "doing something", , );
Ide.Instance.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationDeploy);
//Ide.Instance.StatusBar.Progress(false); tbDisplay.Text += "SelectedItems:" + Environment.NewLine;
foreach (var item in Ide.Instance.SelectedItems)
{
tbDisplay.Text += item.Name + Environment.NewLine;
} tbDisplay.Text += "ExplorerSelectedItems:" + Environment.NewLine;
foreach (var item in Ide.Instance.ExplorerSelectedItems)
{
tbDisplay.Text += item.Name + Environment.NewLine;
} tbDisplay.Text += "ActiveProjects:" + Environment.NewLine;
foreach (var item in Ide.Instance.ActiveProjects)
{
tbDisplay.Text += item.Name + Environment.NewLine;
} tbDisplay.Text += "ActiveWindow:" + Environment.NewLine;
tbDisplay.Text += Ide.Instance.ActiveWindow.Caption + Environment.NewLine; //tbDisplay.Text += "ActiveDocument:" + Environment.NewLine;
//tbDisplay.Text += Ide.Instance.ActiveDocument.Name + Environment.NewLine; //tbDisplay.Text += "SelectedProject:" + Environment.NewLine;
//tbDisplay.Text += Ide.Instance.SelectedProject.Name + Environment.NewLine; Ide.Instance.OutputWindow.WriteLine("Hello world!"); }

效果截图

VSPackge插件系列:常用IDE功能的封装

欢迎各位提供更多的使用心得,共同进步!