次の方法で共有


Walkthrough: Extending Managed VSPackages with Automation (C#)

This walkthrough illustrates how to use automation to create a managed VSPackage that manipulates the Visual Studio integrated development environment (IDE). By running the VSPackage Wizard, you create a sample managed VSPackage and then use automation methods in the resulting VSPackage to display Visual Studio properties in the Output window.

To create a managed VSPackage

  1. Create a new Visual Studio Integration Package project named Auto.

    The Visual Studio Package Wizard runs. For more information about how to create a managed VSPackage, see How to: Create VSPackages (C# and VB).

  2. On the Select a Programming Language page, set the language to Visual C#.

  3. Keep the default values in the Basic VSPackage Information page.

  4. On the Select VSPackage Options page, select the Menu Command check box.

  5. On the Command Options page, change the Command Name to Auto.

  6. Click the Finish button.

    The wizard generates a managed project named Auto.

  7. Build the solution and verify that it compiles without errors.

To call the Automation model

  1. In the Solution Explorer window, right-click the Auto project node and then click Add Reference.

  2. On the .NET tab of the Add Reference dialog box, double-click EnvDTE.

    This adds a reference to the EnvDTE automation namespace.

  3. In the VSPkg.cs file, add using EnvDTE; to the top of the file.

  4. In the VSPkg.cs file, replace the body of the MenuItemCallback method with the following lines:

    Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)
        Dim dte As DTE    dte = CType(GetService(GetType(DTE)), DTE)    Dim myString As String = "Name is " & dte.Name + Constants.vbCr & "Version is " & dte.Version    Dim windows As Windows = dte.Windows    Dim window As Window = CType(windows.Item(EnvDTE.Constants.vsWindowKindOutput), Window)    window.Visible = True    Dim outputWindow As OutputWindow = CType(window.Object, OutputWindow)    Dim outputWindowPane As OutputWindowPane = outputWindow.OutputWindowPanes.Add("Test")    outputWindowPane.Activate()    outputWindowPane.OutputString(myString)End Sub
    
    private void MenuItemCallback(object sender, EventArgs e)
    {    DTE dte;    dte = (DTE) GetService(typeof(DTE));    string myString =       "Name is " + dte.Name + "\rVersion is " + dte.Version;    Windows windows = dte.Windows;    Window window =         (Window) windows.Item(EnvDTE.Constants.vsWindowKindOutput);    window.Visible = true;    OutputWindow outputWindow = (OutputWindow) window.Object;    OutputWindowPane outputWindowPane =               outputWindow.OutputWindowPanes.Add("Test");    outputWindowPane.Activate();    outputWindowPane.OutputString(myString);
    }
    

    This code calls GetService to obtain a DTE automation object that represents the Visual Studio IDE. The automation code in MenuItemCallback creates a new pane in the Output window named Test. The Visual Studio name and version is then written to the new Output pane.

  5. Build and start the Auto project in debug mode by pressing F5.

    This starts the Visual Studio experimental build (Visual Studio Exp).

    注意

    Both versions of Visual Studio are open at this point.

  6. In Visual Studio Exp, on the Tools menu, click Auto.

    A new pane named Test opens in the Output window and displays the following:

    Name is Microsoft Visual Studio
    Version is x.xx
    

    Where x.xx is the latest Visual Studio version number.

    For more information about automation samples, see Automation Samples for Visual Studio.

See Also

Concepts

Extending the Visual Studio Environment