共用方式為


HOW TO:使用 UIHierarchy 管理樹狀檢視

更新:2007 年 11 月

在 Visual Studio 中,有些工具視窗 (例如 [巨集總管] 和 [方案總管]) 並沒有明確的 Automation 物件可以用來管理其內容。但是,這些工具視窗都有樹狀檢視 (也就是具有階層結構、大綱樣式的節點檢視),您可以用程式設計方式進行存取。UIHierarchy 物件即表示這些工具視窗中的樹狀檢視,可以讓您逐一查看並檢視各節點的內容。

物件名稱

說明

UIHierarchy 物件

表示指定工具視窗中的樹狀檢視。

UIHierarchyItems 集合

表示樹狀檢視中的所有節點。

UIHierarchyItem 物件

表示樹狀檢視中的一個節點。

使用這些物件和集合,您可以:

  • 選取 (單一或多重) 並檢視樹狀檢視中的節點。

  • 在樹狀檢視中上下移動插入點。

  • 傳回選取項目的值,或者讓它執行預設動作。

在 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)。

在下列範例中,將會示範如何使用這兩種技術,存取較低層的節點。

這些增益集 (Add-In) 範例會示範,如何參考及使用 UIHierarchy Automation 模型的各個成員,列出 [方案總管] 中的所有項目,以及 [巨集總管] 中 [範例] 節點下的所有巨集。

第一個範例會使用在 [方案總管] 中存取 [參考] 節點內容的 GetItem 方法策略。第二個範例會說明如何在 [巨集總管] 中執行相同的作業。如需如何執行增益集程式碼的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例

注意事項:

在下列程式碼範例中,會用不同的方式傳送輸出資料。[方案總管] 的範例會將資料傳送至 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

請參閱

工作

HOW TO:控制方案總管

HOW TO:變更視窗特性

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考