AE实现右键点击TOC控件任意图层显示属性表

private void axTOCControl1_OnMouseDown( object sender, ESRI.ArcGIS.Controls.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 ();
               
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.Show();
            }
           
        }
复制代码

其中用到的AxTOCControl.HitTest(e.x, e.y, pItem, pMap, pLayer,  pOther, pIndex)方法,主要用于获得当前鼠标点击的图层,pItem, pMap, pLayer, pOther, pIndex几个参数均带ref关键字,返回e.x,e.y位置项所关联的对象,即图层,标注等。

在属性表窗口的代码:

View Code
复制代码
   
   
public partial class FrmAttribute : Form { 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(); } 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 ) { 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_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 ); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值