C# ArcEngine TOCControl上实现右键

第一种方法:使用contextMenuStrip控件

1.新建一个窗体AttributeTable,并定义一个全局变量mLayer,让主窗体里面的axMapControl1的layer传进来,从而获取属性列表!

       ILayer mLayer;
        public AttributeTable(ILayer layer)
        {
            InitializeComponent();
            mLayer = layer;
        }

在AttributteTable窗体的load事件下加载属性表

 private void AttributeTable_Load(object sender, EventArgs e)
        {
            IFeatureLayer pFeatureLayer = mLayer 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++)
                    {
                        //判断feature的形状
                        if (pFeature.Fields.get_Field(j).Name == "Shape")
                        {
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                            {
                                dr[j] = "点";
                            }
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                            {
                                dr[j] = "线";
                            }
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                            {
                                dr[j] = "面";
                            }
                        }
                        else
                        {
                            dr[j] = pFeature.get_Value(j).ToString();//增加行
                        }
                    }
                    dt.Rows.Add(dr);
                    pFeature = pFeatureCursor.NextFeature();
                }
                dataGridView1.DataSource = dt;
            }
        }

2.建立右键菜单(本例只是做了打开属性表操作),入下图:

在主窗口内定义一个公共变量:public ILayer layer;

在axTOCControl1_OnMouseDown事件下

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap map = new MapClass();
                layer = new FeatureLayerClass();
                object other = new object();
                object index = new object();
                axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index); //实现赋值,ref的参数必须初始化
                if (item == esriTOCControlItem.esriTOCControlItemLayer) ////点击的是图层的话,就显示右键菜单
                {
                    contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
                }
            }
        }

3.单击contextMenuStrip1,显示属性表。

 private void contextMenuStrip1_Click(object sender, EventArgs e)
        {
            AttributeTable attributeTable = new AttributeTable(layer);
            attributeTable.Text = "属性表:" + layer.Name;
            attributeTable.ShowDialog();
        }

效果如下:

第一种方法:使用Base Command 类

1.新建一个Base Command类OpenAttributeTable,并定义一个全局变量 private ILayer m_layer,代码如下:

        private ILayer m_layer;
        public OpenAttributeTable(ILayer pLayer)
        {
            base.m_category = "";
            base.m_caption = "打开属性表";
            base.m_message = "";
            base.m_toolTip = "";
            base.m_name = "";   

            m_layer = pLayer;
            try
            {//加载附加图片
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

然后在其单击事件下面加载属性表,代码如下:

public override void OnClick()
        {
            // 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 (pFeature.Fields.get_Field(j).Name == "Shape")
                        {
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                            {
                                dr[j] = "点";
                            }
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                            {
                                dr[j] = "线";
                            }
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                            {
                                dr[j] = "面";
                            }
                        }
                        else
                        {
                            dr[j] = pFeature.get_Value(j).ToString();
                        }
                    }
                    dt.Rows.Add(dr);
                    pFeature = pFeatureCursor.NextFeature();
                }
                AttributeTable AT = new AttributeTable();
                AT.Text = "属性表:" + pFeatureLayer.Name;
                AT.Show();
                AT.dataGridView1.DataSource = dt;
            }
        }

