第一步:根据鼠标点击处的点,找到被选中的要素
1 public IFeature Find2(IPoint pPoint) 2 { 3 ITopologicalOperator pTopoOpe = pPoint as ITopologicalOperator; 4 double dis = m_pMapControl.ActiveView.ScreenDisplay.DisplayTransformation.FromPoints(5); 5 if (dis < 0.01) 6 dis = 0.01; 7 IGeometry pBuffer = pTopoOpe.Buffer(dis); 8 pBuffer.SpatialReference = m_pMapControl.Map.SpatialReference; 9 10 UIDClass id = new UIDClass(); 11 id.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; 12 IEnumLayer pEnumLayer = m_pMapControl.Map.get_Layers(id, true); 13 pEnumLayer.Reset(); 14 IFeatureLayer pFLayer = pEnumLayer.Next() as IFeatureLayer; 15 16 ISpatialFilter pSpatialFilter = new SpatialFilterClass(); 17 pSpatialFilter.Geometry = pBuffer; 18 pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; 19 pSpatialFilter.WhereClause = ""; 20 21 while (pFLayer != null) 22 { 23 if (pFLayer.Visible == false) 24 { 25 pFLayer = pEnumLayer.Next() as IFeatureLayer; 26 continue; 27 } 28 IFeatureCursor pCursor = pFLayer.Search(pSpatialFilter, false); 29 IFeature pFeat = pCursor.NextFeature(); 30 if (pFeat != null) 31 { 32 return pFeat; 33 } 34 System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor); 35 pFLayer = pEnumLayer.Next() as IFeatureLayer; 36 } 37 return null; 38 }
第二步:取出被选要素的属性值
1 IFields pFields = pFeat.Fields; 2 for (int i = 0; i < pFields.FieldCount; i++) 3 { 4 IField pField = pFields.get_Field(i); 5 if (pField.Type != esriFieldType.esriFieldTypeBlob & pField.Type != esriFieldType.esriFieldTypeOID 6 & pField.Type != esriFieldType.esriFieldTypeGeometry) 7 { 8 checkedListBox1.Items.Add(pField.AliasName); 9 } 10 }
第三步:在mapcontrol中显示属性,用到ICallout 加ITextElement
1 ICallout pCallout = new ESRI.ArcGIS.Display.BalloonCalloutClass(); 2 pCallout.AnchorPoint = pPoint; 3 pCallout.LeaderTolerance = 2; 4 5 ITextElement pTextElement = new TextElementClass(); 6 IElement pElement = pTextElement as IElement; 7 ITextSymbol pTextSysmbol = pTextElement.Symbol; 8 IFormattedTextSymbol pFormattedTextSymbol = new TextSymbolClass(); 9 pFormattedTextSymbol.Background = pCallout as ITextBackground; 10 pFormattedTextSymbol.Size = 8; 11 pFormattedTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; 12 pTextElement.Symbol = pFormattedTextSymbol; 13 pTextElement.Text = labelString; 14 15 pElement.Geometry = pPoint; 16 m_pMapControl.ActiveView.GraphicsContainer.AddElement(pElement, 0); 17 m_pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_pMapControl.Extent);