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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM