axtoccontrol右键菜单

第一步:右键菜单参数以及初始化

//右键菜单
        //  private ITOCControl2 pTocControl;
        private IMapControl3 pMapControl;
        // private IToolbarMenu pToolMenuMap;
        private IToolbarMenu pToolMenuLayer;
        private void rightMenu()
        {
            // 取得 MapControl 和 PageLayoutControl 的引用   
            ///  pTocControl = (ITOCControl2)axTOCControl1.Object;
            pMapControl = (IMapControl3)axMapControl1.Object;
            // 创建菜单   
            ///pToolMenuMap = new ToolbarMenuClass();
            pToolMenuLayer = new ToolbarMenuClass();
            pToolMenuLayer.AddItem(new zoomtolayer(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
            pToolMenuLayer.AddItem(new Class1(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
            pToolMenuLayer.AddItem(new 属性表(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
            pToolMenuLayer.SetHook(pMapControl);
        }

第二步:

添加axtoccontrol右击事件代码

private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button != 2) return;
            rightMenu();
            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap map = null; ILayer layer = null;
            object other = null; object index = null;            
            this.axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);            
            if (item == esriTOCControlItem.esriTOCControlItemMap)
                this.axTOCControl1.SelectItem(map, null);            
            else if (item == esriTOCControlItem.esriTOCControlItemLayer)
                this.axTOCControl1.SelectItem(layer, null);    
            axMapControl1.CustomProperty = layer;            
            if (item == esriTOCControlItem.esriTOCControlItemMap)
            {
                MessageBox.Show("点击了Layers");
            }
            if (item == esriTOCControlItem.esriTOCControlItemLayer)
            {
                pToolMenuLayer.PopupMenu(e.x, e.y, this.axTOCControl1.hWnd);
            }  
        }

第三步:

以属性表为例,展示basecommand类这个的代码

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
namespace 创建与调用aoi书签
{
    class 属性表 : BaseCommand
    {        
        private IMapControl3 pMapControl;
        public 属性表()
        {
            base.m_caption = "属性表";
        }
        public override void OnClick()
        {
            openattribute();
        }
        public override void OnCreate(object hook)
        {
            pMapControl = (IMapControl3)hook;
        }
        private void openattribute()
        {
            ILayer pLayer = (ILayer)pMapControl.CustomProperty;
            databoard databoard = new databoard(pLayer);
            databoard.Show();
        }
    }
}

第四步、属性表窗体

public partial class AttributeTable : Form
    {
        private IFeatureLayer pFeatureLayer = null;
        public AttributeTable(IFeatureLayer _FeatureLayer)
        {
            InitializeComponent();
            pFeatureLayer = _FeatureLayer;
        }
 
        private void AttributeTable_Load(object sender, EventArgs e)
        {
            
            IFields pFields;  
            pFields = pFeatureLayer.FeatureClass.Fields;//连接属性表的列
 
            dataGridView1.ColumnCount = pFields.FieldCount;
            for (int i = 0; i < pFields.FieldCount; i++)
            {
                string fldName = pFields.get_Field(i).Name;//字段名
                dataGridView1.Columns[i].Name = fldName;
                dataGridView1.Columns[i].ValueType = Type.GetType(ParseFieldType(pFields.get_Field(i).Type));
                //列名和值类型
            }
 
            IFeatureCursor pFeatureCursor;//定义游标/指针
            pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
 
            IFeature pFeature;
            pFeature = pFeatureCursor.NextFeature();
 
            while (pFeature != null)
            {
                string[] fldValue = new string[pFields.FieldCount];
                for (int i = 0; i < pFields.FieldCount; i++)
                {
                    string fldName;
                    fldName = pFields.get_Field(i).Name;
                    //下代码判断是数值类型还是几何类型字段
                    if (fldName == pFeatureLayer.FeatureClass.ShapeFieldName)
                    {
                        fldValue[i] = pFeature.Shape.GeometryType.ToString().Remove(0,12);
                    }
                    else
                        fldValue[i] = pFeature.get_Value(i).ToString();
                }
                dataGridView1.Rows.Add(fldValue);//写入行值
                pFeature = pFeatureCursor.NextFeature();
            }
 
        }
        private string ParseFieldType(esriFieldType TableFieldType)
        {
            switch (TableFieldType)
            {
                case esriFieldType.esriFieldTypeBlob:
                    return "System.String";
                case esriFieldType.esriFieldTypeDate:
                    return "System.DateTime";
                case esriFieldType.esriFieldTypeDouble:
                    return "System.Double";
                case esriFieldType.esriFieldTypeGeometry:
                    return "System.String";
                case esriFieldType.esriFieldTypeGlobalID:
                    return "System.String";
                case esriFieldType.esriFieldTypeGUID:
                    return "System.String";
                case esriFieldType.esriFieldTypeInteger:
                    return "System.Int32";
                case esriFieldType.esriFieldTypeOID:
                    return "System.String";
                case esriFieldType.esriFieldTypeRaster:
                    return "System.String";
                case esriFieldType.esriFieldTypeSingle:
                    return "System.Single";
                case esriFieldType.esriFieldTypeSmallInteger:
                    return "System.Int32";
                case esriFieldType.esriFieldTypeString:
                    return "System.String";
                default:
                    return "System.String";
            }
        }
    }

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GIS程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值