如何:创建和控制工具窗口

更新:2007 年 11 月

Visual Studio 中的窗口分为两类:文档窗口和工具窗口。文档窗口的内容(如文本文件、HTML 或者类中的代码)可以由代码编辑器进行编辑; 而工具窗口则包含一个或多个控件,如按钮、文本、组合框等。Visual Studio 集成开发环境 (IDE) 使用控件执行任务,如设置选项、查看错误或编辑项目元素。这些控件的示例包括“输出”窗口、“任务列表”和“工具箱”。“工具箱”可以在 IDE 周围自由移动,或者与其他工具窗口停靠在一起,您可以使用 LinkedWindows 集合以编程方式链接或取消链接 IDE 中的工具窗口。有关更多信息,请参见如何:更改窗口特性

除了使用自动化功能操作现有的工具窗口外,还可以使用 Windows2 集合的 CreateToolWindow2 方法创建自己的自定义工具窗口。

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

这种用于 Visual Studio 2005 的新方法取代了 Visual Studio 2002 和 Visual Studio 2003 中使用的 CreateToolWindow 方法。

通过创建自己的自定义工具窗口,可以使用有用的控件填充它以执行任务。例如,您可以使用自定义工具窗口显示专用工具,从而帮助您设置代码格式,跟踪和更改变量设置,或者执行高级调试任务或来源分析。

创建自定义工具窗口的过程为:

  • 创建一个用户控件(使用 Windows 控件库项目)

  • 在窗体上添加所需的控件(按钮、文本框等)和代码。

  • 将项目编译到 DLL 中

  • 创建一个新的 Visual Studio 外接程序项目(或其他项目,如 Windows 应用程序项目)。

  • 使用 CreateToolWindow2 方法创建一个工具窗口来承载新用户控件。

在调用 CreateToolWindow2 创建一个工具窗口之前,您应将用户控件 (ControlObject) 移动到与外接程序的程序集中,或设置用户控件上的所有属性,以使其对 COM 是完全可见的。(例如,在项目的编译选项中选中“为 COM 互操作注册”选项。) 如果您不执行此操作,则控件不会正确封送,而且 CreateToolWindow2 将返回一个空值。

除了以下示例,还可以在“Automation Samples for Visual Studio”(Visual Studio 自动化示例)网站上找到每种语言的附加工具窗口示例以及其他代码示例。

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

如果在新工具窗口可见之前尝试设置该工具窗口的任何可见状态(如高度、宽度或位置),则会发生错误。因此,在试图设置此类属性之前,请确保窗口是可见的。

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

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

创建自定义工具窗口

下面的示例演示如何在 Visual Basic 和 Visual C# 中创建工具窗口。

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

下面的代码必须在外接程序中执行;它不能在宏中执行。

创建自定义工具窗口

  1. 在 Windows 控件库项目中创建一个用户控件。接受默认名称“WindowsControlLibrary1”,或者确保更改了下面代码中 asmPath 参数的名称,使其与 Windows 控件库项目的名称匹配。

    或者,您可以在代码中引用现有的用户控件。

  2. 创建一个新的外接程序项目。

    有关信息,请参见如何:创建外接程序

  3. 用以下内容替换外接程序的 OnConnection 方法:

    Public Sub OnConnection(ByVal application As Object, ByVal _
    connectMode As ext_ConnectMode, ByVal addInInst As Object, _
    ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        Try
            ' ctlProgID - the ProgID for your user control.
            ' asmPath - the path to your user control DLL.
            ' guidStr - a unique GUID for the user control.
            Dim ctlProgID, asmPath, guidStr As String
            ' Variables for the new tool window that will hold
            ' your user control.
            Dim toolWins As EnvDTE80.Windows2
            Dim toolWin As EnvDTE.Window
            Dim objTemp As Object = Nothing
    
            _applicationObject = CType(application, DTE2)
            _addInInstance = CType(addInInst, AddIn)
            ctlProgID = "WindowsControlLibrary2.UserControl1"
            ' Replace the <Path to VS Project> with the path to
            ' the folder where you created the WindowsCotrolLibrary.
            ' Remove the line returns from the path before 
            ' running the add-in.
            asmPath = "<Path to VS Project>\My _
              Documents\Visual Studio 2005\Projects\ _
              WindowsControlLibrary2\WindowsControlLibrary2\_
              bin\Debug\WindowsControlLibrary2.dll"
            guidStr = "{E9C60F2B-F01B-4e3e-A551-C09C62E5F584}"
    
            toolWins = CType(_applicationObject.Windows, Windows2)
            ' Create the new tool window, adding your user control.
            toolWin = toolWins.CreateToolWindow2(_addInInstance, _
              asmPath, ctlProgID, "MyNewToolwindow", guidStr, objTemp)
            ' The tool window must be visible before you do anything 
            ' with it, or you will get an error.
            If Not toolWins Is Nothing Then
                toolWin.Visible = True
            End If
               ' Uncomment the code below to set the new tool window's
               ' height and width, and to close it.
            ' MsgBox("Setting the height to 500 and width to 400...")
            ' toolWin.Height = 500
            ' toolWin.Width = 400
            ' MsgBox("Closing the tool window...")
            ' toolWin.Close(vsSaveChanges.vsSaveChangesNo)
    
        Catch ex As Exception
            MsgBox("Exception: " & ex.ToString)
        End Try
    End Sub
    
    // Before running, add a reference to System.Windows.Forms, 
    // using System.Windows.Forms, to the top of the class.
    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        try
        {
            // ctlProgID - the ProgID for your user control.
            // asmPath - the path to your user control DLL.
            // guidStr - a unique GUID for the user control.
            string ctlProgID, asmPath, guidStr;
            // Variables for the new tool window that will hold
            // your user control.
            EnvDTE80.Windows2 toolWins;
            EnvDTE.Window toolWin;
            object objTemp = null;
    
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            ctlProgID = "WindowsControlLibrary2.UserControl1";
            // Replace the <Path to VS Project> with the path to
            // the folder where you created the WindowsCotrolLibrary.
            // Remove the line returns from the path before 
            // running the add-in.
            asmPath = @"c:\My Documents\Visual Studio 2005\Projects\
              WindowsControlLibrary2\WindowsControlLibrary2\bin\
              Debug\WindowsControlLibrary2.dll";
            guidStr = "{E9C60F2B-F01B-4e3e-A551-C09C62E5F584}";
    
            toolWins = (Windows2)_applicationObject.Windows;
            // Create the new tool window, adding your user control.
            toolWin = toolWins.CreateToolWindow2(_addInInstance, 
              asmPath, ctlProgID, "MyNewToolwindow", guidStr, 
              ref objTemp);
            // The tool window must be visible before you do anything 
            // with it, or you will get an error.
            if (toolWins == null)
            {
                toolWin.Visible = true;
            }
            // Uncomment the code below to set the new tool window's
            // height and width, and to close it.
            // System.Windows.Forms.MessageBox.Show("Setting the height 
            // to 500 and width to 400...");
            // toolWin.Height = 500;
            // toolWin.Width = 400;
            // System.Windows.Forms.MessageBox.Show
            //   ("Closing the tool window...");
            // toolWin.Close(vsSaveChanges.vsSaveChangesNo);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Exception: " 
              + ex.Message);
        }
    }
    
  4. 生成并运行项目。

  5. 在“工具”菜单上单击“外接程序管理器”,以激活外接程序。

您将看到新工具窗口浮在 IDE 中。您可以将它移动到任何位置,或者将它与其他工具窗口停靠在一起。

请参见

任务

如何:更改窗口特性

如何:创建外接程序

演练:创建向导

概念

控制选项设置

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考