Select to view content in your preferred language

How to add handler to mouse click on the map of ArcMap?

7750
13
Jump to solution
06-14-2012 02:07 PM
SuiHuang
Frequent Contributor
Hi Everybody:

    I am writing an ArcMap 10 extension using C#. I want to detect the user's mouse click on the map. However, I haven't successfully get it done.

    I notice that IMapControlEvent2 should give a delegate to handle the event, but I don't know how to get IMapControlEvent2 out of the current application. I did the following but it doesn't work:
***************************************************************
IActiveView view = (_mApplication.Document as IMxDocument).ActivatedView;
IMapControlDefault mapControlDefault = new MapControl();
mapControlDefault.Map = view.FocusMap;
IMapControlEvents2 mapControlEvents = mapControlDefault as IMapControlEvents2;
***************************************************************

    How shall I do it?
    Thank you!
0 Kudos
13 Replies
RichWawrzonek
Frequent Contributor
Are you using Visual Studio? There are templates in VS to create custom tools for ArcMap. You can create them as COM extensions (need an installer to deploy) or using the newer Add-in framework. I recommend the Add-in method because it makes deployment much easier. It looks like your code is attempting to use the COM method. There is a lot more to it than what you have and yes it has to be registered. Do you have the template?
0 Kudos
SuiHuang
Frequent Contributor
Hi richwawr:

    Yes I am using Visual Studio, and I actually created the code with template and then modified it. I am using the COM approach because COM can still do more then Add-in, even though Add-in is easy to deploy.
    The reason I why I was not using the custom tool template at the beginning is that: I don't actually want to create a tool with a button on the ArcMap toolbar. Instead I want to make a button on a windows form to bring user into a state of picking features on the map, so I tried to handle the map events first. However, I still end up building a tool (but hide it from the toolbar) 🙂
    I just use the following derived BaseTool classes to allow the state of ArcMap tool rollback to original after user has interacted.
    Any better idea?
    Thank you!

    public abstract class RollbackableToolBase : BaseTool      {         protected IApplication m_Application = null;           public ICommandItem RollbackTool { get; set; }          public override sealed void OnCreate(object hook)         {             m_Application = hook as IApplication;         }     }      public class RollbackableTool1 : RollbackableToolBase      {         public RollbackableTool1 ()         {             Result = null;         }          // the shoot event         public event RanEventHandler Ran= null;          public object Result { get; private set; }          public override void OnMouseDown(int button, int shift, int x, int y)         {             // interact with the screen and get the result              // restore the current tool             m_Application.CurrentTool = this.RollbackTool;         }     }      public delegate void RanEventHandler(object sender, EventArgs e);


Are you using Visual Studio? There are templates in VS to create custom tools for ArcMap. You can create them as COM extensions (need an installer to deploy) or using the newer Add-in framework. I recommend the Add-in method because it makes deployment much easier. It looks like your code is attempting to use the COM method. There is a lot more to it than what you have and yes it has to be registered. Do you have the template?
0 Kudos
RichWawrzonek
Frequent Contributor
So is it working ok now? You seem to be doing it right. Use the template to build the ITool, then hide it from the toolbar. Because it is a COM tool you can access it by the GUID. It doesn't need to be re-registered at runtime. It gets registered when it is installed. Then it is available to you when ArcMap starts via the GUID.
0 Kudos
SuiHuang
Frequent Contributor
Hi richwawr:

    Yes it is working for me now. Thank you for your help!

Sui

So is it working ok now? You seem to be doing it right. Use the template to build the ITool, then hide it from the toolbar. Because it is a COM tool you can access it by the GUID. It doesn't need to be re-registered at runtime. It gets registered when it is installed. Then it is available to you when ArcMap starts via the GUID.
0 Kudos