Wednesday 23 May 2018

arcobjects - How to implement mouse down event when the user clicks on the map?


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 description for GetXY.
///

[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
///
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
///

private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);

MxCommands.Register(regKey);

}
///
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
///

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

///
/// Occurs when this tool is created
///

/// Instance of the application
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

}

///
/// Occurs when this tool is clicked
///

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.



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...