Solved! Go to Solution.
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?
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!
// variable declarations in class level private IMapControlEvents2_Event mapControlEvents = null; private IMapControlDefault mapControlDefault = null; // wiring up the events in one of the called member function, each event handler use MessageBox.Show to display a message mapControlDefault = new MapControl(); mapControlDefault.Map = view.FocusMap; mapControlEvents = mapControlDefault as IMapControlEvents2_Event; mapControlEvents.OnMouseDown += new IMapControlEvents2_OnMouseDownEventHandler(mapControlEvents_OnMouseDown); mapControlEvents.OnSelectionChanged += new IMapControlEvents2_OnSelectionChangedEventHandler(mapControlEvents_OnSelectionChanged); mapControlEvents.OnMouseUp += new IMapControlEvents2_OnMouseUpEventHandler(mapControlEvents_OnMouseUp); mapControlEvents.OnMouseMove += new IMapControlEvents2_OnMouseMoveEventHandler(mapControlEvents_OnMouseMove); mapControlEvents.OnDoubleClick += new IMapControlEvents2_OnDoubleClickEventHandler(mapControlEvents_OnDoubleClick); mapControlEvents.OnExtentUpdated += new IMapControlEvents2_OnExtentUpdatedEventHandler(mapControlEvents_OnExtentUpdated); mapControlEvents.OnKeyDown += new IMapControlEvents2_OnKeyDownEventHandler(mapControlEvents_OnKeyDown);
IMapControl4 mapControl = axMapControl1.Object as IMapControl4;
Sui-
I don't think you want to get a reference to your IMapControl2 object using the new keyword. I think you need to query interface from an existing object.
Something like this:IMapControl4 mapControl = axMapControl1.Object as IMapControl4;
Do you have an axMapControl1 object somewhere?
Are you using ArcEngine? I assumed you were since you were trying to use the IMapControl. That interface is for accessing the map of an ArcEngine control.
That really depends on what you are trying to do. Are you trying to return information from a feature clicked in the map? Trying to pop up a context menu when user right-clicks? It's all different. Please define what you are trying to acheive.
A typical scenario is the need to capture the location when a user left-click the map. To accomplish this I create a ITool that the user activates prior to clicking. The ITool provides event handlers such as OnMouseUp that you can add code to.
// set the tool with hardcoded UID to current ICommandBars documentBars = _mApplication.Document.CommandBars; UID cmdID = new UIDClass(); cmdID.Value = "{0dxxc599-c213-4xxb-8978-1227bac1bx8d}"; ICommandItem cmdItem = documentBars.Find(cmdID, false, false); (cmdItem.Command as TestTool).rollbackTool = _mApplication.CurrentTool; _mApplication.CurrentTool = cmdItem; [Guid("0dxxc599-c213-4xxb-8978-1227bac1bx8d")] [ClassInterface(ClassInterfaceType.None)] [ProgId("CustTool.TestTool")] public class TestTool : BaseTool { // registration methods // ... // OnCreate method // .. public ICommandItem rollbackTool = null; private IApplication mApplication = null; public override void OnMouseDown(int button, int shift, int x, int y) { // do the work mApplication.CurrentTool = this.rollbackTool; } }
That really depends on what you are trying to do. Are you trying to return information from a feature clicked in the map? Trying to pop up a context menu when user right-clicks? It's all different. Please define what you are trying to acheive.
A typical scenario is the need to capture the location when a user left-click the map. To accomplish this I create a ITool that the user activates prior to clicking. The ITool provides event handlers such as OnMouseUp that you can add code to.