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

╄→尐↘猪︶ㄣ 提交于 2020-01-17 04:15:15

问题


I'm using Visual Studio 2013.

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

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?.


回答1:


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?");
}


来源:https://stackoverflow.com/questions/31160915/vs-2013-sdk-how-to-develop-an-extension-accesible-through-the-editors-contextm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!