Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Newbie programming in C#.net.

I have a very simple desktop application in in arcmap. I'm trying to get the coordinates when a user clicks a button on a toolbar and when click the map. The toolbar is implemented using the BaseToolBar. I use the base tool to grab the mouse down event ( Not sure whether this is the correct way even) Here is the code which it generates.

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
}
}

Now I need to get the coordinates out. How can I do this? where should I call the event mouse down event. The message box does not display at all.

Pleas help.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

There is nothing wrong with your code. It works fine. You are doing it right. I can select the tool, click on the map and the coordinates are displayed in the message box.

To convert to map units add references to ESRI.ArcGIS.Display, ESRI.ArcGIS.Geometry and ESRI.ArcGIS.Carto

and replace the OnMouseDown with this

public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = (m_application.Document as IMxDocument).ActiveView.ScreenDisplay;
            ESRI.ArcGIS.Geometry.IPoint point = screenDisplay.DisplayTransformation.ToMapPoint(X, Y);

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

Edit: You don't "call the mouse down event". The code you place in the OnMouseDown function is executed when the map is clicked. OnMouseDown is an event handler.

share|improve this answer

You should be able to use the OnMouse down event on the MapControl see snippet MapControl Left and Right Mouse Clicks Snippet to get the map coordinates you need to convert the screen coordinates to map coordinates, see snippet: Convert Display Pixels to Map Units Snippet

share|improve this answer

Your Answer

 
discard

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.