如何:控制“命令”窗口

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

CommandWindow 对象表示 Visual Studio 自动化模型中的**“命令”**窗口。 您可以使用 CommandWindow 对象来:

  • 将命令插入到**“命令”**窗口并选择使用 SendInput 方法执行该命令。

  • 使用 OutputString 方法将信息性文本插入到**“命令”**窗口。

  • 使用 Clear 方法清除**“命令”**窗口中的所有文本。

除了控制**“命令”**窗口的内容以外,还可以控制其特性(如宽度和高度)。 有关详细信息,请参阅如何:更改窗口特性

备注

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

示例

本示例演示如何引用和使用**“命令”**窗口自动化模型的各种成员。 有关如何运行本示例的更多信息,请参见 如何:编译和运行自动化对象模型代码示例

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.
    CommandWinExample(_applicationObject)
End Sub

Sub CommandWinExample(ByVal dte As DTE2)
    Try
        ' Get a reference to the Command window.
        Dim CW As CommandWindow = dte.ToolWindows.CommandWindow

        ' Insert informative text into the Command window.
        CW.OutputString("This takes you to the Microsoft Web site.")

        ' Add a command to the Command window and execute it.
        CW.SendInput("nav https://www.microsoft.com", True)

        ' Clear the contents of the Command window.
        MsgBox("Clearing the Command window...")
        CW.Clear()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
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.
    CommandWinExample(_applicationObject);
}

public void CommandWinExample(DTE2 dte)
{
    try
    {
        // Get a reference to the Command window.
        CommandWindow CW = dte.ToolWindows.CommandWindow;

        // Insert informative text into the Command window.
        CW.OutputString("This takes you to the Microsoft Web site.");

        // Add a command to the Command window and execute it.
        CW.SendInput("nav https://www.microsoft.com", true);

        // Clear the contents of the Command window.
        System.Windows.Forms.MessageBox.Show(
          "Clearing the Command window...");
        CW.Clear();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

请参见

任务

如何:更改窗口特性

如何:创建外接程序

演练:创建向导

概念

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考