Share via


如何:控制解决方案资源管理器

更新:2007 年 11 月

“解决方案资源管理器”是 Visual Studio 集成开发环境 (IDE) 中的工具窗口,它显示解决方案的内容,其中包含解决方案的项目和每个项目的项。与 Visual Studio 中的其他工具窗口一样,可以控制它的物理参数,如大小、位置以及它是停靠的还是自由浮动的。有关如何操作该工具窗口和其他 Visual Studio 工具窗口的信息,请参见如何:更改窗口特性

“解决方案资源管理器”本身没有自己的自动化对象,但是可以使用 UIHierarchy 在一定程度上控制其层次结构的内容。若要控制解决方案中的项目和项目项,请使用项目自动化模型。有关更多信息,请参见 控制项目和解决方案

0ah3b098.alert_note(zh-cn,VS.90).gif说明:

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您的当前设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

使用 UIHierarchy 控制解决方案资源管理器

  1. 如果“解决方案资源管理器”不可见,请单击“视图”菜单上的“解决方案资源管理器”。

  2. 打开一个含有许多元素的项目,如外接程序项目。

  3. 在“解决方案资源管理器”中单击至少包含两个子节点的节点。

  4. 运行以下代码。

示例

该示例演示如何通过使用 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)
    ' Pass the applicationObject member variable to the code example.
    slnExplUIHierarchyExample(_applicationObject)
End Sub

Sub slnExplUIHierarchyExample(ByVal dte As DTE2)
    Dim UIH As UIHierarchy = dte.ToolWindows.SolutionExplorer
    ' Requires a reference to System.Text.
    ' Set a reference to the first level nodes in Solution Explorer. 
    ' Automation collections are one-based.
    Dim UIHItem As UIHierarchyItem = UIH.UIHierarchyItems.Item(1)
    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)
            ' Iterate through third level nodes (if they exist).
            Dim subSubItem As UIHierarchyItem
            For Each subSubItem In subitem.UIHierarchyItems
                sb.AppendLine("        " & subSubItem.Name)
            Next
        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;
    // Pass the applicationObject member variable to the code example.
    slnExplUIHierarchyExample(_applicationObject);
}

public void slnExplUIHierarchyExample(DTE2 dte)
{
    UIHierarchy UIH = dte.ToolWindows.SolutionExplorer;
    // Requires a reference to System.Text.
    // Set a reference to the first level nodes in Solution Explorer. 
    // Automation collections are one-based.
    UIHierarchyItem UIHItem = UIH.UIHierarchyItems.Item(1);
    StringBuilder sb = new StringBuilder();

    // Iterate through first level nodes.
    foreach ( UIHierarchyItem fid in UIHItem.UIHierarchyItems )
    {
        sb.AppendLine(fid.Name);
        // Iterate through second level nodes (if they exist).
        foreach ( UIHierarchyItem subitem in fid.UIHierarchyItems )
        {
            sb.AppendLine("   "+subitem.Name);
            // Iterate through third level nodes (if they exist).
            foreach ( UIHierarchyItem subSubItem in 
              subitem.UIHierarchyItems )
            {
                sb.AppendLine("        "+subSubItem.Name);
            }
        }
    }
    System.Windows.Forms.MessageBox.Show(sb.ToString());
}

请参见

任务

如何:创建外接程序

演练:创建向导

概念

VSProject2 对象介绍

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考