5

I am new to programming in C#.net.

I have a very simple desktop application 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.

To do this where should I call the event mouse down event?

The message box does not display at all.

2

3 Answers 3

3

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.

1

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

0

an accepted answer should mean it is working and correct, so why isnt this working for me ? I did the same thing and nothing happens when I click on the map. I even tried this twice, first time, I created a class that inheret Basetool, nothing happens... and then I tried to create a class that implements Itool implemented the members. again run the tool no results what so ever and the debugger doesnt event stop at the breake points for those events. where am I going wrong here ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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