//http://resources.esri.com/help/9.3/arcgisengine/arcobjects/esriDisplay/Moving_Feedback_Example.htm
//アドイン例
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
namespace ArcMapAddin1
{
public class Tool1 : ESRI.ArcGIS.Desktop.AddIns.Tool
{
private IMxDocument m_mxDoc;
private IActiveView m_activeView;
private IScreenDisplay m_screenDisp;
private IDisplayFeedback m_dispFeed;
private IElement m_hitElem;
private IGraphicsContainer m_graphicsCont;
public Tool1()
{
}
protected override void OnUpdate()
{
Enabled = ArcMap.Application != null;
}
protected override void OnActivate()
{
m_mxDoc = (IMxDocument)ArcMap.Application.Document;
m_activeView = m_mxDoc.ActiveView;
m_screenDisp = m_activeView.ScreenDisplay;
}
protected override void OnMouseDown(MouseEventArgs arg)
{
base.OnMouseDown(arg);
IPoint point = null;
IGeometry geomElem = null;
// Get the current mouse location in Map Units
point = m_screenDisp.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
// Use a function to return the first element at this point (if any)
m_hitElem = GetHitElement(point);
// If an element was returned then check what type of geometry it has (Point, Polyline, Envelope or Polygon)
if (m_hitElem != null)
{
geomElem = m_hitElem.Geometry;
//Point geometry
if (geomElem is IPoint)
{
// Create a MovePointFeedback object and set its display property (to the ActiveView's ScreenDisplay)
m_dispFeed = new MovePointFeedback();
m_dispFeed.Display = m_screenDisp;
// QI for the IMovePointFeedback interface
IMovePointFeedback mvPtFeed = (IMovePointFeedback)m_dispFeed;
//Start the feedback using the input (Point) geometry at the current mouse location
mvPtFeed.Start((IPoint)geomElem, point);
// Polyline geometry
}
else if (geomElem is ESRI.ArcGIS.Geometry.IPolyline)
{
// Create a MoveLineFeedback object and set its display property (to the ActiveView's ScreenDisplay)
m_dispFeed = new MoveLineFeedback();
m_dispFeed.Display = m_screenDisp;
// QI for the IMoveLineFeedback interface
IMoveLineFeedback mvLnFeed = (IMoveLineFeedback)m_dispFeed;
//Start the feedback using the input (Polyline) geometry at the current mouse location
mvLnFeed.Start((IPolyline)geomElem, point);
// Rectangle (Envelope) geometry
}
else if (geomElem is IEnvelope)
{
// Create a MoveEnvelopeFeedback object and set its display property (to the ActiveView's ScreenDisplay)
m_dispFeed = new MoveEnvelopeFeedback();
m_dispFeed.Display = m_screenDisp;
// QI for the IMoveEnvelopeFeedback interface
IMoveEnvelopeFeedback mvEnvFeed = (IMoveEnvelopeFeedback)m_dispFeed;
//Start the feedback using the input (Rectangle) geometry at the current mouse location
mvEnvFeed.Start((IEnvelope)geomElem, point);
// Polygon geometry
}
else if (geomElem is IPolygon)
{
// Create a MovePolygonFeedback object and set its display property (to the ActiveView's ScreenDisplay)
m_dispFeed = new MovePolygonFeedback();
m_dispFeed.Display = m_screenDisp;
// QI for the IMovePolygonFeedback interface
IMovePolygonFeedback mvPolyFeed = (IMovePolygonFeedback)m_dispFeed;
//Start the feedback using the input (Polygon) geometry at the current mouse location
mvPolyFeed.Start((IPolygon)geomElem, point);
}
}
}
protected override void OnMouseUp(MouseEventArgs arg)
{
IGeometry geomResult = null;
IGeometry geomElem = null;
// Check that the user is using the feedback
if (m_hitElem != null)
{
// Get the geometry type for our element again
geomElem = m_hitElem.Geometry;
// Check what type of geometry the element has (again)
// Point geometry
if (geomElem is IPoint)
{
// QI for the IMovePointFeedback interface and get the finished geometry
IMovePointFeedback mvPtFeed = (IMovePointFeedback)m_dispFeed;
geomResult = mvPtFeed.Stop();
}
else if (geomElem is IPolyline)
{
// QI for the IMoveLineFeedback interface and get the finished geometry
IMoveLineFeedback mvLnFeed = (IMoveLineFeedback)m_dispFeed;
geomResult = mvLnFeed.Stop();
}
else if (geomElem is IEnvelope)
{
// QI for the IMoveEnvelopeFeedback interface and get the finished geometry
IMoveEnvelopeFeedback mvEnvFeed = (IMoveEnvelopeFeedback)m_dispFeed;
geomResult = mvEnvFeed.Stop();
}
else if (geomElem is IPolygon)
{
// QI for the IMovePolygonFeedback interface and get the finished geometry
IMovePolygonFeedback mvPolyFeed = (IMovePolygonFeedback)m_dispFeed;
geomResult = mvPolyFeed.Stop();
}
// Set the geometry of the element and call update
m_hitElem.Geometry = geomResult;
m_graphicsCont.UpdateElement(m_hitElem);
// Clear out the objects
m_dispFeed = null;
m_hitElem = null;
// Refresh the ActiveView
m_activeView.Refresh();
}
}
protected override void OnRefresh(int hDC)
{
base.OnRefresh(hDC);
m_mxDoc = (IMxDocument)ArcMap.Application.Document;
m_activeView = m_mxDoc.ActiveView;
m_screenDisp = m_activeView.ScreenDisplay;
}
protected override void OnMouseMove(MouseEventArgs arg)
{
if (m_dispFeed != null)
{
IPoint point = null;
// Get the current mouse location in Map Units and move the feedback
point = m_screenDisp.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
m_dispFeed.MoveTo(point);
}
}
private IElement GetHitElement(IPoint pInPt)
{
// Takes an IPoint and returns the first element that is hit (if any) in the ActiveView's BasicGraphicsLayer
IEnumElement enumElem = null;
double dblSrchDis = 0;
// QI for the IGraphicsContainer interface from the IActiveView, allows access to the BasicGraphicsLayer
m_graphicsCont = (IGraphicsContainer)m_activeView;
// Calculate the Search Distance (in MapUnits) based upon a portion of the ActiveView's width
dblSrchDis = m_activeView.Extent.Width / 200;
// Return an enumerator for those elements found within the search distance (in mapunits)
enumElem = m_graphicsCont.LocateElements(pInPt, dblSrchDis);
// If the enumerator is not empty then return the FIRST element found
if (enumElem != null)
{
return enumElem.Next();
}
else
{
return null;
}
}
}
}
記事
