TOCControl右键菜单功能实现

利用ArcEngine进行GIS软件开发时,TOCControl控件本身没有提供右键菜单功能,需要开发人员编写代码实现,具体实现有如下两种方式:

    1. 利用C#自带的contextMenuStrip来实现;

       ①在contextMenuStrip的OnClick事件中编写相应显示代码;
       ②在axTOCControl的OnMouseDown事件中将对应事件与axTOCControl挂钩;

       contextMenuStrip.Show(axTOCControl1, new Point(e.x, e.y));

    2. 利用AE自定义菜单功能实现;

       ①自定义OpenAttributeTable类,基于BaseCommand

       TOCControl右键菜单功能实现
在新创建的OpenAttributeTable中重写OnClick事件

using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Data;

namespace 校园信息管理系统
{
    /// <summary>
    /// Summary description for OpenAttributeTable.
    /// </summary>
    [Guid("a87c4340-dc1e-4210-a557-e8cbadedb6fb")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("校园信息管理系统.OpenAttributeTable")]
    public sealed class OpenAttributeTable : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private ILayer m_layer;

        public OpenAttributeTable(ILayer pLayer)//构造函数,用来传参
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "打开属性表"; //localizable text
            base.m_caption = "打开属性表";  //localizable text
            base.m_message = "打开属性表";  //localizable text
            base.m_toolTip = "打开属性表";  //localizable text
            base.m_name = "打开属性表";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")
            m_layer = pLayer;

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;
            // TODO:  Add other initialization code
        }
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()//重载Click函数,实现自定义功能
        {
            // TODO: Add OpenAttributeTable.OnClick implementation
            IFeatureLayer pFeatureLayer = m_layer as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            DataTable dt = new DataTable();
            if (pFeatureClass != null)
            {
                DataColumn dc;
                for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
                {
                    dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);
                    dt.Columns.Add(dc);
                }
                IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
                IFeature pFeature = pFeatureCursor.NextFeature();
                DataRow dr;
                while (pFeature != null)
                {
                    dr = dt.NewRow();
                    for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
                    {
                        if (pFeatureClass.FindField(pFeatureClass.ShapeFieldName) == j)
                        {
                            dr[j] = GeoMapLoad.getShapeType(pFeatureClass.ShapeType);
                        }
                        else
                        {
                            dr[j] = pFeature.get_Value(j).ToString();
                        }
                    }
                    dt.Rows.Add(dr);
                    pFeature = pFeatureCursor.NextFeature();
                }
                AttributeTable attributeTable = new AttributeTable();
                attributeTable.Show();
                attributeTable.AttributeTableDataGridView.DataSource = dt;
            }
          
        }

        #endregion
    }
}

       ②在MainFrm中将新创建的菜单与axMapControl挂钩
        创建全局变量

        private ITOCControl2 m_TOCControl;
        private IToolbarMenu m_menuLayer;
        在窗体加载函数中初始化

        m_TOCControl = (ITOCControl2)axTOCControl1.Object;
        m_menuLayer = new ToolbarMenuClass();
        m_menuLayer.SetHook(axMapControl1);

        在TOCControl控件的OnMouseDown事件中弹出菜单

        if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
        {
              m_menuLayer.AddItem(new OpenAttributeTable(pLayer), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);
              m_menuLayer.PopupMenu(e.x, e.y, m_TOCControl.hWnd);

//如果不传参,这句话可以放在窗体加载事件中,放在这要注意移除菜单项,否则每点击一次添加相同的菜单项
              m_menuLayer.RemoveAll();
        }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值