我可以以编程方式修改IDE上下文菜单吗?

时间:2011-08-18 13:28:50

标签: visual-studio-2010 ide vb.net-2010 visual-studio-macros

我正在尝试为“项目和解决方案上下文菜单>解决方案”菜单创建一个额外的菜单项。但是,我希望此上下文菜单仅在打开某个解决方案时出现,否则我不希望它显示。我想我可以在Visual Studio IDE中使用MACROS。解决方案开放时会有一些活动。项目等。

我的问题是,我可以以编程方式创建我想要的上下文菜单,将其指向我希望它运行的MACRO,然后以编程方式将其附加到IDE中我想要显示它的CONTEXT菜单中吗?

3 个答案:

答案 0 :(得分:2)

我发现this link 非常非常有用。

在OnConnection方法中,我有这个:

CommandBars cmdBars = (CommandBars)applicationObject.CommandBars;
        CommandBar codeWindowCmdBar = cmdBars["Code Window"];

        CommandBarPopup doxygenPopup = (CommandBarPopup)codeWindowCmdBar.Controls.Add(MsoControlType.msoControlPopup,
                                    System.Reflection.Missing.Value,
                                    System.Reflection.Missing.Value, 
                                    1, 
                                    true);
        doxygenPopup.Caption = "Doxygen Commenting";


        CommandBarControl mnuListItem = (CommandBarControl)doxygenPopup.Controls.Add(MsoControlType.msoControlButton,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value, 
            1, 
            true);

        mnuListItem.Caption = "Wrap as List Item";
        mnuListItemHandler = (CommandBarEvents) applicationObject.Events.get_CommandBarEvents(mnuListItem);
        mnuListItemHandler.Click += mnuListItemHandler_Click;

        CommandBarControl mnuUnorderedList = (CommandBarControl)doxygenPopup.Controls.Add(MsoControlType.msoControlButton,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            2,
            true);

        mnuUnorderedList.Caption = "Wrap as Unordered List";
        mnuUnorderedListHandler = (CommandBarEvents)applicationObject.Events.get_CommandBarEvents(mnuListItem);
        mnuUnorderedListHandler.Click += mnuUnorderedListHandler_Click;

        CommandBarControl mnuOrderedList = (CommandBarControl)doxygenPopup.Controls.Add(MsoControlType.msoControlButton,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            2,
            true);

        mnuOrderedList.Caption = "Wrap as Ordered List";
        mnuOrderedListHandler = (CommandBarEvents)applicationObject.Events.get_CommandBarEvents(mnuListItem);
        mnuOrderedListHandler.Click += mnuOrderedListHandler_Click;

在click事件处理程序中,我做了所需的一切。

请注意,这是针对包含子项目的主菜单项。对于单个项目,请查看this video。这家伙一步一步地走了......做得很好。

答案 1 :(得分:0)

您可以使用DTE.ExecuteCommand()执行其他宏。

答案 2 :(得分:0)

我找到了问题的答案。我使用以下代码创建了我需要的菜单项:

Private WithEvents _objSolutionExplorer_ContextMenu_ItemHandler As EnvDTE.CommandBarEvents
Private _objHash_ContextMenuItemHandlers As New Hashtable

Private Function CreateContextMenuButtonItem(ByVal p_objMenuItem As Object, ByVal p_strMenuItemCaption As String) As CommandBarButton
    '************************************************************************
    ' Procedure/Function: CreateContextMenuButtonItem()
    ' Author: Ben Santiago
    ' Created On: 08/18/2011
    ' Description:
    '       Creates specified MenuButton item.
    '************************************************************************

    '***************************************
    ' Initialize Variables
    '***************************************
    Dim objMenuButton As CommandBarButton
    Dim objClickEventHandler As EnvDTE.CommandBarEvents

    '***************************************
    ' If MenuItem Exists, Delete It
    '***************************************
    For intCounter As Integer = 1 To p_objMenuItem.Controls.Count
        If p_objMenuItem.Controls.Item(intCounter).Caption = p_strMenuItemCaption Then
            p_objMenuItem.Controls.Item(intCounter).Delete()
            Exit For
        End If
    Next
    If EnvironmentEvents._objHash_ContextMenuItemHandlers.Contains(p_strMenuItemCaption) Then
        EnvironmentEvents._objHash_ContextMenuItemHandlers.Remove(p_strMenuItemCaption)
    End If

    '***************************************
    ' Create New Menu Item w/ Click Event Handling
    '***************************************
    objNewMenuItem = p_objMenuItem.Controls.Add(MsoControlType.msoControlButton, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, True)
    objNewMenuItem.Caption = p_strMenuItemCaption
    objClickEventHandler = DTE.Events.CommandBarEvents(objNewMenuItem)
    AddHandler objClickEventHandler.Click, AddressOf TestScoring_objSolutionExplorer_ContextMenu_ItemHandler_Click
    If EnvironmentEvents._objHash_ContextMenuItemHandlers.Contains(p_strMenuItemCaption) Then
        EnvironmentEvents._objHash_ContextMenuItemHandlers.Remove(p_strMenuItemCaption)
    End If
    EnvironmentEvents._objHash_ContextMenuItemHandlers.Add(p_strMenuItemCaption, objClickEventHandler)

    '***************************************
    ' Return Reference To MenuButton Item
    '***************************************
    Return objNewMenuItem
End Function

Private Function CreateContextMenuPopupItem(ByVal p_strCommandBarName As String, ByVal p_strMenuCaption As String) As CommandBarPopup
    '************************************************************************
    ' Procedure/Function: CreateContextMenuPopupItem()
    ' Author: Ben Santiago
    ' Created On: 08/18/2011
    ' Description:
    '       Creates specified MenuPopup item.
    '************************************************************************

    '***************************************
    ' Initialize Variables
    '***************************************
    Dim objCommandBar As CommandBar = DTE.CommandBars(p_strCommandBarName)
    Dim objMenuPopup As CommandBarPopup

    '***************************************
    ' Search For Existing ContextMenuPopup
    '***************************************
    For intCounter As Integer = 1 To objCommandBar.Controls.Count
        If objCommandBar.Controls.Item(intCounter).Caption = p_strMenuCaption Then
            objMenuPopup = objCommandBar.Controls.Item(intCounter)
            Exit For
        End If
    Next

    If objMenuPopup Is Nothing Then
        '***************************************
        ' Create Menu If Needed
        '***************************************
        objMenuPopup = objCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, True)
        objMenuPopup.Caption = p_strMenuCaption
    Else
        '***************************************
        ' Delete All Child Menu Items
        '***************************************
        For intCounter As Integer = objMenuPopup.Controls.Count To 1 Step -1
            If EnvironmentEvents._objHash_ContextMenuItemHandlers.Contains(objMenuPopup.Controls.Item(intCounter).Caption) Then
                EnvironmentEvents._objHash_ContextMenuItemHandlers.Remove(objMenuPopup.Controls.Item(intCounter).Caption)
            End If
            objMenuPopup.Controls.Item(intCounter).Delete()
        Next
    End If

    '***************************************
    ' Return Reference To MenuPopup Item
    '***************************************
    Return objMenuPopup
End Function