目前市面上的ArcEngine教程一般只會介紹shp、geodatabase等數據的編輯操作,基本不會涉及IElement的編輯操作。舉個例子,當你在ArcMap中添加一個指北針或比例尺的時候,你可以利用鼠標動態更改它們的大小及位置,其實在ArcEngine中也可以實現類似操作,先給大家看看效果:
主界面代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using WindowsFormsApplication2.Tool;
namespace WindowsFormsApplication2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
// 繪製元素
private void btnDrawElement_Click(object sender, EventArgs e)
{
ICommand command = new DrawElementTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 編輯元素
private void btnEditElement_Click(object sender, EventArgs e)
{
ICommand command = new EditElementTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
}
}
繪製元素代碼
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using System.Windows.Forms;
namespace WindowsFormsApplication2.Tool
{
/// <summary>
/// Summary description for DrawElementTool.
/// </summary>
[Guid("2b53c3d5-fe61-41cd-985e-4b4d7676980e")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication2.Tool.DrawElementTool")]
public sealed class DrawElementTool : 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);
ControlsCommands.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);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
public DrawElementTool()
{
//
// TODO: Define values for the public properties
//
base.m_category = ""; //localizable text
base.m_caption = ""; //localizable text
base.m_message = ""; //localizable text
base.m_toolTip = ""; //localizable text
base.m_name = ""; //unique id, non-localizable (e.g. "MyCategory_MyTool")
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 Overridden Class Methods
/// <summary>
/// Occurs when this tool is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
// TODO: Add DrawElementTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawElementTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IMapControlDefault m_mapControl = m_hookHelper.Hook as IMapControlDefault;
IGeometry pGeometry = m_mapControl.TrackPolygon();
// 創建顏色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 創建符號
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.Color = pRgbColor;
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSCross;
// 創建元素
IFillShapeElement pFillShapeElement = new PolygonElement() as IFillShapeElement;
pFillShapeElement.Symbol = pSimpleFillSymbol;
IElement pElement = pFillShapeElement as IElement;
pElement.Geometry = pGeometry;
// 繪製元素
IActiveView pActiveView = m_mapControl.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.DeleteAllElements();
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
// 選中該元素
IGraphicsContainerSelect pGraphicsContainerSelect = pGraphicsContainer as IGraphicsContainerSelect;
pGraphicsContainerSelect.SelectElement(pElement);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection, null, null);
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawElementTool.OnMouseMove implementation
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawElementTool.OnMouseUp implementation
}
#endregion
}
}
編輯元素代碼
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using System.Windows.Forms;
namespace WindowsFormsApplication2.Tool
{
/// <summary>
/// Summary description for EditElementTool.
/// </summary>
[Guid("e3405301-33a6-4a07-95d6-6479ef1cefde")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication2.Tool.EditElementTool")]
public sealed class EditElementTool : 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);
ControlsCommands.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);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
private IElement pElement;
private ISelectionTracker pSelectionTracker;
public EditElementTool()
{
//
// TODO: Define values for the public properties
//
base.m_category = ""; //localizable text
base.m_caption = ""; //localizable text
base.m_message = ""; //localizable text
base.m_toolTip = ""; //localizable text
base.m_name = ""; //unique id, non-localizable (e.g. "MyCategory_MyTool")
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 Overridden Class Methods
/// <summary>
/// Occurs when this tool is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add EditElementTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IGraphicsContainer pGraphicsContainer = m_hookHelper.ActiveView.GraphicsContainer;
IGraphicsContainerSelect pGraphicsContainerSelect = pGraphicsContainer as IGraphicsContainerSelect;
// 判斷是否有元素被選中
IEnumElement pEnumElement = pGraphicsContainerSelect.SelectedElements;
pEnumElement.Reset();
pElement = pEnumElement.Next();
if (pElement == null)
{
MessageBox.Show("當前沒有被選中的元素");
return;
}
// 初始化ISelectionTracker
IElementEditVertices pElementEditVertices = pElement as IElementEditVertices;
pElementEditVertices.MovingVertices = true;
pSelectionTracker = pElementEditVertices.GetMoveVerticesSelectionTracker();
pSelectionTracker.Display = m_hookHelper.ActiveView.ScreenDisplay;
pSelectionTracker.Draw(m_hookHelper.ActiveView.ScreenDisplay, 0, esriTrackerStyle.esriTrackerActive);
pSelectionTracker.OnMouseDown(Button, Shift, X, Y);
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (pSelectionTracker != null)
{
pSelectionTracker.OnMouseMove(Button, Shift, X, Y);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (pSelectionTracker != null)
{
pSelectionTracker.OnMouseUp(Button, Shift, X, Y);
pElement.Geometry = pSelectionTracker.Geometry;
m_hookHelper.ActiveView.GraphicsContainer.UpdateElement(pElement);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphicSelection, null, null);
}
}
#endregion
}
}