TOCControl上实现右键

时间:2022-02-04 23:52:48

第一步:新建另外一个窗体

  • 首先要定义一个全局变量 ILayer。
  • 窗体要带参数,以便将 ILayer 传递过来。
  • 获取属性列表。
    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; namespace MyGIS
    {
    public partial class Property : Form
    {
    ILayer pLayer;
    public Property(ILayer layer)
    {
    InitializeComponent();
    pLayer = layer;
    } private void Property_Load(object sender, EventArgs e)
    {
    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
    IFeature pFeature = pFeatureCursor.NextFeature();
    IFields pFields = pFeatureClass.Fields; DataTable pDataTable = new DataTable();
    for (int i = ; i < pFields.FieldCount; i++)//获取所有列
    {
    DataColumn pColumn = new DataColumn(pFields.get_Field(i).Name);
    pDataTable.Columns.Add(pColumn);
    }
    while (pFeature != null)
    {
    DataRow pRow = pDataTable.NewRow();
    for (int i = ; i < pFields.FieldCount; i++)
    {
    pRow[i] = pFeature.get_Value(i);
    }
    pDataTable.Rows.Add(pRow);
    pFeature = pFeatureCursor.NextFeature();
    }
    dataGridView1.DataSource = pDataTable; }
    }
    }

    第二步:建立右键菜单项

    • 添加一个 ContextMenuStrip 控件,然后增加几个菜单项。
    • 特别注意加入一个菜单项为“显示属性”。
    • 判断在什么情况下显示右键菜单。
       ILayer pGlobalFeatureLayer;
      private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
      {
      if (e.button == )
      {
      esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
      IBasicMap pBasicMap = null;
      ILayer pLayer = null;
      object other = null;
      object index = null;
      axTOCControl1.HitTest(e.x, e.y, ref item, ref pBasicMap, ref pLayer, ref other, ref index);
      pGlobalFeatureLayer = pLayer;//实现赋值
      if (item == esriTOCControlItem.esriTOCControlItemLayer)//点击是图层的话,就显示右键菜单
      {
      contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));//显示右键菜单,并定义其位置,正好在鼠标处显示
      }
      }
      } private void 显示属性ToolStripMenuItem_Click(object sender, System.EventArgs e)
      {
      Property frm = new Property(pGlobalFeatureLayer);
      frm.Text = "Attributies of" + pGlobalFeatureLayer.Name;
      frm.ShowDialog();
      }