Had to dig back to old versions of the API help. But from the 2013 help here is the example code. I've test this and the code still works in 2017.
Add a button to the context menu API Sample
' Copy the following into a Module.
Option Explicit
Public oClass1 As Class1
Sub AddToMenu()
Set oClass1 = New Class1
oClass1.Initialize
End Sub
'*************************************************************
' The declarations and functions below need to be copied into
' a class module whose name is "Class1". The name
' can be changed but you'll need to change the declaration in
' the calling function "AddToMenu" to use the new name.
Option Explicit
Private WithEvents oUserInputEvents As UserInputEvents
Public Sub Initialize()
Set oUserInputEvents = ThisApplication.CommandManager.UserInputEvents
End Sub
Private Sub oUserInputEvents_OnContextMenu(ByVal SelectionDevice As SelectionDeviceEnum, ByVal AdditionalInfo As NameValueMap, ByVal CommandBar As CommandBar)
On Error Resume Next
' Check if the context menu has the 'Home View' (formerly 'Isometric View') command
Dim oCmdBarControl As CommandBarControl
Set oCmdBarControl = CommandBar.Controls.Item("AppIsometricViewCmd")
If Not oCmdBarControl Is Nothing Then
Dim oCtrlDef As ControlDefinition
Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomAllCmd")
' Add the "Zoom All" command before the home view command
Call CommandBar.Controls.AddButton(oCtrlDef, oCmdBarControl.index)
End If
End Sub
|
I still use this code in software using C#.
Summarizing what it does.
Create a button definition. Add it to the ControlDefinitions of the Application.CommandManager object. Add a event handler on the button class as normal. Add an event handler to the CommandManager.UserInputs.OnContextMenu event. In that event handler determine if it's the appropriate context to show your command. IE... look at any selected objects etc... If so add your button to the list if it's not there. If not and it's present remove your button. The definition still exists.
Some C# code I use to achieve this. I create the command definition as normal, but use the event to conditionally add it to the context menu.
Snippet
void UserInputEvents_OnContextMenu(SelectionDeviceEnum SelectionDevice, NameValueMap AdditionalInfo, CommandBar CommandBar)
{
if (appInventor.ActiveDocument == null)
return;
CommandBarControl cmdCtrl = null;
foreach (CommandBarControl tCtrl in CommandBar.Controls)
{
if (tCtrl.InternalName.Equals("btnNewBtnName"))
{
cmdCtrl = tCtrl;
break;
}
}
bool hasItems = appInventor.ActiveDocument.SelectSet.Count > 0;
if ((cmdCtrl == null) && hasItems)
{
cmdCtrl = CommandBar.Controls.AddButton(btnNewBtn);
}
else if ((cmdCtrl != null) && !hasItems)
{
cmdCtrl.Delete();
}
}