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

在主窗口的代码:

View Code
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);
}
}

posted @ 2011-04-03 12:44  师士  阅读(2775)  评论(0编辑  收藏  举报