I have read and searched the web for an answer, but nothing is working. so I need someone's help please.
I have an extension that implements I command, I have the method OnContextMenu(x,y) which fires on right clicking.
I would like to create a context menu, that contains 4 options, show, hide, add, delete... here is what I am doing and what I am getting
Extension Class
public class Extension1 : ESRI.ArcGIS.Desktop.AddIns.Extension , ICommand{
public Extension1() {
}
protected override void OnStartup() {
var mDocEvents = NEAutomation2.Application.Instance().IApplication.Document as IDocumentEvents_Event;
if (mDocEvents != null) {
mDocEvents.OnContextMenu += m_docEvents_OnContextMenu;
}
WireDocumentEvents();
}
private void WireDocumentEvents() {
// Named event handler
ArcMap.Events.NewDocument += delegate() { ArcMap_NewDocument(); };
// Anonymous event handler
ArcMap.Events.BeforeCloseDocument += delegate() {
// Return true to stop document from closing
ESRI.ArcGIS.Framework.IMessageDialog msgBox = new ESRI.ArcGIS.Framework.MessageDialogClass();
return msgBox.DoModal("BeforeCloseDocument Event", "Abort closing?", "Yes", "No", ArcMap.Application.hWnd);
};
}
private bool m_docEvents_OnContextMenu(int x, int y) {
// display context menu
}
Here is the context menu class that I built
namespace desktopExtension {
/// <summary>
/// Summary description for ContextMenu.
/// </summary>
[Guid("ca8860e1-d80c-4071-8ace-a3a47692d1ef")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("desktopExtension.ContextMenu")]
public sealed class ContextMenu : BaseMenu, IShortcutMenu {
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType) {
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType) {
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType) {
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommandBars.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType) {
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommandBars.Unregister(regKey);
}
#endregion
#endregion
public ContextMenu() {
// here I want to add the items that I want, add,remove,show,hide
not the ones that are built in
}
public override string Caption {
get {
//TODO: Replace bar caption
return "My C# Context Menu";
}
}
public override string Name {
get {
//TODO: Replace bar ID
return "ContextMenu";
}
}
}
}
I managed to create a menu using this code but I can't add items to it, and when I try to find my context menu that I created above I get value is not within range, or command not found error, as if the menu is not registered or something. Here is the code that I use in OnContextMenu
var uid = new ESRI.ArcGIS.esriSystem.UIDClass();
uid.Value=""desktopExtension.ContextMenu"";-------------> error here
IDocument document = ArcMap.Document as IDocument;
var bars = document.CommandBars;
var dound=bars.Find(uid) as ICommandBar;
dound.Popup(x, y);
Also I tried to create the context menu as follows, but can't add items because I don't know where to get their UIDs from or how to create the Command Items
var men=commandBars.Create("TemporaryContextMenu",esriCmdBarType.esriCmdBarTypeShortcutMenu);
men.Add() // takes UID of command Item
I really need someone to help me with this... there are no examples online that can help me with this, the snippets don't do what I want either, they only add a context menu, and the command items are the ones built in.