C# ArcEngine TOCControl上实现右键

C# ArcEngine TOCControl上实现右键

第一种方法:使用contextMenuStrip控件

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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事件下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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,显示属性表。

1
2
3
4
5
6
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,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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" );
     }
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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事件下完成单击事件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

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。


                                     山茶花

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值