Condividi tramite


How to: Use an Add-in to Display Information About a Help Topic

A tool window hosted by a Visual Studio add-in can be connected to the Help2 object in order to interact with Document Explorer (dexplorer.exe), which is the program that displays Help topics in Visual Studio.

The following procedure explains how to create a tool window in a Visual Studio add-in and add buttons to it to display Help content and information about the Help.

To create a tool window

  1. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  2. In the Project types pane, click Visual C#, and then click Windows Control Library in the Templates pane.

  3. In the Name box, type AddinHelpToolWin.

  4. In the Location box, type a path or accept the default folder, <drive>:\Documents and Settings\<User Name>\My Documents\Visual Studio Projects, and then click OK.

  5. On the View menu, click Toolbox.

  6. In the Toolbox, expand Common Controls.

  7. Drag two Button items to the UserControl1.cs[Design] form. Arrange them side-by-side on the bottom of the user control. Resize the user control if necessary to fit the button control items.

  8. Drag a TextBox control item to the UserControl1.cs[Design] form and place it above the two buttons.

  9. Select button1 and click Properties Window on the View menu.

  10. In the Properties window, find the Text option within the Appearance group and type HelpInfo to replace the button1 text.

  11. Similarly, for button2, replace the button2 text with Help.

  12. Select the text box on the UserControl1.cs[Design] form .

  13. In the Properties window, set the Multiline behavior to True within the Behavior group, by selecting True from the drop-down menu.

  14. Resize the text box so that it fills most of the user control above the two buttons.

  15. In Solution Explorer, click the AddinHelpToolWin project.

  16. On the Project menu, click Add Reference.

  17. In the Add Reference dialog box, click the COM tab, click Microsoft Development Environment 8.0 in the Component Name list, and then click OK.

  18. On the Project menu, click Add Reference.

  19. In the Add Reference dialog box, click the NET tab, shift-click Microsoft.VisualStudio.VSHelp and Microsoft.VisualStudio.VSHelp80 in the Component Name list, and then click OK.

  20. On the View menu, click Designer if the user control is not already in Design view.

  21. Double-click the Help Info button and replace the code that appears in the UserControl1.cs file with the code below:

    private void button1_Click(object sender, EventArgs e)
            {
    // Declare variables.
        EnvDTE.DTE dte;
        Microsoft.VisualStudio.VSHelp80.Help2 help2;
        // Get the active Visual Studio DTE object.
        dte = 
    (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.
    GetActiveObject("VisualStudio.DTE.8.0");
        // Get the Help2 object.
        help2 = (Microsoft.VisualStudio.VSHelp80.Help2)dte.GetObject("Help2");
        // Display basic property information about the Help collection.
        textBox1.Text=("The name of the currently active help collection
     is: " + help2.Collection + ".  The Help object is: "+
     help2.Help.ToString() + ". The Help owner is: " +
     help2.HelpOwner.ToString() + " And " + " The HxSession is: " +
     help2.HxSession.ToString());
    }
    
  22. On the View menu, click Designer.

  23. Double-click the Help button and replace the code that appears in the UserControl1.cs file with the code below:

    private void button2_Click(object sender, EventArgs e)
    {
        // Pass in the F1 keyword for a topic in the     // MS.VSIPCC.v80 Help collection.    DisplayHelp("Owner");
    
    }
    
  24. Add the DisplayHelp function right under the UserControl1 constructor code.

    public UserControl1()
    {
        InitializeComponent();
    }
    public void DisplayHelp(String F1)
    {
        EnvDTE.DTE dte;
    
        Microsoft.VisualStudio.VSHelp80.Help2 help2;
        // Get the active Visual Studio DTE object.
        dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal
    .GetActiveObject("VisualStudio.DTE.8.0");
        help2 = (Microsoft.VisualStudio.VSHelp80.Help2)dte.GetObject("Help2");
        help2.SetCollection("ms-help://MS.VSCC.v80/MS.VSIPCC.v80", "");
        help2.DisplayTopicFromF1Keyword(F1);
    }
    
  25. On the File menu, click Save UserControl1.cs.

  26. On the Build menu, click Build Solution.

    You have now created a custom tool window that will be hosted by the Visual Studio add-in that you create in the next procedure.

  27. On the File menu, click Close Solution.

To create an add-in project that hosts the tool window

  1. On the File menu, point to New, and then click Project.

  2. In the Project types pane, expand Other project types, click Extensibility, click Visual Studio Add-in, and then click OK.

    The Visual Studio Add-in Wizard starts.

  3. Accept the default options in each page of the wizard.

    For information, see How to: Create an Add-In.

  4. On the Project menu, click Add Reference.

  5. In the Add Reference dialog box, click the NET tab, click System.Windows.Forms in the Component Name list, and then click OK.

  6. Add the following using statement to the top of the Connect.cs file:

    using System.Windows.Forms;
    
  7. Replace the OnConnection method of the add-in with the following:

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        ToolwinFunc(_applicationObject);
    }
    void ToolwinFunc(DTE2 dte)
    {
        try
        {
            // Variables for the new tool window that will hold
            // your user control.
            EnvDTE80.Windows2 toolWins;
            EnvDTE.Window toolWin;
            object objTemp = null;
    
            // 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 = "AddinHelpToolWin.UserControl1";
            // Replace the <Installation path> with the path to
            // the folder where you created the WindowsCotrolLibrary.
            // Remove the line returns from the path before 
            // running the add-in.
            // Replace the text <Installation path> with the actual path
            // on your computer.
            string asmPath = "<Installation path>\\AddinHelpToolWin\\
    AddinHelpToolWin\\bin\\Debug\\AddinHelpToolWin.dll";
            // The following GUID must be unique for each tool window.
            // Create a new one, or modify this one slightly to avoid
            // GUID collisions.
            string guidStr = "{69B7BB98-7FDD-4aa5-8ADB-96653EAE978B}";
    
            toolWins = (EnvDTE80.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 can do 
            // anything with it.
            if (toolWins == null)
            {
                toolWin.Visible = true;
            }
            toolWin.Activate();
            MessageBox.Show
             ("Setting the height to 500 and width to 400...");
            toolWin.Height = 500;
            toolWin.Width = 400;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception: " + ex);
        }
    }
    
  8. In the code above, replace the placeholder text, <Installation path>, with the path to the user control on your computer.

  9. Build and run the project.

  10. On the Tools menu, click Add-In Manager to activate the add-in.

    You see your new tool window floating in the Visual Studio integrated development environment (IDE). You can move it or dock it with other tool windows.

  11. In the tool window, click the Help Info button to see information about the Help collection displayed in the tool window text box.

  12. In the tool window, click the Help button to start the Microsoft Document Explorer (dexplorer.exe) in which the topic identified by the F1-keyword is identified.

    The Owner Property displays in the Help viewer.

See Also

Concepts

How to: Create and Control Tool Windows

Creating Add-ins and Wizards

Programmatically Interacting with Document Explorer

Microsoft Document Explorer Overview

Reference

GetActiveObject

Windows2

CreateToolWindow2