Autodesk. AutoCAD. applicationservices. application supports two contextmenu extensions: defaultcontextmenu and objectcontextmenu.
Defaultcontextmenu: no shortcut menu is selected in the current context.
Objectcontextmenu: select the shortcut menu for the specified type of entity in the current context. For example:
# Region Addcontextmenu
/// <Summary>
/// Right-click menu
/// </Summary>
Private Void Addcontextmenu ()
{
Try
{
// Defaultcontextmenu
Contextmenuextension defaultcontextmenu = New Contextmenuextension ();
Defaultcontextmenu. Title = " Mydefaconcontextmenu " ;
Autodesk. AutoCAD. Windows. menuitem defaultcontextmenu_item1 = New Autodesk. AutoCAD. Windows. menuitem ( " Mydefaconcontextmenu_item1 " , ARX. UI. Resources. resource1.taobao );
Defaultcontextmenu_item1.click + = New Eventhandler (defaultcontextmenu_onclick );
Defaultcontextmenu. menuitems. Add (defaultcontextmenu_item1 );
Arxapp. adddefaconcontextmenuextension (defaultcontextmenu );
// Objectcontextmenu
Contextmenuextension objcontextmenu = New Contextmenuextension ();
Objcontextmenu. Title = " Myobjectcontextmenu " ;
Objcontextmenu. Popup + = New Eventhandler (objcontextmenu_popup );
Autodesk. AutoCAD. Windows. menuitem objcontextmenu_item1 = New Autodesk. AutoCAD. Windows. menuitem ( " Go to Baidu " , ARX. UI. Resources. resource1.baidu );
Objcontextmenu_item1.click + = New Eventhandler (objcontextmenu_item1_click );
Objcontextmenu. menuitems. Add (objcontextmenu_item1 );
Autodesk. AutoCAD. Windows. menuitem objcontextmenu_item2 = New Autodesk. AutoCAD. Windows. menuitem ( " Go to Google " , ARX. UI. Resources. resource1.google );
Objcontextmenu_item2.click + = New Eventhandler (objcontextmenu_item2_click );
Objcontextmenu. menuitems. Add (objcontextmenu_item2 );
Arxapp. addobjectcontextmenuextension (rxobject. getclass ( Typeof (Polyline), objcontextmenu );
}
Catch (System. Exception exc)
{
Writeline ( String . Format ( " \ N contextmenu error: {0} " , Exc. Message ));
}
}
Void Objcontextmenu_popup ( Object Sender, eventargs E)
{
Using (Documentlock doclock = Arxapp. documentmanager. mdiactivedocument. lockdocument ())
{
Document Doc = Application. documentmanager. mdiactivedocument;
Database DB = Doc. database;
Editor ed = Doc. Editor;
Bool Visible = True ;
// If that is multiple selection, disabled the menu item.
Promptselectionresult selectionres = Ed. selectimplied ();
If (Selectionres. Status = Promptstatus. OK)
{
Objectid [] objids = Selectionres. value. getobjectids ();
If (Objids ! = Null && Objids. Length > 1 )
{
Visible = False ;
}
}
Contextmenuextension objcontextmenu = Sender As Contextmenuextension;
If (Objcontextmenu ! = Null )
{
Foreach (Menuitem item In Objcontextmenu. menuitems)
{
Item. Enabled = Visible;
}
}
}
}
Private Void Defaultcontextmenu_onclick ( Object Sender, eventargs E)
{
Using (Documentlock doclock = Arxapp. documentmanager. mdiactivedocument. lockdocument ())
{
Arxapp. showalertdialog ( " Defaultcontextmenu_onclick " );
}
}
Void Objcontextmenu_item1_click ( Object Sender, eventargs E)
{
Using (Documentlock doclock = Arxapp. documentmanager. mdiactivedocument. lockdocument ())
{
System. Diagnostics. process. Start ( " Iexplore. exe " , " Http://www.baidu.com " );
}
}
Void Objcontextmenu_item2_click ( Object Sender, eventargs E)
{
Using (Documentlock doclock = Arxapp. documentmanager. mdiactivedocument. lockdocument ())
{
System. Diagnostics. process. Start ( " Iexplore. exe " , " Http://www.google.com " );
}
}
# Endregion
You can perform some logic processing in the objcontextmenu_popup event.
The following is the namespace used:
Code
Using Autodesk. AutoCAD. applicationservices;
Using Arxapp = Autodesk. AutoCAD. applicationservices. Application;
Using Arxdoc = Autodesk. AutoCAD. applicationservices. Document;
Using Autodesk. AutoCAD. databaseservices;
Using Autodesk. AutoCAD. windows;