在private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)中添加一下代码片段:
case "DrawPolygon":
IActiveView pActiveView = axMapControl1.ActiveView;
IRubberBand pRubberBand;
IRgbColor pRgbColor;
IPolygonElement pPolygonElement;
ISimpleFillSymbol pSimpleFillSymbol;
IPolygon pPolygon;
pSimpleFillSymbol = new SimpleFillSymbolClass();
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;//面的样式
pRgbColor = new RgbColorClass();
pRgbColor.Red = 250;
pSimpleFillSymbol.Color = pRgbColor;
pRubberBand = new RubberPolygonClass();
pPolygonElement = new PolygonElementClass();
pPolygon = pRubberBand.TrackNew(pActiveView.ScreenDisplay, pSimpleFillSymbol as ISymbol) as IPolygon;
// 获取一条线上的点
IPointCollection myPoints = (pPolygon as IPointCollection);
List<IPoint> pList = new List<IPoint>();
for (int t = 0; t < myPoints.PointCount; t++)
{
pList.Add(myPoints.Point[t]);
//pList.Insert(0, myPoints.Point[t]);
double xx = myPoints.Point[t].X;// X坐标
double yy = myPoints.Point[t].Y;// Y坐标
}
ILayer pLayer = GetLayerByName("MyDraw"); // 获取MyDraw图层
IFeatureLayer pFeatureLyr = pLayer as IFeatureLayer; //将ILayer转换为IFeaturelayer,为了对图层上的要素进行编辑
IFeatureClass pFeatCls = pFeatureLyr.FeatureClass; //定义一个要素集合,并获取图层的要素集合
IFeatureClassWrite fr = (IFeatureClassWrite)pFeatCls; //定义一个实现新增要素的接口实例,并该实例作用于当前图层的要素集
IWorkspaceEdit w =
(pFeatCls as IDataset).Workspace as IWorkspaceEdit; //定义一个工作编辑工作空间,用于开启前图层的编辑状态
IFeature f; //定义一个IFeature实例,用于添加到当前图层上
w.StartEditing(true); //开启编辑状态
w.StartEditOperation(); //开启编辑操作
// 删除图层上所有要素
ITable pTable = pFeatCls as ITable;
pTable.DeleteSearchedRows(null);
// 添加 个多边形要素
f = pFeatCls.CreateFeature(); //实例化IFeature对象, 这样IFeature对象就具有当前图层上要素的字段信息
f.Shape = pPolygon; //设置IFeature对象的形状属性
f.Store(); //保存IFeature对象
fr.WriteFeature(f); //将IFeature对象,添加到当前图层上
w.StopEditOperation(); //停止编辑操作
w.StopEditing(true); //关闭编辑状态,并保存修改
axMapControl1.Refresh();//刷新地图
break;