Search code examples
c#.netvb.netvisual-studiovisual-studio-extensions

VS 2013 SDK: How to develop an extension accesible through the editor's contextmenu?


I'm using Visual Studio 2013.

The project wizard of a Visual Studio Package project gives these three options:

enter image description here

However, what I pretend to do is create a very simple extension (which modifies the selected text) and it should be accesible through a command from the contextmenu of the text-editor, not a menu command neither a tool window and neither a custom editor (...I think).

Firstly, what is the project that I should select for my needs? (I'm using a menu command).

Secondly, what are the necessary steps to modify the behavior of the extension to add it only in the contextmenu of the text editor?.


Solution

  • private void CreateContextMenu()
    {
        // Get a reference to the context menu of code windows.
        CommandBars commandBars = (CommandBars)applicationObject.CommandBars;
        CommandBar codeWindowCommandBar = commandBars["Code Window"];
    
        // Add a popup command bar.
        CommandBarControl myCommandBarControl = codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
    
        CommandBarPopup myPopup = (CommandBarPopup)myCommandBarControl;
    
        // Change its caption
        myPopup.Caption = "My popup";
    
        // Add controls to the popup command bar
        CommandBarButton myButton = (CommandBarButton)myPopup.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, 1, true);
        myButton.Caption = "Click Me";
        myButton.Click += myButton_Click;
    }
    
    void myButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
    {
        MessageBox.Show("What's up?");
    }