AE开发——右键TOC中的图层名显示属性表

  这是我设计的主窗体

  新建一个窗体,拖入一个datagridview控件,用来展示图层属性,Dock属性设为Fill

  在主窗体中设置一个OnMouseDown事件

private void axTOCControl1_OnMouseDown ( object sender, ITOCControlEvents_OnMouseDownEvent e )
{
  //鼠标右键
   if (e.button == 2)
   {
     esriTOCControlItem Item = new esriTOCControlItem();
     IBasicMap pMap = new MapClass();
     ILayer pLayer = new FeatureLayerClass();
     object pOther = new object();
     object pIndex = new object();
     //获取鼠标点击的图层,通过ref参数返回点击位置所关联的图层
     this.axTOCControl1.HitTest( e.x, e.y, ref Item, ref pMap, ref pLayer, ref pOther, ref pIndex );

     IMapControl2 pMapControl = (IMapControl2)axMapControl1.Object;
     IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
     FrmAttribute pFemAttribute = new FrmAttribute( pMapControl, pFeatureLayer.Name );
     pFemAttribute.ShowDialog();
   }
}

以下是属性展示窗体的源代码,其中包括了全屏显示属性、datagridview自适应form宽度、在表中点击字段名在主窗体中高亮显示等功能

namespace GIS3._6
{
    public partial class FrmAttribute : Form
    {
        public FrmAttribute ( )
        {
            InitializeComponent();
        }

        int width = 0;
        public IMapControl2 pMapControl;
        public IMap pMap;
        public int LayerIndex;
        public string LayerName;

        public FrmAttribute ( IMapControl2 pMapControl, string LyrName )
        {
            InitializeComponent();
            this.pMapControl = pMapControl;
            pMap = pMapControl.Map;
            LayerName = LyrName;
        }

        private void FrmAttribute_Load ( object sender, EventArgs e )
        {
            GetValues();
            #region datagridview自适应form
            for (int i = 0 ; i < this.dataGridView1.Columns.Count ; i++)
            {
                //将每一列都调整为自动适应模式
                //this.dataGridView1.AutoResizeColumn( DataGridViewAutoSizeColumnMode.AllCells);
                //记录整个DataGridView的宽度
                width += this.dataGridView1.Columns[i].Width;
            }
            if (width > this.dataGridView1.Size.Width)
            {
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            }
            else
            {
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            //冻结某列 从左开始 0,1,2
            dataGridView1.Columns[1].Frozen = true;
            #endregion
        }
        #region 枚举
        public enum DataGridViewAutoSizeColumnMode
        {
            NotSet = 0,
            None = 1,
            ColumnHeader = 2,
            AllCellsExceptHeader = 4,
            AllCells = 6,
            DisplayedCellsExceptHeader = 8,
            DisplayedCells = 10,
            Fill = 16,
        }
        #endregion
        public void GetValues ( )
        {
            for (int i = 0 ; i < pMap.LayerCount ; i++)
            {
                if (LayerName == pMap.get_Layer( i ).Name)
                {
                    LayerIndex = i;
                    break;
                }
            }
            IFeatureLayer pFeatureLayer = pMap.get_Layer( LayerIndex ) as IFeatureLayer;
            IFields pFields = pFeatureLayer.FeatureClass.Fields;
            dataGridView1.ColumnCount = pFields.FieldCount;
            for (int i = 0 ; i < pFields.FieldCount ; i++)
            {
                string fieldname;
                fieldname = pFields.get_Field( i ).Name;
                dataGridView1.Columns[i].Name = fieldname;
            }
            IFeatureCursor pFeatureCursor = pFeatureLayer.FeatureClass.Search( null, false );
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                //pFields.FieldCount  总字段数
                string[] fldvalue = new string[pFields.FieldCount];
                for (int i = 0 ; i < pFields.FieldCount ; i++)
                {

                    if (pFields.get_Field( i ).Name == "Shape")
                    {
                        fldvalue[i] = Convert.ToString( pFeature.Shape.GeometryType );
                    }
                    else
                    {
                        fldvalue[i] = Convert.ToString( pFeature.get_Value( i ) );
                    }
                }
                dataGridView1.Rows.Add( fldvalue );
                pFeature = pFeatureCursor.NextFeature();
            }
        }
        private void dataGridView1_CellContentClick ( object sender, DataGridViewCellEventArgs e )
        {

        }
        #region 双击表内容在屏幕居中展示所选要素
        private void dataGridView1_CellDoubleClick ( object sender, DataGridViewCellEventArgs e )
        {
            string FID;
            FID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

            if (FID == "")
                return;
            IActiveView pActiveView;
            pActiveView = (IActiveView)pMap;
            pMap.ClearSelection();
            pActiveView.Refresh();

            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.WhereClause = "FID=" + FID;

            IFeatureLayer pFeatureLayer;
            pFeatureLayer = (IFeatureLayer)pMap.get_Layer( LayerIndex );

            IFeatureCursor pFeatureCursor;
            pFeatureCursor = pFeatureLayer.Search( pQueryFilter, false );

            IFeature pFeature;
            pFeature = pFeatureCursor.NextFeature();

            pMap.SelectFeature( pFeatureLayer, pFeature );

            IPoint pPoint = new PointClass();
            pPoint.X = ( pFeature.Extent.XMin + pFeature.Extent.XMax ) / 2;
            pPoint.Y = ( pFeature.Extent.YMin + pFeature.Extent.YMax ) / 2;

            pMapControl.CenterAt( pPoint );

            pActiveView.PartialRefresh( esriViewDrawPhase.esriViewGeoSelection, null, null );
        }
#endregion
    }
}

  以下是运行效果,通过鼠标右键TOC中的图层名直接弹出属性查看窗体

  目前正在写一个只显示指定图层的指定属性的功能

原文地址:https://www.cnblogs.com/liuliang1999/p/10634507.html