第一种方法:使用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。

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

  1. TOCControl上实现右键

    第一步:新建另外一个窗体 首先要定义一个全局变量 ILayer. 窗体要带参数,以便将 ILayer 传递过来. 获取属性列表. using System; using System.Collecti ...

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

    在Win7/8上特别简单,只需要在按下shift键后,再点击鼠标右键,即可进入命令行界面.

  3. 如何在Macbook苹果笔记本上按右键点击(适用小米黑苹果)

    1.按下Control键.保持按下Control(Ctrl)键,同时点击鼠标. 这一操作相当于在一个双键鼠标上右击. 点击鼠标后,你可以松开Control键. 该方法适用于单键鼠标或者MacBook ...

  4. DevExpress的TreeList实现节点上添加自定义右键菜单并实现删除节点功能

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  5. DevExpress中GridView上的右键菜单

    1. 先拖一个PopupMenu和BarManage控件,设置PopupMenu的Manager属性为BarManager. 2. 先选中GridView,不是GridControl,在属性窗口中,选 ...

  6. 技巧:开启ubuntu系统桌面上的右键进入terminal命令行控制台功能

    $ sudo apt-get install nautilus-open-terminal 执行上述命令,重启. 重启命令: $ sudo reboot 注意:需要联网

  7. Arcengine 二次开发添加右键菜单

    最近在搞arcengine 二次开发,遇到了好多问题,也通过网上查资料试着慢慢解决了,把解决的步骤记录下来,有需要帮助的可以看一下,也欢迎各位来批评指正. 想给自己的map application在图 ...

  8. SkylineGlobe 6.6 三维地图上实现自定义右键菜单示例代码

    1.OnRButtonDown.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  9. AE安装部署以及监测ArcEngine runtime 9.3是否安装

    目的:用ArcEngine9.3开发项目以后,用Visual Studio2008打包工具打包: 同时监测别的机器上是否有ArcEngine Runtime或者Desktop的支持. 解决方案: 1. ...

随机推荐

  1. Mac 使用 launchctl 定时运行程序(转载)

    摘要: 在linux下可以用crontab来定时执行任务,在mac下可以用launchctl来定时执行任务. 在linux下可以用crontab来定时执行任务,在MAC下可以用launchctl来定时 ...

  2. PHP 使用 Memcached

    PHP 想往 Memcached 中增删查改数据,需要先安装 memcache 扩展模块: cd /usr/local/src wget http://www.apelearn.com/bbs/dat ...

  3. Linux 下 c 语言 聊天软件

    这是我学C语言写的第一个软件,是一个完整的聊天软件,里面包括客户端,和服务器端,可以互现聊天,共享文件,有聊天室等,是一个有TCP和UDP协议的聊天软件,测试过很多次在CENTOS和UBUNTU下都通 ...

  4. AngularJs HTML DOM、AngularJS 事件以及模块的学习(5)

    今天的基础就到了操作DOM,事件和模块的学习,其实我个人感觉学习起来AngularJS并没有想象中的那么的艰难,可能是因为这个太基础化吧,但是我们从初学开始就应该更加的自信一些,后来我可能会写一个小的 ...

  5. 状态保持以及AJAX的初步学习

    嘿嘿,今天学习的有点迷茫哦,主要学习把验证码使用在登录页面时间的一些逻辑,学习这个时间并没有那么的迷惑哦,可是自己写程序时间倒是有点反应迟钝,不过还好总是在最后搞清楚啦,另外就是一步一步的学习是接近项 ...

  6. 项目适配iOS9遇到的一些问题及解决办法(更新两个小问题)

    本文转载至 http://www.bubuko.com/infodetail-1110714.html http://www.jianshu.com/p/631bd7f12a38 1.网络请求报错.升 ...

  7. vc 关于局部刷新

    在绘制图像对象的时候,时刻获取其所占范围大小,并使用InvalidateRect( m_rectRefresh);刷新,但是光这样还是不行的要在onDraw()函数里获取PAINTSTRUCT结构的无 ...

  8. Linux设备驱动剖析之IIC(一)

    写在前面 由于IIC总线只需要两根线就可以完成读写操作,而且通信协议简单,一条总线上可以挂载多个设备,因此被广泛使用.但是IIC总线有一个缺点,就是传输速率比较低.本文基于Linux-2.6.36版本 ...

  9. <转>Python OOP(1):从基础开始

    转自  http://www.cnblogs.com/BeginMan/p/3510786.html 本文旨在Python复习和总结: 1.如何创建类和实例? # 创建类 class ClassNam ...

  10. 【Laravel5.5】 Laravel 在views中加载公共页面怎么实现

    背景: 在做后台功能时候,我们需要把头部和尾部摘出来作为公共模板使用 1:我们使用了Blade模板,并创建一个header.blade.php作为通用的模板.将子页面作为yield输出: header ...