Share via


방법: UIHierarchy를 사용하여 트리 뷰 조작

업데이트: 2007년 11월

매크로 탐색기와 솔루션 탐색기 같은 일부 Visual Studio 도구 창에는 안에 포함된 내용을 조작하는 데 사용할 수 있는 명시적 자동화 개체가 없습니다. 그러나 이러한 도구 창에는 프로그래밍 방식으로 액세스할 수 있는 개요 스타일의 계층적 노드 뷰인 트리 뷰가 있습니다. UIHierarchy 개체는 이러한 도구 창의 트리 뷰를 나타냅니다. 이 개체를 사용하여 트리 뷰를 반복하며 노드의 내용을 볼 수 있습니다.

개체 이름

설명

UIHierarchy 개체

지정된 도구 창의 트리 뷰를 나타냅니다.

UIHierarchyItems 컬렉션

트리 뷰의 모든 노드를 나타냅니다.

UIHierarchyItem 개체

트리 뷰의 단일 노드를 나타냅니다.

이러한 개체와 컬렉션을 사용하여 다음 작업을 수행할 수 있습니다.

  • 트리 뷰에서 하나 또는 여러 개의 노드 선택 및 보기

  • 트리 뷰에서 삽입 지점을 위아래로 이동

  • 선택된 항목의 값을 반환하거나 기본 동작을 수행하도록 지정

Visual Studio 2005에서는 새로운 ToolWindows 개체를 사용하여 Visual Studio의 여러 가지 도구 창을 더 쉽게 참조할 수 있습니다. 이 개체는 ToolWindows에서도 반환됩니다. 예를 들어, _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)을 사용하는 대신 _applicationObject.ToolWindows.OutputWindow를 사용할 수 있습니다.

참고:

표시되는 대화 상자와 메뉴 명령은 실제 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 이러한 절차는 일반 개발 설정을 사용하여 개발되었습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 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

참고 항목

작업

방법: 솔루션 탐색기 제어

방법: 창 특성 변경

개념

자동화 개체 모델 차트

기타 리소스

환경 창 만들기 및 제어

추가 기능 및 마법사 만들기

자동화 및 확장성 참조