Export (0) Print
Expand All

Adding a Toolbar to a Managed Tool Window

Visual Studio .NET 2003
 

Microsoft Corporation

February 2004

Applies to:
    Microsoft® Visual Studio® .NET 2003

Summary: Learn steps that add a toolbar to a managed tool window. (7 printed pages)

Contents

Introduction
To Create a Managed Tool Window
To add a Toolbar
To add a Combo Box to the Toolbar

Introduction

This article guides you through steps that add a toolbar to a managed tool window. You create a VSPackage using the Package Wizard, then add a toolbar, toolbar button, and combo box using managed code.

Creating a Managed Tool Window

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

    The Package Wizard runs.

  2. In the Select a Programming Language page, set the language to Visual C#.
  3. In the Select VSPackage Options page, check the Tool Window checkbox. Accept all other defaults.

    The wizard generates a managed project Tool and an unmanaged resource-only project ToolUI.

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

Adding a Toolbar

  1. Open the ToolUI\PkgCmd.ctc file and add this boldfaced line to the MENUS block:
    MENUS_BEGIN
    // NewMenu                   Relative to Group            Priority 
      Type              Name          Text
    guidToolCmdSet:cmdidToolbar, guidToolCmdSet:cmdidToolbar, 0x0000, 
      TOOLWINDOWTOOLBAR, "My Toolbar", "My Toolbar Text"; 
    MENUS_END
    
    
  2. Add this boldfaced line to the NEWGROUPS block:
    NEWGROUPS_BEGIN
    // NewGroup                 Parent Group                       Priority
    guidToolCmdSet:MyMenuGroup, guidSHLMainMenu:IDM_VS_MENU_TOOLS, 0x0600;
    guidToolCmdSet:MyToolbarGroup, guidToolCmdSet:cmdidToolbar, 0x0600;
    NEWGROUPS_END
    
    
  3. Add this boldfaced line to the BUTTONS block:
    BUTTONS_BEGIN
    // Command                  Parent Group
      Priority Image                  Type
    guidToolCmdSet:cmdidMyTool, guidSHLMainMenu:IDG_VS_WNDO_OTRWNDWS1, 
      0x0100, guidToolCmdSet:bmpPic2, BUTTON, , "My Tool Window";
    guidToolCmdSet:cmdidMyToolbarButton, guidToolCmdSet:MyToolbarGroup, 
      0x0100, guidToolCmdSet:bmpPicSmile, BUTTON, , "My Toolbar Button";
    BUTTONS_END
    
    
    Note   Creating a toolbar with no controls can cause a runtime error. If you want a toolbar with no buttons, you must explicitly change guidToolCmdSet:bmpPicSmile in the line above to guidOfficeIcon:msotcidNoIcon.
  4. Open the ToolUI\PkgCmdID.h file and add this boldfaced line to the Menu IDs:
    // Menu IDs
    #define cmdidToolbar 0x1021
    
    
  5. Add this boldfaced line to the Menu Group IDs:
    // Menu Group IDs
    #define MyMenuGroup 0x1020
    #define MyToolbarGroup 0x1022
    
    
  6. Add this boldfaced line to the Command IDs:
    // Command IDs
    #define cmdidMyTool 0x101
    #define cmdidMyToolbarButton 0x102
    
    
  7. Open the Tool\PkgCmdID.cs file and add these boldfaced lines to the PkgCmdIDList class:
    class PkgCmdIDList
    {
        public const uint cmdidMyTool = 0x101;
        public const uint cmdidMyToolbarButton = 0x102;
        public const uint MyToolbar = 0x1021;
    };
    
    
    Note    These new constants must match the constants defined in ToolUI\PkgCmdID.h.
  8. Open the Tool\My Tool Window.cs file and change the access level (modifiers property) of button1 to public:
    public System.Windows.Forms.Button button1;
    
    
  9. Open the Tool\VsPkg.cs file. Add this boldfaced line to the start of the Tool class:
    private IVsWindowFrame windowFrame;
    private MyToolWindow tool;
    
    
  10. Expand the Package Members region. In the CreateTool method, change the tool assignment statement by replacing
        MyToolWindow tool = new MyToolWindow();
    
    

    with

        tool = new MyToolWindow();
    
    

    Because the new tool window is assigned to the tool field, it is accessible to other Tool class methods.

  11. In the CreateTool method, add this boldfaced flag to the CreateToolWindow call:
    vsUiShell.CreateToolWindow(
        (uint)(ToolWindowFlags.CTW_fForceCreate | 
      ToolWindowFlags.CTW_fToolbarHost), // flags
    
    

    When CreateToolWindow is called, the shell creates a tool window that can host toolbars.

  12. In the CreateTool method, add these boldfaced lines just under the tool.Frame assignment statement:
    tool.Frame = windowFrame;
    object obj;
    windowFrame.GetProperty((int) __VSFPROPID.VSFPROPID_ToolbarHost, out 
      obj);
    IVsToolWindowToolbarHost tbh = (IVsToolWindowToolbarHost) obj;
    if (tbh != null)
    {
        Guid myCmdSet = GuidList.guidToolCmdSet;
        tbh.AddToolbar(VSTWT_LOCATION.VSTWT_TOP, ref myCmdSet,
          PkgCmdIDList.MyToolbar);
    }
    
    

    The toolbar is added to the toolbar host, which is obtained from the tool window frame.

  13. Expand the IOleCommandTarget Members region. In the QueryStatus method, add this boldfaced case to the prgCmds[0].cmdID switch statement:
    case PkgCmdIDList.cmdidMyTool:
    case PkgCmdIDList.cmdidMyToolbarButton:
    {
        // This command is always enabled
        cmdf = OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED;
        break;
    }
    
    

    This enables the Combo Box.

  14. In the Exec method, add this case statement to the switch (cmdID) statement:
    case PkgCmdIDList.cmdidMyToolbarButton:
    {
        tool.button1.Text = "Toolbar button clicked.";
        break;
    }
    
    

    When the toolbar button is clicked, the tool window button label is set to the given text.

  15. Build and start the Tool project in debug mode by pressing the keyboard shortcut, F5.

    This starts Visual Studio .NET Exp. Note that both versions of Visual Studio are open at this time.

  16. In Visual Studio .NET Exp, click the View menu, point to Other Windows and click My Tool Window.

    A tool window opens with a toolbar and a toolbar button that displays the smiley face icon.

  17. Click the smiley face button.

    The tool window button label changes to Toolbar button clicked.

    Note    Enlarge the tool window by dragging the lower right edge downward if you don't see the text.

Adding a Combo Box to the Toolbar

  1. Close Visual Studio .NET Exp. Open the ToolUI\PkgCmd.ctc file and add a COMBOS block between the BUTTONS and BITMAPS blocks:
    COMBOS_BEGIN
    guidToolCmdSet:cmdidMyToolbarCombo, guidToolCmdSet:MyToolbarGroup, 
      0x0010, cmdidMyToolbarPopulator, 100, DYNAMICCOMBO, DEFAULTDISABLED, 
      "Fruit (btn text)", , "Fruit (tt text)";
    COMBOS_END
    
    
  2. Open the ToolUI\PkgCmdID.h file and add these boldfaced lines to the Command IDs:
    // Command IDs
    #define cmdidMyTool 0x101
    #define cmdidMyToolbarButton 0x102
    #define cmdidMyToolbarCombo 0x103
    #define cmdidMyToolbarPopulator 0x104
    
    
  3. Open the Tool\PkgCmdID.cs file and add these boldfaced lines to the PkgCmdIDList class:
    class PkgCmdIDList
    {
        public const uint cmdidMyTool = 0x101;
        public const uint MyToolbar = 0x1021;
        public const uint cmdidMyToolbarButton = 0x102;
        public const uint cmdidMyToolbarCombo = 0x103;
        public const uint cmdidMyToolbarPopulator = 0x104;
    };
    
    
    Note    These new constants must match the constants defined in ToolUI\PkgCmdID.h.
  4. Open the Tool\VsPkg.cs file. Expand the IOleCommandTarget Members region. In the QueryStatus method, add this boldfaced case to the prgCmds[0].cmdID switch statement:
    case PkgCmdIDList.cmdidMyTool:
    case PkgCmdIDList.cmdidMyToolbarButton:
    case PkgCmdIDList.cmdidMyToolbarCombo:
    case PkgCmdIDList.cmdidMyToolbarPopulator:
    {
        // This command is always enabled
        cmdf = OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED;
        break;
    }
    
    

    This enables the combo box.

  5. In the Exec method, add these case statements to the switch (cmdID) statement:
    case PkgCmdIDList.cmdidMyToolbarCombo:
    {
        if (pvaIn != IntPtr.Zero)
        {
            string enteredString = (string) 
              Marshal.GetObjectForNativeVariant(pvaIn);
            tool.button1.Text = enteredString;
        }
        if (pvaOut != IntPtr.Zero)
        {
            Marshal.GetNativeVariantForObject(tool.button1.Text, pvaOut);
        }
        break;
    }
    case PkgCmdIDList.cmdidMyToolbarPopulator:
    {
        if (pvaOut != IntPtr.Zero)
        {
            string [] stringArray = new string [3] {"Apple", "Banana", 
              "Mango"};
            Marshal.GetNativeVariantForObject(stringArray, pvaOut);
        }
        break;
    }
    
    

    When the Combo Box first appears, it is populated with "Apple," "Banana," and "Mango." When the Combo Box edit field changes, the button label changes accordingly, and vice versa.

  6. Build and start the Tool project in debug mode by pressing the keyboard shortcut, F5.

    This starts Visual Studio .NET Exp. Note that both versions of Visual Studio are open at this time.

  7. In Visual Studio .NET Exp, click the View menu, point to Other Windows and click My Tool Window.

    A tool window opens with a toolbar and a toolbar button that displays both the Combo Box and the smiley face icon. The initial value of the Combo Box edit field is Click Me!

  8. Select Banana from the Combo Box drop-down list.

    Both the combo box edit field and button label change to Banana.

Show:
© 2014 Microsoft