关键点:
对IArray、IFeature.ShapeCopy、HookHelperClass以及IHookActions.DoActionOnMultiple的正确使用。
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; namespace AgsTester { /// <summary> /// 本用户控件上需要放一个地图控件 /// </summary> public partial class FlashMutiGeometry : UserControl { public FlashMutiGeometry() { InitializeComponent(); } /// <summary> /// 加载地图 /// </summary> /// <param name="pathMap"></param> public void LoadMap(string pathMap) { axMapControl1.LoadMxFile(pathMap); } /// <summary> /// 以村为单位过滤显示 /// </summary> /// <param name="where">查询条件</param> public void FilterLayer(string where) { IFeatureLayer flyr = (IFeatureLayer)axMapControl1.get_Layer(0); IFeatureClass fcls = flyr.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = where; // 缩放到选择结果集,并高亮显示 ZoomToSelectedFeature(flyr, queryFilter); //闪烁选中得图斑 IFeatureCursor featureCursor = fcls.Search(queryFilter, true); FlashPolygons(featureCursor); } /// <summary> /// 缩放到选择结果集,并高亮显示 /// </summary> /// <param name="pFeatureLyr"></param> /// <param name="pQueryFilter"></param> private void ZoomToSelectedFeature(IFeatureLayer pFeatureLyr, IQueryFilter pQueryFilter) { #region 高亮显示查询到的要素集合 //符号边线颜色 IRgbColor pLineColor = new RgbColor(); pLineColor.Red = 255; ILineSymbol ilSymbl = new SimpleLineSymbolClass(); ilSymbl.Color = pLineColor; ilSymbl.Width = 5; //定义选中要素的符号为红色 ISimpleFillSymbol ipSimpleFillSymbol = new SimpleFillSymbol(); ipSimpleFillSymbol.Outline = ilSymbl; RgbColor pFillColor = new RgbColor(); pFillColor.Green = 60; ipSimpleFillSymbol.Color = pFillColor; ipSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSForwardDiagonal; //选取要素集 IFeatureSelection pFtSelection = pFeatureLyr as IFeatureSelection; pFtSelection.SetSelectionSymbol = true; pFtSelection.SelectionSymbol = (ISymbol)ipSimpleFillSymbol; pFtSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); #endregion ISelectionSet pSelectionSet = pFtSelection.SelectionSet; //居中显示选中要素 IEnumGeometry pEnumGeom = new EnumFeatureGeometry(); IEnumGeometryBind pEnumGeomBind = pEnumGeom as IEnumGeometryBind; pEnumGeomBind.BindGeometrySource(null, pSelectionSet); IGeometryFactory pGeomFactory = new GeometryEnvironmentClass(); IGeometry pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom); axMapControl1.ActiveView.Extent = pGeom.Envelope; axMapControl1.ActiveView.Refresh(); } /// <summary> /// 闪烁选中得图斑 /// </summary> /// <param name="featureCursor"></param> private void FlashPolygons(IFeatureCursor featureCursor) { IArray geoArray = new ArrayClass(); IFeature feature = null; while ((feature = featureCursor.NextFeature()) != null) { //feature是循环外指针,所以必须用ShapeCopy geoArray.Add(feature.ShapeCopy); } //通过IHookActions闪烁要素集合 HookHelperClass m_pHookHelper = new HookHelperClass(); m_pHookHelper.Hook = axMapControl1.Object; IHookActions hookActions = (IHookActions)m_pHookHelper; hookActions.DoActionOnMultiple(geoArray, esriHookActions.esriHookActionsPan); //hookActions.DoActionOnMultiple(geoArray, esriHookActions.esriHookActionsGraphic); //hookActions.DoActionOnMultiple(geoArray, esriHookActions.esriHookActionsZoom); Application.DoEvents(); m_pHookHelper.ActiveView.ScreenDisplay.UpdateWindow(); hookActions.DoActionOnMultiple(geoArray, esriHookActions.esriHookActionsFlash); } } }
截图如下: