Walkthrough: Adding a Command to an Editor Generated by the Package Wizard

The Visual Studio Integration Package Wizard can be used to generate an embedded editor that inherits from the RichTextBox class. The generated editor can be used either as a starting point for other editor applications or as a model for a similar embedded editor. This walkthrough guides you through the steps of adding your own command to the editor that the wizard generates.

注意

Beginning with Visual Studio 2008 SDK, use XML Command Table (.vsct) files instead of command table configuration (.ctc) files to define how menus and commands appear in your VSPackages. For more information, see XML-Based Command Table Configuration (.vsct) Files.

Prerequisites

To run and build this walkthrough, you must have Visual Studio 2008 and the Visual Studio SDK installed.

To create a VSPackage

  1. Create a new VSPackage using the Visual Studio 2008 Integration Package Wizard. Name the project TestEditor and click OK to start the wizard.

    For more information, see How to: Create VSPackages (C# and VB).

  2. In the Select a Programming Language page, select C#, and select the options to have a key file generated for you.

  3. In the Basic VSPackage Information page, accept the default settings, and click Next.

  4. In the Select VSPackageOptions page, select the Menu Command and Custom Editor options to create an editor VSPackage with an embedded editor and a menu command.

    This walkthrough does not utilize the menu command, but selecting it in the wizard generates a .ctc file. It is easier to modify an existing .ctc file than to create one yourself.

  5. In the Command Options page, accept the default values.

  6. In the Editor Options page, type My Test Editor in the Editor Name field, mytestext in the File Extension field, and MytestExtFile in the Default File Name field.

  7. Click Finish to generate your VSPackage.

    The wizard generates a project named TestEditor.

  8. Build the solution and verify that it built with no errors.

Adding the Command Code to the Generated Editor

The Visual Studio Integration Package Wizard creates a sample embedded editor that adds commands from the standard VSConstants.VSStd97CmdID command set. In the following section you add the necessary code to create your own command.

To add the command code

  1. To add the command id, in PkgCmdID.cs add the following line to the body of the PkgCmdIDList class:

    namespace Vsip.TestEditor
    {
        class PkgCmdIDList
        {
             public const uint cmdidMyCommand =        0x100;
             public const uint cmdidColorFont = 0x102; 
        };
    }
    
  2. In PkgCmdID.h, located in the CtcComponents node, add the following line after #define icmdFontSizeHandler 0x001d:

    // Font Commands
    #define icmdFontName                    0x001a
    #define icmdFontNameHandler                0x001b
    #define icmdFontSize                    0x001c
    #define icmdFontSizeHandler                0x001d
    
    #define cmdidMyCommand 0x100
    #define cmdidColorFont 0x102 
    
  3. In EditorPane.cs in the #region IOleCommandTarget Members, add the following lines to the QueryStatus method after the switch (prgCmds[0].cmdID) statement:

    if (guidCmdGroup == GuidList.guidTestEditorCmdSet)
       {
         // Process our Commands.
         switch (prgCmds[0].cmdID)
          {         case PkgCmdIDList.cmdidColorFont: 
                { 
                  // Enable if something is selected.
                if (textBox1.SelectionLength > 0) 
                    cmdf |= OLECMDF.OLECMDF_ENABLED; 
                        break; 
                }
    default:
    return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
                    }
                }
                else
                    return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);;
                prgCmds[0].cmdf = (uint)cmdf;
                return NativeMethods.S_OK;
            }
    
  4. In the same section of EditorPane.cs (in the #region IOleCommandTarget Members), add the following lines to the Exec method after the switch (prgCmds[0].cmdID) statement:

    else if (guidCmdGroup == GuidList.guidTestEditorCmdSet)
    {
            // Process our Commands
            switch (prgCmds[0].cmdID)
            {
                // If we had commands specific to our editor, they would 
                // be processed here.
                case PkgCmdIDList.cmdidColorFont:                textBox1.SelectionColor = Color.Crimson;                break;
                default:
                    return (int)(Microsoft.VisualStudio.OLE.Interop.
                         Constants.OLECMDERR_E_NOTSUPPORTED);
                    }
                }
    
  5. To define MyMenuGroup as a new menu group, to set its members, and to set the guidEditPack to be displayed on the Edit menu, add the following lines to TestEditor.ctc between the NEWGROUPS_BEGIN body and NEWGROUPS_END statements:

    guidTestEditorCmdSet:MyMenuGroup, guidSHLMainMenu:IDM_VS_MENU_EDIT, 0x0600; 
    
  6. To define the cmdidColorFont command to be in MyMenuGroup, to set its visibility, to set its title, and to associate the "Smile" icon with it, add the following lines to PkgCmd.ctc in the body of the BUTTONS_BEGIN section and before the BUTTONS_END section:

    BUTTONS_BEGIN
        // Command                    Parent Group                Priority    Image            Type    Visibility        
    
    // Those commands are not defined as shared commands, so they use
    // our package CLSID as the command set GUID. Also, by
    // specifying blank for the FLAGS, this command is visible by
    // default and enabled. Other valid values for FLAGS are the following:
    //DEFAULTDISABLED, DEFAULTINVISIBLE, DYNAMICVISIBILITY, TEXTCHANGES
    // These values for FLAGS can be or'ed together, for example, "DEFAULTINVISIBLE | DYNAMICVISIBILITY"
    // If you do not want an image next to your command, set the image to "guidOfficeIcon:msotcidNoIcon"
    guidTestEditorCmdSet:cmdidColorFont,  guidTestEditorCmdSet:MyMenuGroup,   0x0100, guidTestEditorCmdSet:bmpPicSmile, BUTTON, DYNAMICVISIBILITY | DEFAULTINVISIBLE | DEFAULTDISABLED,  "Color Text"; 
    guidTestEditorCmdSet:cmdidMyCommand,  guidSHLMainMenu:IDM_VS_MENU_TOOLS,  0x0100, guidTestEditorCmdSet:bmpPic1,  BUTTON,  ,  "My Command Name";
    

    This sets the cmdidColorFont to be in the MyMenuGroup command group, where the MyMenuGroup is automatically generated by the Visual Studio Integration Package Wizard.

    The "Smile" icon, bmpPicSmile bitmap, is generated as part of the Visual Studio Integration Package Wizard code. The visibility flags ensure that the Color Text command is displayed in the Edit menu only when your VSPackage is loaded in the experimental version of Visual Studio.

  7. Click the Save All button, close all open code windows, and then click Build Solution on the Build menu to ensure the application builds correctly.

To start an instance of Visual Studio Exp

  • At the Visual Studio command prompt, type Devenv /rootsuffix exp.

To test your editor command

  1. On the File menu of Visual Studio Exp, point to New and then click File. In the Categories pane of the New File dialog box, click My Test Editor.

  2. In the Templates pane, click My Test Editor.

  3. Click Open.

  4. Expand the Edit menu.

    Only Select All is enabled.

  5. Type text in the editor pane and select some of it.

    The commands, Copy, Paste, Cut, and Color Text are enabled.

  6. Click Color Text to color the selected text crimson.

Next Steps

Another walkthrough adds a keyboard binding to the ColorText command. For more information, see Walkthrough: Adding a Keyboard Binding to an Editor Command.

See Also

Concepts

Editor Walkthroughs