2. 在axTOCControl1_OnMouseDown事件下完成单击事件,代码如下:

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap map = new MapClass();
                layer = new FeatureLayerClass();
                object other = new object();
                object index = new object();
                axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
                if (item == esriTOCControlItem.esriTOCControlItemLayer)
                {
                    m_ToolMenuLayer.AddItem(new Owntolayer(layer, axMapControl1, eve), 0, 0, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
                    m_ToolMenuLayer.AddItem(new FullExtent(axMapControl1), 0, 1, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
                    m_ToolMenuLayer.AddItem(new DeleteLayer(layer), 0, 2, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
                    m_ToolMenuLayer.AddItem(new OpenAttributeTable(layer), 0, 3, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
                    m_ToolMenuLayer.PopupMenu(e.x, e.y, m_TocControl.hWnd);
                    m_ToolMenuLayer.RemoveAll();
                }
            }
        }

效果如下:

说明:

1.public int AddItem (object item,int SubType,int index,bool beginGroup,esriCommandStyles Style)

第一个参数:菜单项的内容,功能实现。
第二个参数:对于一个工具定义多个 type 的时候,才会用到,每一个 int 代表一个新的实现。
第三个参数:索引值,在菜单项上面显示的位置。默认为 -1,按书写顺序排序。
第四个参数:是否开始一个新组,就是在其上面有一个“——”的效果。
第五个参数:显示样式。

2. axTOCControl1.HitTest()方法

[C#]public void HitTest (
    intX,
    intY,
    ref esriTOCControlItemItemType,
    ref IBasicMapBasicMap,
    ref ILayerLayer,
    ref objectUnk,
    ref objectData);

Product Availability

Available with ArcGIS Engine.

Description

x is the X coordinate, in pixels, where the mouse button was pressed referenced against the origin (0, 0) of the TOCControl (the top left hand corner).

y is the Y coordinate, in pixels, where the mouse button was pressed referenced against the origin (0, 0) of the TOCControl (the top left hand corner).

ItemType specifies an enumeration indicating the type of item (none, map, layer, heading or legend class).

Map specifies an IMap object.

Layer specifies an ILayer object.

Unk specifies an ILegendGroup object.

Data specifies a long indicating the index of the legend class within the legend group. Use this index in conjunction with the legend group to obtain a particular legend class. An index of -1 refers to the heading if it is present.

注:本文所用的环境:windows7操作系统;VS2010;基于C#语言;ArcEngine版本为10.1。

时间: 2024-08-25 00:21:28

C# ArcEngine TOCControl上实现右键的相关文章

TOCControl上实现右键

第一步:新建另外一个窗体 首先要定义一个全局变量 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; u

在ListCtrl上添加右键菜单,并创建响应函数。

一:添加右键菜单(在选中的行上面添加右键响应菜单) 1.首先创建一个菜单资源,在该项目上右键,添加资源,菜单资源,新建一个菜单资源(不会显示顶级菜单,只显示次级菜单). 2.在自己新添加的类CMyListCtrl(该类继承自CListCtrl,并且该类类型的对象与ListCtrl资源进行了绑定),并在该类的消息中添加=NM_RCLICK消息,在它的消息处理函数中, 添加右键弹出菜单代码: CMenu menu; menu.LoadMenuW(IDR_MENU1); //加载菜单资源 CMenu*

在XP、Win7/8上如何右键进入命令行

在Win7/8上特别简单,只需要在按下shift键后,再点击鼠标右键,即可进入命令行界面. 在xp下,需要修改注册表了.也同样适用于Win7/8

Arcengine 二次开发添加右键菜单

最近在搞arcengine 二次开发,遇到了好多问题,也通过网上查资料试着慢慢解决了,把解决的步骤记录下来,有需要帮助的可以看一下,也欢迎各位来批评指正. 想给自己的map application在图层上添加右键菜单,谷歌了一下,找到了解决的方法,原文的地址edndoc.esri.com/arcobjects/9.2/NET/1ED14BF2-A0E3-4e56-A70D-B9A7F7EC7880.htm.然后我根据这个添加了自己的右键菜单,又有一些改动. 效果如图所示(有点简陋),仅仅是简单的

WIN7系统的桌面突然不好使了,右键没反应,桌面上所有的图标点击都没反应explorer

就是重启一下 explorer 在任务栏上点右键> 启动任务管理器> 进程> 结束explorer.exe> 文件> 新建任务> 浏览> explorer.exe>

导入Maven项目,右键Team下功能缺失

1.从Git Repositories界面下import Projects,重新导入项目 2.在导入的项目上,右键-->Configure-->Convert To Maven Project

WPF--鼠标右键菜单中的Command命令实现

一个功能,在ListView中的ListBoxItem控件上实现右键菜单关闭选项,将该ListBoxItem从ListView中删除. 利用 RoutedCommand类创建Command命令,MSDN上将其定义为一个实现 ICommand 并在元素树之内进行路由的命令. C#代码: private RoutedCommand closeCmd = new RoutedCommand("Clear", typeof(MainWindow)); private void ListBoxI

使用VS GDB扩充套件在VS上远端侦错Linux上的C/C++程序

在 Linux 上开发 C/C++ 程序,或许你会直接(本机或远端)登入 Linux,打开编辑器写完代码后,就用 gcc/g++ 来编译,遇到要除错(debug)的时候,则会选择使用 gdb 来进行除错.现在,如果你刚好也很喜欢 Visual Studio,你可以不必改变习惯,用 Visual Studio写程式.然后远端送到 Linux 上编译.甚至还能接上 gdb 来除错.这个对于开发像是嵌入式系统.或是 IoT 装置的程序等等就可以多多利用 Visual Studio 强大的 IDE 能力

在Windows Server 2008上部署SVN代码管理总结

这段时间在公司开发Flex程序,所以使用TortoiseSVN作为团队代码管理器,今天在公司服务器上部署SVN服务器,并实验成功,总结如下: 服务器环境: 操作系统:Windows Server 2008: SVN服务器端程序:VisualSVN Server 2.1.5:(http://www.visualsvn.com/ ) 客户端环境: 操作系统:Windows 7 32Bit: SVN客户端程序:TortoiseSVN-1.6.12.20536-win32-svn-1.6.15:(htt