方法 : UIHierarchy を使用してツリー ビューを操作する

更新 : 2007 年 11 月

マクロ エクスプローラやソリューション エクスプローラなど、Visual Studio の一部のツール ウィンドウには、その内容を操作するために使用できる明示的なオートメーション オブジェクトがありません。ただし、これらのツール ウィンドウには、ツリー ビュー (階層構造でアウトラインを示すノードのビュー) があります。このビューには、プログラムでアクセスできます。UIHierarchy オブジェクトは、このようなツール ウィンドウのツリー ビューを表します。また、このオブジェクトにより、ツリー ビューを反復処理し、ノードの内容を表示できます。

オブジェクト名

説明

UIHierarchy オブジェクト

指定したツール ウィンドウにあるツリー ビューを表します。

UIHierarchyItems コレクション

ツリー ビューにあるすべてのノードを表します。

UIHierarchyItem オブジェクト

ツリー ビューにある単一のノードを表します。

これらのオブジェクトとコレクションを使用して、次の操作を行うことができます。

  • ツリー ビューで (1 つまたは多数の) ノードを選択して表示します。

  • ツリー ビュー内でカーソル位置を上下に移動します。

  • 選択した項目の値を返すか、選択した項目の既定のアクションが実行されます。

Visual Studio 2005 では、(ToolWindows からも返される) 新しい ToolWindows オブジェクトによって、Visual Studio のさまざまなツール ウィンドウを簡単に参照できます。たとえば、現在は、_applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) ではなく、_applicationObject.ToolWindows.OutputWindow を使用できます。

d790tay6.alert_note(ja-jp,VS.90).gifメモ :

使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

使用例

UIHierarchy オブジェクトは、ソリューション エクスプローラやマクロ エクスプローラなど、ツリー ビューを持つほとんどのツール ウィンドウの内容を表しますが、ツール ウィンドウ自体は Window オブジェクトのままです。UIHierarchyItems プロパティは、指定したツール ウィンドウのトップレベル ノードのコレクションを返します。ソリューション エクスプローラには、単一のトップレベル ノード (ソリューション) だけが表示されます。また、マクロ エクスプローラにも、単一のトップレベル ノード (マクロ ノード) だけが表示されます。その結果、このような特定のウィンドウのプロジェクト ノードは、ウィンドウの UIHierarchyItems コレクションではなく、トップレベル ノードのコレクションに含まれます。

この点を考慮に入れると、ツリー ビュー内の特定のノード (UIHierarchyItem) にアクセスするには次の 2 つの方法が考えられます。

  • GetItem メソッドを使用し、ソリューション/プロジェクト/項目というパターンを使用して目的のノードを直接参照します。

  • UIHierarchyItems.Item.UIHierarchyItems... (コレクション/アイテム/コレクションというパターン) を使用します。

    深い階層にあるノードに移動する場合は、このパターンを繰り返します。たとえば、トップレベル ノードの下位にあるノードに移動するには、UIHierarchy.UIHierarchyItems.Item(1).UIHierarchyItems.Item(2) を使用します。

この 2 つの方法を使用して下位のノードにアクセスする方法の例を次に示します。

これらのアドインの例では、UIHierarchy オートメーション モデルのさまざまなメンバを参照および使用し、ソリューション エクスプローラ内のすべての項目の一覧、およびマクロ エクスプローラの [Samples] ノードにあるすべてのマクロの一覧を表示する方法について説明します。

最初の例では、GetItem メソッドを使用して、ソリューション エクスプローラ内の [References] ノードの内容にアクセスします。2 番目の例では、マクロ エクスプローラに対して同じ処理を行う方法を示します。アドイン コードの実行方法の詳細については、「方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する」を参照してください。

d790tay6.alert_note(ja-jp,VS.90).gifメモ :

次のコード例では、さまざまな方法で出力を行います。ソリューション エクスプローラの例では、データを 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;
}

次の例では、UIHierarchy のノードにアクセスするために、UIHierarchyItems.Item.UIHierarchyItems のパターンを使用しています。この例では、マクロ エクスプローラの 2 番目の 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

参照

処理手順

方法 : ソリューション エクスプローラを制御する

方法 : ウィンドウの特性を変更する

概念

オートメーション オブジェクト モデルの階層図

その他の技術情報

環境ウィンドウの作成と制御

アドインおよびウィザードの作成

オートメーションと機能拡張のリファレンス