基于C#的AE二次开发之图层右键菜单打开属性表及图层相关操作

28 篇文章 16 订阅
24 篇文章 44 订阅

基于C#的AE二次开发之图层右键菜单打开属性表及图层相关操作

我的开发环境为ArcGIS Engine 10.2与Visual studio2010。主地图名称为axMapControl1,Toc目录名为axTOCControl1。(注意相关事件的添加与动态链接库的引入)!

  • 效果预览

  • 实现方法
  1. 引入contextMenuStrip工具,并添加菜单栏工具目录。我这里有打开属性表、图层可选、图层不可选、缩放至图层、移除图层这几个功能。

  2. 在解决方案中,创建属性表窗体OpenAtrributeForm.cs,添加工具箱数据栏中的DataGridView组件,命名为dataGridView1,调整好大小后,写入相关实现代码。(建议选择性地复制)

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.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace MyMapControlApplication01.Attribute
{
    public partial class OpenAtrributeForm : Form
    {
        public OpenAtrributeForm()
        {
            InitializeComponent();
        }
		//获取主窗体传入的图层
        private IFeatureLayer currentFeatureLayer;

        public IFeatureLayer CurrentFeatureLayer
        {
            get { return currentFeatureLayer; }
            set { currentFeatureLayer = value; }
        }
        //获取主窗体传入的地图
        private IMap currentMap;

        public IMap CurrentMap
        {
            get { return currentMap; }
            set { currentMap = value; }
        }

		//定义窗体显示方法,等待调用
        public void InitUI()
        {
            if (currentFeatureLayer == null) return;
            DataTable pTable = new DataTable();
            //查询所有的列标题
            for (int i = 0; i < currentFeatureLayer.FeatureClass.Fields.FieldCount; i++)
            {
                pTable.Columns.Add(currentFeatureLayer.FeatureClass.Fields.get_Field(i).AliasName,Type.GetType("System.Object"));
            }
            //查询要素类中的所有的要素
            IFeatureCursor pCursor = currentFeatureLayer.Search(null, false);
            IFeature pFeature = pCursor.NextFeature();
            //遍历游标查询要素类中的所有要素的字段信息
            while (pFeature != null)
            {
                DataRow pRow = pTable.NewRow();
                for (int k = 0; k < pFeature.Fields.FieldCount; k++)
                {
                	//将Shape列字段,改为对应的类型。默认显示的是代码类型
                    if (pFeature.Fields.get_Field(k).Name == "Shape")
                    {
                        if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
                        {
                            pRow[k] = "点要素";
                        }
                        else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                        {
                            pRow[k] = "线要素";
                        }
                        else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                        {
                            pRow[k] = "面要素";
                        }
                    }
                    else
                    {
                        pRow[k] = pFeature.get_Value(k).ToString();
                    }                  
                }
                pTable.Rows.Add(pRow);
                pFeature = pCursor.NextFeature();
            }
            //将查询到的数据添加到窗体表格中
            dataGridView1.DataSource = pTable;
        }

        #region 高亮显示并缩放至要素
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            DataGridViewRow pRow = dataGridView1.Rows[e.RowIndex];
            int index = Convert.ToInt32(pRow.Cells[0].Value);
            IFeature pFeature = currentFeatureLayer.FeatureClass.GetFeature(index);
            IEnvelope pEnvelope = new Envelope() as IEnvelope;
            pFeature.Shape.QueryEnvelope(pEnvelope);
            IActiveView pView = currentMap as IActiveView;
            pView.FocusMap.ClearSelection();
            IFeatureSelection pSelection = currentFeatureLayer as IFeatureSelection;
            pSelection.Add(pFeature);
            pView.Extent = pEnvelope;
            pView.Refresh();
        }
        #endregion
    }
}

  1. 在地图所在的主窗体里,给添加Toc目录(axTOCControl1)添加OnMouseDown事件,写入如下代码。
  • 类库
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
  • 实现代码
#region TOCC右键菜单及其实现
private IFeatureLayer pToccFeatureLayer = null;//当前选择的要素类图层
private OpenAtrributeForm OpenAtrribute = null;//引入创建的属性表窗体
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
    //鼠标点击右键,触发contextMenuStrip的显示
    if (e.button == 2)
    {
        esriTOCControlItem pItem = esriTOCControlItem.esriTOCControlItemNone;
        IBasicMap pMap = null;
        ILayer pLayer = null;
        object unk = null;
        object data = null;
        axTOCControl1.HitTest(e.x, e.y, ref pItem, ref pMap, ref pLayer, ref unk, ref data);
        if (pItem == esriTOCControlItem.esriTOCControlItemLayer && pLayer != null)
        {
            pToccFeatureLayer = pLayer as IFeatureLayer;
            //如果不写图层可选或不可选栏目,下面的两行代码不是必须的
            图层可选ToolStripMenuItem.Enabled = ! pToccFeatureLayer.Selectable;
            图层不可选ToolStripMenuItem.Enabled = pToccFeatureLayer.Selectable;
            //contextMenuStrip的显示,出现在鼠标点击位置
            contextMenuStrip1.Show(Control.MousePosition);
        }
    }
}

//打开属性表窗体点击事件
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenAtrribute = new OpenAtrributeForm();
    OpenAtrribute.CurrentFeatureLayer = pToccFeatureLayer;
    OpenAtrribute.CurrentMap = axMapControl1.Map;
    OpenAtrribute.InitUI();
    OpenAtrribute.Show();
}

//要素类图层允许被选择功能
private void 图层可选ToolStripMenuItem_Click(object sender, EventArgs e)
{
    pToccFeatureLayer.Selectable = true;
    图层可选ToolStripMenuItem.Enabled = !图层不可选ToolStripMenuItem.Enabled;
}

//要素类图层不允许被选择功能
private void 图层不可选ToolStripMenuItem_Click(object sender, EventArgs e)
{
    pToccFeatureLayer.Selectable = false;
    图层可选ToolStripMenuItem.Enabled = !图层不可选ToolStripMenuItem.Enabled;
}

//移除要素类图层功能
private void 移除图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
    DialogResult result = MessageBox.Show("确定要移除[" + pToccFeatureLayer .Name+ "]图层吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
    if (result == DialogResult.OK)
    {
        axMapControl1.Map.DeleteLayer(pToccFeatureLayer);
    }
}

//缩放至要素类图层功能
private void 缩放至图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
    IEnvelope pEnvelope = pToccFeatureLayer.AreaOfInterest;
    axMapControl1.ActiveView.Extent = pEnvelope;
    axMapControl1.Refresh();
}

#endregion

感兴趣的小伙伴们快去试试吧,对于不明白的地方可以留言提问,博主看到后会第一时间解答。

  • 31
    点赞
  • 143
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值