将代码写在Mapcontrol的OnMouseDown事件中,具体代码如下:
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
//实例化一个点
IPoint pPoint = new PointClass();
//以该点作拓扑算子
ITopologicalOperator pTopologicalOperator=pPoint as ITopologicalOperator;
//将点击的位置坐标赋予pPoint
pPoint.PutCoords(e.mapX, e.mapY);
//以缓冲半径为0进行缓冲 得到一个点
IGeometry pGeometry = pTopologicalOperator.Buffer(0);
//以该点进行要素选择(只能选中面状要素,点和线无法选中)
axMapControl1.Map.SelectByShape(pGeometry, null, false);
//刷新视图
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
//IMap.FeatureSelection默认只返回 shape 信息,可以通过 IEnumFeatureSetup::AllFields 方法来设置允许返回属性
// 获取选择集
ISelection pSelection = axMapControl1.Map.FeatureSelection;
// 打开属性标签
IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
pEnumFeatureSetup.AllFields = true;
// 获取要素
IEnumFeature pEnumFeature = pSelection as IEnumFeature;
IFeature pFeature = pEnumFeature.Next();
while (pFeature != null)
{
double area = 0;
double mu = 0;
if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
{
//计算面积
IArea pArea = pFeature.Shape as IArea;
area = area + pArea.Area;//得到的面积单位是平方米
mu = area * 0.0015;//转换为亩
}
textBox1.Text = Convert.ToString(Math.Round(area,6));
textBox2.Text = Convert.ToString(Math.Round(mu, 6));
break;
}
}