我正在尝试为“项目和解决方案上下文菜单>解决方案”菜单创建一个附加菜单项。但是,我希望此上下文菜单仅在打开某个解决方案时出现,否则我不希望它显示。我想我可以在 Visual Studio IDE 中使用 MACROS。在解决方案和项目的开放等情况下会举办一些活动。
我的问题是,我能否以编程方式创建我想要的上下文菜单,将其指向我希望它运行的宏,然后以编程方式将其附加到我想要在其中显示它的 IDE 内的 CONTEXT 菜单?
我正在尝试为“项目和解决方案上下文菜单>解决方案”菜单创建一个附加菜单项。但是,我希望此上下文菜单仅在打开某个解决方案时出现,否则我不希望它显示。我想我可以在 Visual Studio IDE 中使用 MACROS。在解决方案和项目的开放等情况下会举办一些活动。
我的问题是,我能否以编程方式创建我想要的上下文菜单,将其指向我希望它运行的宏,然后以编程方式将其附加到我想要在其中显示它的 IDE 内的 CONTEXT 菜单?
我发现这个链接非常有用。
在 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;
在点击事件处理程序中,我做了任何需要的事情。
请注意,这是针对带有子项的主菜单项。对于单个项目,请查看此视频。这家伙一步一步走……做得很好。
您可以使用它DTE.ExecuteCommand()
来执行其他宏。
我找到了我的问题的答案。我使用以下代码来创建我需要的菜单项:
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