创建一个ArcMap基本界面,具体过程戳 —>基于C#的ArcEngine二次开发——创建一个基本的ArcMap界面
在Form1.cs中输入以下代码:
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.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace arc_engine获取选中要素的属性表
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
//1.判断是否导入了地图,即图层个数是否大于0
if (axMapControl1.LayerCount > 0)
{
//2.选中要素
//实例化一个点
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);
//3.判断是否选中要素
// 获取选择集
ISelection pSelection = axMapControl1.Map.FeatureSelection;
// 打开属性标签
IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
pEnumFeatureSetup.AllFields = true;
// 获取要素
IEnumFeature pEnumFeature = pSelection as IEnumFeature;
IFeature pFeature = pEnumFeature.Next();
if (pFeature == null)
MessageBox.Show("请选择有效区域!");
while (pFeature != null)
{
string field = pFeature.get_Value(9).ToString();//获取第10个字段的值
MessageBox.Show("此区域为:" + field);
break;
}
}
else
MessageBox.Show("请导入地图!");
}
}
}
运行结果如下:
小生不才,若有错误还请不吝赐教!!