ARCEngine中右键图层以遍历图层名和字段名

                private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
                {
                    esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
                    IBasicMap map = null; ILayer layer = null;
                    object other = null; object index = null;
                    this.axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);

                    if (e.button == 2)
                    {
                        IFeatureLayer featureLayer;
                        featureLayer = layer as IFeatureLayer;
                        //IFeatureLayer featureLayer = axMapControl1.get_Layer(6) as IFeatureLayer;
                        ILayerFields layerFields = featureLayer as ILayerFields;
                        List<string> list = new List<string>();

                        for (int i = 0; i < axMapControl1.Map.LayerCount; i++)
                        {
                            string layer_name = axMapControl1.Map.get_Layer(i).Name.ToString();
                            list.Add(layer_name);
                        }

                        for (int i = 0; i < layerFields.FieldCount; i++)
                        {
                            IField field = layerFields.get_Field(i);
                            string fieldName = field.Name;
                            list.Add(fieldName);
                        }

                        string sum = "";
                        for (int j = 0; j < list.Count; j++)
                        {
                            string tucengming = "图层名:";
                            if (j < axMapControl1.Map.LayerCount)
                            {
                                sum += tucengming + list[j].ToString() + "  ";

                            }
                            else
                            {
                                string ziduanming = "字段名:";
                                sum += ziduanming + list[j].ToString() + "  ";
                            }

                        }

                        MessageBox.Show(sum, "图层名和字段名");
                    }
                }

运行效果:

猜你喜欢

转载自blog.csdn.net/qq_40323256/article/details/85117677