Share via


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

Visual Studio 2013 中已弃用 Visual Studio 外接程序。 你应该将外接程序升级到 VSPackage 扩展。 有关升级的详细信息,请参阅常见问题:将外接程序转换为 VSPackage 扩展

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

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

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

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

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

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

  • 将项目编译到 DLL 中。

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

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

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

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

备注

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

备注

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于你现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入”“导出”“设置”。有关详细信息,请参阅在 Visual Studio 中自定义开发设置

创建自定义工具窗口

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

备注

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

创建自定义工具窗口

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

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

备注

你的用户控件类必须有一个附加到类定义的 GuidAttribute

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

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

  2. 用以下内容替换外接程序的 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 2013\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 toolWin 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 2013\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 (toolWin != null)
            {
                toolWin.Visible = true;
            }
            // Set the new tool window's height and width, 
            // and then 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);
        }
    }
    

    注意   上述代码需要一个对 System.Windows.Forms 命名空间的引用。

  3. 更改 ctlProgID、asmPath 和 guidStr 变量的值以反映你的用户控件。

  4. 生成并运行该项目。

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

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

请参见

任务

如何:更改窗口特性

如何:创建外接程序

演练:创建向导

概念

控制选项设置

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考