Getting the X,Y coordinates when a user clicks on a given layer

7782
9
12-15-2010 09:04 AM
shasam
by
New Contributor
Hi,

Newbie to arcobjects programming. I code in C#.net.

Developing an application where I need to get the coordinates when a user click on a particular point on a layer in arcmap.  I have the following 2 questions.

1) How can I get these coordinates?
2) How can I transform them to Map points?

Any code example would be greatly helpful.

Thanks
0 Kudos
9 Replies
SteveFang
New Contributor III
You would need to get the screen XY from the OnMouseDown event for your tool.

As for transforming them to map points, look up the IDisplayTransformation.ToMapPoint.  I think there are samples and code snippet in the documentations.
0 Kudos
shasam
by
New Contributor
Sorry dd not understand.

Developing and application in arcdesktop.

How can I get the screen coordinates (X,Y) where the user points to in the map from c#.net ?

Thanks
0 Kudos
SteveFang
New Contributor III
In your class that is implementing the ITool interface, one of the event is "OnMouseDown".  Basically, that is fired when a user clicks on the map in your application.  Two of the parameters are X and Y.  Basically, they are XY of the click on the screen.  You need to take these two parameters to get XY in map units.  Do something like,

Dim pPnt as Ipoint = Nothing
Dim pActiveView as IActiveView

pActiveView = "Your Map Object"
pPnt = pActiveView.ScreeDisplay.Displaytransformation.ToMapPoint(x, y)

pPnt is a point with XY coordinate in map units that correspond to your screen click.
0 Kudos
shasam
by
New Contributor
Thank you for the feed back.

Still having problems.

I do not have a ITool interface implemented anywhere.
I'm using BaseToolbar to get my tool bar in ArcMap.

Should I implement the ITool interface in that class or in a new class?
Also is there sample code on how to implement the ITool ? Then the next question is how should the communication happen between my tool bar and the ITool mouse down event?

Please help..
0 Kudos
SteveFang
New Contributor III
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//000100000066000000

It explains commands and tool and how to use them.  Right below that section is a section on toolbar.  You would need to implement ITool in a new class, try using basetool so you don't have to type so much.  But the documentation explains it all.  Good luck.
0 Kudos
Venkata_RaoTammineni
Occasional Contributor
Hi

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
using System.Windows.Forms;

namespace GIS_FORMS
{
    /// <summary>
    /// Summary description for GetXY.
    /// </summary>
    [Guid("b1e4d6f0-53de-44d3-b679-f29432d1fe35")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("GIS_FORMS.GetXY")]
    public sealed class GetXY : BaseTool
    {
        #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);
            MxCommands.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);
            MxCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IApplication m_application;
        public GetXY()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "GIS_FORMS"; //localizable text
            base.m_caption = "get xy";  //localizable text
            base.m_message = "get xy";  //localizable text
            base.m_toolTip = "get xy";  //localizable text
            base.m_name = "get_xy";   //unique id, non-localizable (e.g. "MyCategory_ArcMapTool")
            try
            {
                //
                // TODO: change resource name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this tool is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            m_application = hook as IApplication;

            //Disable if it is not ArcMap
            if (hook is IMxApplication)
                base.m_enabled = true;
            else
                base.m_enabled = false;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add GetXY.OnClick implementation
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add GetXY.OnMouseDown implementation

            MessageBox.Show("X position is " + X.ToString() +"Y position is :" + Y.ToString());
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add GetXY.OnMouseMove implementation
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add GetXY.OnMouseUp implementation
        }
        #endregion
    }
}

You are looking for the same or you want to get layer extents ?
0 Kudos
shasam
by
New Contributor
This is what I'm looking for.
Thanks a lot.

This can be a stupid question. but how can i call this?   and from where should I call this?
I implement a BaseToolbar and BaseCommand .
Should I call it from one of those?

when I do this on the click event on the BaseCommand class , I need to pass paramaters...
GetXY newGetXY = new GetXY();
newGetXY.OnMouseDown(); // need to pass arguments..

Please help on how i can call the GetXY method and from where?
0 Kudos
Venkata_RaoTammineni
Occasional Contributor
This is what I'm looking for.
Thanks a lot.

This can be a stupid question. but how can i call this?   and from where should I call this?
I implement a BaseToolbar and BaseCommand .
Should I call it from one of those?

when I do this on the click event on the BaseCommand class , I need to pass paramaters...
GetXY newGetXY = new GetXY();
newGetXY.OnMouseDown(); // need to pass arguments..

Please help on how i can call the GetXY method and from where?


Hi,

Please check the url..

http://resources.esri.com/help/9.3/ArcGISDesktop/dotnet/concepts_start.htm#01c01659-cdf8-4579-9c87-2...
0 Kudos
LukeBadgerow
New Contributor
So what if I'm not running a tool at all.  I'm adding items to a context menu that will need the screen point that was clicked to fire the context menu?
0 Kudos