如何:使用 UIHierarchy 操作树视图
更新:2007 年 11 月
Visual Studio 中的某些工具窗口(如“宏资源管理器”和“解决方案资源管理器”)没有显式的自动化对象可用于操作其内容。但是,这些工具窗口确实具有一个可以按编程方式访问的树视图,它是一个分层的大纲样式的节点视图。UIHierarchy 对象表示这些工具窗口中的树视图,使您可以循环访问树视图和查看其节点的内容。
对象名 |
说明 |
---|---|
UIHierarchy 对象 |
表示指定工具窗口中的树视图。 |
表示树视图中的所有节点。 |
|
表示树视图中的一个节点。 |
通过使用这些对象和集合,您可以:
选择(一个或多个)并查看树视图中的节点。
在树视图中上下移动插入点。
返回选定项的值或使选定项执行其默认操作。
在 Visual Studio 2005 中,新的 ToolWindows 对象(也是从 ToolWindows 返回)使引用 Visual Studio 中的各种工具窗口更简单。例如,现在您可以使用 _applicationObject.ToolWindows.OutputWindow,而不需要使用 _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)。
说明: |
---|
显示的对话框和菜单命令可能会与“帮助”中描述的不同,具体取决于当前的设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。 |
示例
虽然 UIHierarchy 对象表示具有树视图的几乎任何工具窗口的内容,如“解决方案资源管理器”或“宏资源管理器”,但是工具窗口本身仍为 Window 对象。该 UIHierarchyItems 属性返回指定工具窗口中顶级节点的集合。在“解决方案资源管理器”中,只有一个顶级节点,即解决方案。在“宏资源管理器”中,也只有一个顶级节点,即“宏”节点。因此,这些特定窗口的项目节点位于顶级节点的集合中,而不是位于窗口的 UIHierarchyItems 集合中。
请记住以上原则,可以使用两种方法来访问树视图中的某个特定节点 (UIHierarchyItem):
通过使用 GetItem 方法可以直接引用使用解决方案/项目/项模式的所需节点。
通过使用 UIHierarchyItems.Item.UIHierarchyItems...(集合/项/集合模式)。
若要定位到更深的节点嵌套中,只需继续使用此模式即可。例如,若要转到从属于顶级节点的节点,请使用 UIHierarchy.UIHierarchyItems.Item(1).UIHierarchyItems.Item(2)。
下面的示例演示如何使用两种技术来访问较低级别节点的示例。
这些外接程序示例演示如何引用和使用 UIHierarchy 自动化模型的各种成员,以列出“解决方案资源管理器”中的所有项以及“宏资源管理器”的“示例”节点中的所有宏。
第一个示例使用 GetItem 方法策略访问“解决方案资源管理器”中的引用内容节点。第二个示例演示如何对“宏资源管理器”执行相同操作。有关如何运行外接程序代码的更多信息,请参见如何:编译和运行自动化对象模型代码示例。
说明: |
---|
下面的代码示例以不同方式形成其输出。针对“解决方案资源管理器”的示例将其数据发送给 Windows 消息框,而针对“宏资源管理器”的示例将其数据发送给“输出窗口”。每种方法都有效且与 UIHierarchy 的用法无关。 |
Imports System.Text
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
listSlnExpNodes(_applicationObject)
End Sub
Sub listSlnExpNodes(dte as DTE2)
' Requires reference to System.Text for StringBuilder.
Dim UIH As UIHierarchy = dte.ToolWindows.SolutionExplorer
' Set a reference to the first level nodes in Solution Explorer.
' Automation collections are one-based.
Dim UIHItem As UIHierarchyItem = _
UIH.GetItem("MyAddin1\MyAddin1\References")
Dim file As UIHierarchyItem
Dim sb As New StringBuilder
' Iterate through first level nodes.
For Each file In UIHItem.UIHierarchyItems
sb.AppendLine(file.Name)
' Iterate through second level nodes (if they exist).
Dim subitem As UIHierarchyItem
For Each subitem In file.UIHierarchyItems
sb.AppendLine(" " & subitem.Name)
Next
Next
MsgBox(sb.ToString)
End Sub
using System.Text;
public void OnConnection(object application, ext_ConnectMode _
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
listSlnExpNodes(_applicationObject);
}
public void listSlnExpNodes(DTE2 dte)
{
// Requires reference to System.Text for StringBuilder.
UIHierarchy UIH = dte.ToolWindows.SolutionExplorer;
// Set a reference to the first level nodes in Solution Explorer.
// Automation collections are one-based.
UIHierarchyItem UIHItem =
UIH.GetItem("MyAddin1\\MyAddin1\\References");
StringBuilder sb = new StringBuilder();
// Iterate through first level nodes.
foreach ( UIHierarchyItem file in UIHItem.UIHierarchyItems )
{
sb.AppendLine(file.Name);
// Iterate through second level nodes (if they exist).
foreach ( UIHierarchyItem subitem in file.UIHierarchyItems )
{
sb.AppendLine(" "+subitem.Name);
}
}
MessageBox.Show(sb.ToString());
}
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
ListMacroSamples1(_applicationObject)
End Sub
Sub ListMacroSamples1(ByVal dte As DTE2)
' Reference the UIHierarchy, UIHierarchyItem, and OutputWindow
' objects.
Dim UIH As UIHierarchy = CType(dte.Windows.Item _
(Constants.vsWindowKindMacroExplorer).Object, UIHierarchy)
Dim samples As UIHierarchyItem = UIH.GetItem("Macros\Samples")
Dim OWPane As OutputWindowPane = GetOutputWindowPane _
("List Macros", True)
Dim file As UIHierarchyItem
OWPane.Clear()
For Each file In samples.UIHierarchyItems
OWPane.OutputString(file.Name & _
Microsoft.VisualBasic.Constants.vbCrLf)
Dim macro As UIHierarchyItem
For Each macro In file.UIHierarchyItems
OWPane.OutputString(" " & macro.Name & _
Microsoft.VisualBasic.Constants.vbCrLf)
Next
Next
End Sub
Function GetOutputWindowPane(ByVal Name As String, Optional ByVal _
show As Boolean = True) As OutputWindowPane
' This is a support function for ListMacroSamples(). It provides
' the Output window to list the contents of the Sample node.
Dim win As Window = _applicationObject.Windows.Item _
(EnvDTE.Constants.vsWindowKindOutput)
If show Then win.Visible = True
Dim OW As OutputWindow = _applicationObject. _
ToolWindows.OutputWindow
Dim OWPane As OutputWindowPane
Try
OWPane = OW.OutputWindowPanes.Item(Name)
Catch e As System.Exception
OWPane = OW.OutputWindowPanes.Add(Name)
End Try
OWPane.Activate()
Return OWPane
End Function
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
ListMacroSamples1(_applicationObject);
}
public void ListMacroSamples1(DTE2 dte)
{
// Reference the UIHierarchy, UIHierarchyItem, and OutputWindow
// objects.
UIHierarchy UIH = (UIHierarchy)
dte.Windows.Item(Constants.vsWindowKindMacroExplorer).Object;
UIHierarchyItem samples = UIH.GetItem("Macros\\Samples");
OutputWindowPane OWPane = GetOutputWindowPane("List Macros", true);
OWPane.Clear();
foreach ( UIHierarchyItem fid in samples.UIHierarchyItems )
{
OWPane.OutputString(fid.Name+Environment.NewLine);
foreach ( UIHierarchyItem macro in fid.UIHierarchyItems )
{
OWPane.OutputString(" " + macro.Name +
Environment.NewLine);
}
}
}
public OutputWindowPane GetOutputWindowPane(string Name, bool show)
{
// This is a support function for ListMacroSamples(). It provides
// the Output window to list the contents of the Sample node.
Window win = _applicationObject.Windows.Item
(EnvDTE.Constants.vsWindowKindOutput);
if (show) { win.Visible = true; }
OutputWindow OW = _applicationObject.ToolWindows.OutputWindow;
OutputWindowPane OWPane;
try
{
OWPane = OW.OutputWindowPanes.Item(Name);
}
catch (System.Exception e)
{
OWPane = OW.OutputWindowPanes.Add(Name +
Environment.NewLine + e.Message);
}
OWPane.Activate();
return OWPane;
}
此示例使用 UIHierarchyItems.Item.UIHierarchyItems 模式来访问 UIHierarchy 中的节点。该示例列出“宏资源管理器”的第二个 UIHierarchy 节点中包含的宏。请注意,该示例也从上面的示例中调用函数 GetOutputWindowPane。
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
ListMacroSamples2(_applicationObject)
End Sub
Sub ListMacroSamples2(ByVal dte As DTE2)
Dim uih As UIHierarchy = CType(dte.Windows.Item _
(Constants.vsWindowKindMacroExplorer).Object, UIHierarchy)
' Set a reference to the second node in Macro Explorer. The
' collections are one-based.
Dim uihItem As UIHierarchyItem = _
uih.UIHierarchyItems.Item(1).UIHierarchyItems.Item(2)
Dim file As UIHierarchyItem
Dim owPane As OutputWindowPane = GetOutputWindowPane("List Macros")
For Each file In uihItem.UIHierarchyItems
owPane.OutputString(file.Name & _
Microsoft.VisualBasic.Constants.vbCrLf)
Dim macro As UIHierarchyItem
For Each macro In file.UIHierarchyItems
owPane.OutputString(" " & macro.Name & _
Microsoft.VisualBasic.Constants.vbCrLf)
Next
Next
End Sub
Function GetOutputWindowPane(ByVal Name As String, Optional ByVal _
show As Boolean = True) As OutputWindowPane
' This is a support function for ListMacroSamples(). It provides
' the Output window to list the contents of the Sample node.
Dim win As Window = _applicationObject.Windows.Item _
(EnvDTE.Constants.vsWindowKindOutput)
If show Then win.Visible = True
Dim OW As OutputWindow = _
_applicationObject.ToolWindows.OutputWindow
Dim OWPane As OutputWindowPane
Try
OWPane = OW.OutputWindowPanes.Item(Name)
Catch e As System.Exception
OWPane = OW.OutputWindowPanes.Add(Name)
End Try
OWPane.Activate()
Return OWPane
End Function
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
ListMacroSamples2(_applicationObject);
}
public void ListMacroSamples2(DTE2 dte)
{
UIHierarchy uih = (UIHierarchy)
dte.Windows.Item(Constants.vsWindowKindMacroExplorer).Object;
// Set a reference to the second node in Macro Explorer. The
// collections are one-based.
UIHierarchyItem uihItem =
uih.UIHierarchyItems.Item(1).UIHierarchyItems.Item(2);
OutputWindowPane owPane = GetOutputWindowPane("List Macros", true);
foreach ( UIHierarchyItem fid in uihItem.UIHierarchyItems )
{
owPane.OutputString(fid.Name+Environment.NewLine);
foreach ( UIHierarchyItem macro in fid.UIHierarchyItems )
{
owPane.OutputString(" " + macro.Name +
Environment.NewLine);
}
}
}
public OutputWindowPane GetOutputWindowPane(string Name, bool show)
{
// This is a support function for ListMacroSamples(). It provides
// the Output window to list the contents of the Sample node.
Window win = _applicationObject.Windows.Item
(EnvDTE.Constants.vsWindowKindOutput);
if (show) { win.Visible = true; }
OutputWindow OW = _applicationObject.ToolWindows.OutputWindow;
OutputWindowPane OWPane;
try
{
OWPane = OW.OutputWindowPanes.Item(Name);
}
catch (System.Exception e)
{
OWPane = OW.OutputWindowPanes.Add(Name + Environment.NewLine +
e.Message);
}
OWPane.Activate();
return OWPane;
}
下面的宏示例演示如何使用 UIHierarchy 列出“解决方案资源管理器”窗口的树视图的内容。
Sub cvTreeView()
Dim uih As UIHierarchy = DTE.ToolWindows.SolutionExplorer
Dim uihItem As UIHierarchyItem
Dim uihItems As UIHierarchyItems = uih.UIHierarchyItems
Dim msg As String
For Each uihItem In uihItems
msg += uihItem.Name & vbCr
Next
MsgBox(msg)
End Sub