ArcEngine绘制点、线、多边形、矩形、圆形、椭圆的代码
程序员文章站
2022-07-14 23:06:13
...
熟悉ArcGIS的同志应该知道在ArcMap中编辑shp文件时,我们可以绘制点、线、多边形、矩形、圆形、椭圆等几何图形。其实在ArcEngine中也可以实现同样的功能,我们可以利用INewLineFeedback等接口实现不同几何图形的绘制。我在这里做了一个demo,界面如下图所示:
1、绘制点
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
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;
namespace WindowsFormsApplication1.Utility
{
/// <summary>
/// Summary description for DrawPointTool.
/// </summary>
[Guid("3547308e-1854-45ec-850b-706fac097943")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Utility.DrawPointTool")]
public sealed class DrawPointTool : 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 DrawPointTool()
{
//
// 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 DrawPointTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawPointTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
// 颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 点符号
ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbol();
pSimpleMarkerSymbol.Color = pRgbColor;
pSimpleMarkerSymbol.Size = 10;
pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
// 点元素
IMarkerElement pMarkerElement = new MarkerElement() as IMarkerElement;
pMarkerElement.Symbol = pSimpleMarkerSymbol;
IElement pElement = pMarkerElement as IElement;
pElement.Geometry = pPoint;
// 绘制点
IActiveView pActiveView = m_hookHelper.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawPointTool.OnMouseMove implementation
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawPointTool.OnMouseUp implementation
}
#endregion
}
}
2、绘制线
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
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;
namespace WindowsFormsApplication1.Utility
{
/// <summary>
/// Summary description for DrawPolylineTool.
/// </summary>
[Guid("a6ab0371-ff13-4b44-b470-b06dcd8293d6")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Utility.DrawPolylineTool")]
public sealed class DrawPolylineTool : 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 INewLineFeedback m_NewLineFeedback;
public DrawPolylineTool()
{
//
// 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 DrawPolylineTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawPolylineTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewLineFeedback == null)
{
m_NewLineFeedback = new NewLineFeedback();
m_NewLineFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_NewLineFeedback.Start(pPoint);
}
else
{
m_NewLineFeedback.AddPoint(pPoint);
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewLineFeedback != null)
{
m_NewLineFeedback.MoveTo(pPoint);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawPolylineTool.OnMouseUp implementation
}
public override void OnDblClick()
{
if (m_NewLineFeedback != null)
{
IPolyline pPolyline = m_NewLineFeedback.Stop();
m_NewLineFeedback = null;
// 颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 0;
pRgbColor.Green = 255;
pRgbColor.Blue = 0;
// 线符号
ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
pSimpleLineSymbol.Color = pRgbColor;
pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDashDotDot;
pSimpleLineSymbol.Width = 5;
// 线元素
ILineElement pLineElement = new LineElement() as ILineElement;
pLineElement.Symbol = pSimpleLineSymbol;
IElement pElement = pLineElement as IElement;
pElement.Geometry = pPolyline;
// 绘制线
IActiveView pActiveView = m_hookHelper.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
#endregion
}
}
3、绘制多边形
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
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;
namespace WindowsFormsApplication1.Utility
{
/// <summary>
/// Summary description for DrawPolygonTool.
/// </summary>
[Guid("926a3cff-e646-459d-9794-5ff255d5c854")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Utility.DrawPolygonTool")]
public sealed class DrawPolygonTool : 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 INewPolygonFeedback m_NewPolygonFeedback;
public DrawPolygonTool()
{
//
// 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 DrawPolygonTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawPolygonTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewPolygonFeedback == null)
{
m_NewPolygonFeedback = new NewPolygonFeedback();
m_NewPolygonFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_NewPolygonFeedback.Start(pPoint);
}
else
{
m_NewPolygonFeedback.AddPoint(pPoint);
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewPolygonFeedback != null)
{
m_NewPolygonFeedback.MoveTo(pPoint);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawPolygonTool.OnMouseUp implementation
}
public override void OnDblClick()
{
if (m_NewPolygonFeedback != null)
{
IPolygon pPolygon = m_NewPolygonFeedback.Stop();
m_NewPolygonFeedback = null;
// 颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 0;
pRgbColor.Green = 0;
pRgbColor.Blue = 255;
// 多边形符号
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 = pPolygon;
// 绘制多边形
IActiveView pActiveView = m_hookHelper.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
#endregion
}
}
4、绘制矩形
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
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;
namespace WindowsFormsApplication1.Utility
{
/// <summary>
/// Summary description for DrawRectangleTool.
/// </summary>
[Guid("884ee227-8201-453f-bff4-59fd0d26dd49")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Utility.DrawRectangleTool")]
public sealed class DrawRectangleTool : 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 INewRectangleFeedback m_NewRectangleFeedback;
private bool ok;
public DrawRectangleTool()
{
//
// 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 DrawRectangleTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawRectangleTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewRectangleFeedback == null)
{
m_NewRectangleFeedback = new NewRectangleFeedback() as INewRectangleFeedback;
m_NewRectangleFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_NewRectangleFeedback.Start(pPoint);
}
else
{
if (ok)
{
IGeometry pGeometry = m_NewRectangleFeedback.Stop(pPoint);
m_NewRectangleFeedback = null;
// 颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 255;
pRgbColor.Blue = 0;
// 矩形符号
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.Color = pRgbColor;
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSHorizontal;
// 矩形元素
IFillShapeElement pFillShapeElement = new RectangleElement() as IFillShapeElement;
pFillShapeElement.Symbol = pSimpleFillSymbol;
IElement pElement = pFillShapeElement as IElement;
pElement.Geometry = pGeometry;
// 绘制矩形
IActiveView pActiveView = m_hookHelper.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else
{
m_NewRectangleFeedback.SetPoint(pPoint);
}
ok = !ok;
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewRectangleFeedback != null)
{
m_NewRectangleFeedback.MoveTo(pPoint);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
}
#endregion
}
}
5、绘制圆形
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
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;
namespace WindowsFormsApplication1.Utility
{
/// <summary>
/// Summary description for DrawCircleTool.
/// </summary>
[Guid("a3ee1c26-6023-4dc6-81a2-520967d7a724")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Utility.DrawCircleTool")]
public sealed class DrawCircleTool : 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 INewCircleFeedback m_NewCircleFeedback;
public DrawCircleTool()
{
//
// 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 DrawCircleTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawCircleTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewCircleFeedback == null)
{
m_NewCircleFeedback = new NewCircleFeedback();
m_NewCircleFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_NewCircleFeedback.Start(pPoint);
}
else
{
ICircularArc pCircularArc = m_NewCircleFeedback.Stop();
m_NewCircleFeedback = null;
// 片段集合
object missing = Type.Missing;
ISegmentCollection pSegmentCollection = new Ring() as ISegmentCollection;
pSegmentCollection.AddSegment(pCircularArc as ISegment, ref missing, ref missing);
// 转换为环
IRing pRing = pSegmentCollection as IRing;
pRing.Close();
// 转换多边形
IGeometryCollection pGeometryCollection = new Polygon() as IGeometryCollection;
pGeometryCollection.AddGeometry(pRing, ref missing, ref missing);
IPolygon pPolygon = pGeometryCollection as IPolygon;
// 颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 255;
// 圆形符号
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 = pPolygon;
// 绘制圆形
IActiveView pActiveView = m_hookHelper.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewCircleFeedback != null)
{
m_NewCircleFeedback.MoveTo(pPoint);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawCircleTool.OnMouseUp implementation
}
#endregion
}
}
6、绘制椭圆
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
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;
namespace WindowsFormsApplication1.Utility
{
/// <summary>
/// Summary description for DrawEllipseTool.
/// </summary>
[Guid("c3c840c6-a4a0-405c-a6be-a42a7472d90c")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Utility.DrawEllipseTool")]
public sealed class DrawEllipseTool : 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 INewEllipseFeedback m_NewEllipseFeedback;
private bool ok;
public DrawEllipseTool()
{
//
// 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 DrawEllipseTool.OnCreate implementation
}
/// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DrawEllipseTool.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewEllipseFeedback == null)
{
m_NewEllipseFeedback = new NewEllipseFeedback() as INewEllipseFeedback;
m_NewEllipseFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_NewEllipseFeedback.Start(pPoint);
}
else
{
if (ok)
{
IGeometry pGeometry = m_NewEllipseFeedback.Stop(pPoint);
m_NewEllipseFeedback = null;
// 颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 0;
pRgbColor.Green = 255;
pRgbColor.Blue = 255;
// 椭圆符号
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.Color = pRgbColor;
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
// 椭圆元素
IFillShapeElement pFillShapeElement = new EllipseElement() as IFillShapeElement;
pFillShapeElement.Symbol = pSimpleFillSymbol;
IElement pElement = pFillShapeElement as IElement;
pElement.Geometry = pGeometry;
// 绘制椭圆
IActiveView pActiveView = m_hookHelper.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else
{
m_NewEllipseFeedback.SetPoint(pPoint);
}
ok = !ok;
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_NewEllipseFeedback != null)
{
m_NewEllipseFeedback.MoveTo(pPoint);
}
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
// TODO: Add DrawEllipseTool.OnMouseUp implementation
}
#endregion
}
}
7、主界面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
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 WindowsFormsApplication1.Utility;
namespace WindowsFormsApplication1
{
public partial class MinForm : Form
{
public MinForm()
{
InitializeComponent();
}
// 放大
private void btnZoomIn_Click(object sender, EventArgs e)
{
ICommand command = new ControlsMapZoomInTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 缩小
private void btnZoomOut_Click(object sender, EventArgs e)
{
ICommand command = new ControlsMapZoomOutTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 漫游
private void btnPan_Click(object sender, EventArgs e)
{
ICommand command = new ControlsMapPanTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 全图
private void btnFullExtent_Click(object sender, EventArgs e)
{
ICommand command = new ControlsMapFullExtentCommand();
command.OnCreate(axMapControl1.Object);
command.OnClick();
}
// 画点
private void btnDrawPoint_Click(object sender, EventArgs e)
{
ICommand command = new DrawPointTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 画线
private void btnDrawPolyline_Click(object sender, EventArgs e)
{
ICommand command = new DrawPolylineTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 画多边形
private void btnDrawPolygon_Click(object sender, EventArgs e)
{
ICommand command = new DrawPolygonTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 画矩形
private void btnDrawRectangle_Click(object sender, EventArgs e)
{
ICommand command = new DrawRectangleTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 画圆
private void btnDrawCircle_Click(object sender, EventArgs e)
{
ICommand command = new DrawCircleTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 画椭圆
private void btnDrawEllipse_Click(object sender, EventArgs e)
{
ICommand command = new DrawEllipseTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
}
}
上一篇: python基础--字符串、列表、元组、集合、字典
下一篇: 简单IO接口作业