Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got a context menu to work by following this tutorial:

The interesting thing about the tutorial is that the context menu items are COM objects and they are added to the registry via the EsriRegAsm.exe tool at build time.

I add them programmatically to my context menu at runtime via their GUIDs. I right click my map, then I display a context menu popup, then I select a menu item which is derived from a BaseCommand (here I can't debug into because it is in the COM world), now, according to the tutorial, I am supposed to do some map operations here since I have a IMapControl4 object. But what I really want to do is to pass the control up to my WPF project.

To summarize I would like to call a custom function of mine from the BaseCommand.

I am using ArcEngine 10 in VS2010 with C#.

I found a dumb hack to do it, in the OnClick method, I set the IMapControl4instance.DocumentFilename = "SomeCommand";

Then in my main app, I have an mouse clicked event handler. From the COM object I simulate a mouse move event from within the OnClick() via the user32.dll. Then in my main app the mouseclick event handler checks if mapControl.DocumentFileName is one of my commands, and then sets DocumentFileName to the original value, and executes some operation.

I'm not really proud of this hack, but I'm pleased it works, I'd rather do it the normal way, whatever that is.

share|improve this question

Sounds like the problem you have is not with ArcGIS Engine, it's probably caused by not separating GUI from application logic. I would suggest looking at patterns like MVC or frameworks like prism these help you sort out communication between different parts of you application.

share|improve this answer
The project is a .NET app, and it hosts context menus which are registered via COM. It is a property of their design, I suspect the proper way to do it is to open up a COM communication layer between the registered COM context menu and the .NET app. If I get around to doing it that way I will update this question. – patrick Oct 25 '10 at 14:33
@Iterationx, I created my own context menu for the TOC using .Net so I haven't looked much on the ESRI menus, but if your requirement is to use COM registration you can still do what Herb is suggesting, a singleton object that expose the variables to the command or tool. – MathiasWestin Oct 25 '10 at 20:33
@Iterationx, If you plan to continue to use the IMapControl take a look at the CustomProperty it's the standard way to store custom objects in the map control.… – MathiasWestin Oct 26 '10 at 5:51

Since the context menu items derive from BaseCommand, their functionality is specified in the "OnClick" function - that's where you put your custom functionality. Or am I missing the point?

Edit: I was missing the point.

You probably need to start here: HookHelper. Short version is that an object is passed into OnCreate for your commands, and the HookHelper will help you figure out what it is. You should be able to get to your custom context menu object, which - since you're creating it in code can have a reference back to some application object - gets you where you need to be.

The other alternative is to create a singleton for your application, and thus have a known class with a static method. It's probably easier, but more tightly couples your commands back to the application.

share|improve this answer
The functionality is in the OnClick. I load up my WPF application and I have a bunch of variables I want to access, but I don't know how to get at them from the OnClick. It would be nice if I could pass in a delegate for the OnClick to invoke for example. – patrick Oct 5 '10 at 21:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.