步骤
1,首先在含有主视图控件 ESRI.ArcGIS.Controls.AxMapControl mapCtrl_main 的主类中定义一个 IEnvelope 成员变量,用于记录鼠标在主视图控件画好的轮廓
1 private ESRI.ArcGIS.Geometry.IEnvelope mainInvelope = null; //(主视图上) 2 public ESRI.ArcGIS.Geometry.IEnvelope MainInvelopeValue { 3 get { 4 return mainInvelope; 5 } 6 }
并设置成外界可访问的属性.
2,在该类中定义一bool变量,以控制查询
1 private bool startMainInvelope = false; //标识在主视图上画轮廓矩形,以进行该范围内点查询.
3,在 mapCtrl_main 的 OnMouseDown 响应事件中,控制轮廓的顺畅画好
1 if (1 == e.button && startMainInvelope) { //在主视图上画轮廓. 2 mainInvelope = mapCtrl_main.TrackRectangle(); 3 try { 4 if (!mainInvelope.IsEmpty) 5 Engine.App_Code.Draw.DrawSymbol(mapCtrl_main.Map, mainInvelope); 6 } 7 catch (System.Exception ex) { 8 MessageBox.Show(ex.Message); 9 } 10 }
其中用到画轮廓的辅助方法定义为:
1 /// <summary> 2 /// 画轮廓. 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e">根据IEnvelope对象画轮廓.</param> 6 public static void DrawSymbol(ESRI.ArcGIS.Carto.IMap map, ESRI.ArcGIS.Geometry.IEnvelope e) { 7 ESRI.ArcGIS.Carto.IGraphicsContainer hawkGC = (ESRI.ArcGIS.Carto.IGraphicsContainer)map; 8 ESRI.ArcGIS.Carto.IActiveView aView = (ESRI.ArcGIS.Carto.IActiveView)hawkGC; 9 hawkGC.DeleteAllElements(); 10 11 ESRI.ArcGIS.Carto.IElement recEle = (ESRI.ArcGIS.Carto.IElement)new ESRI.ArcGIS.Carto.RectangleElementClass(); 12 recEle.Geometry = e; 13 ESRI.ArcGIS.Display.ISimpleLineSymbol outLine = new ESRI.ArcGIS.Display.SimpleLineSymbolClass(); 14 outLine.Color = ColorPaint(255, 255); 15 outLine.Width = 2; 16 17 //填充样式. 18 ESRI.ArcGIS.Display.ISimpleFillSymbol fillSym = new ESRI.ArcGIS.Display.SimpleFillSymbolClass(); 19 fillSym.Color = ColorPaint(255, 0); 20 fillSym.Outline = outLine; 21 22 ESRI.ArcGIS.Carto.IFillShapeElement fillShape = (ESRI.ArcGIS.Carto.IFillShapeElement)recEle; 23 fillShape.Symbol = fillSym; 24 hawkGC.AddElement((ESRI.ArcGIS.Carto.IElement)fillShape, 0); 25 aView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null); 26 }
4,在查询窗口中,用 GetLayerByName 方法定位好待查询的点要素图层(方法定义为)
1 /// <summary> 2 /// 根据图层名获取图层. 3 /// </summary> 4 /// <param name="map"></param> 5 /// <param name="layerName">图层名称.</param> 6 /// <returns></returns> 7 public static ESRI.ArcGIS.Carto.ILayer GetLayerByName(ESRI.ArcGIS.Carto.IMap map, string layerName) { 8 ESRI.ArcGIS.Carto.IEnumLayer enunLayer = map.get_Layers(null, false); 9 enunLayer.Reset(); 10 ESRI.ArcGIS.Carto.ILayer resultLayer = null; 11 while ((resultLayer = enunLayer.Next()) != null) { 12 if (resultLayer.Name.Equals(layerName)) { 13 break; 14 } 15 } 16 return resultLayer; 17 }
5,查询窗口实现查询的所有代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Engine { 11 public partial class fQueryInvelopePoints : Form { 12 private ESRI.ArcGIS.Carto.IMap map = null; 13 private fMain fMain = null; 14 public fQueryInvelopePoints(fMain fMain, ESRI.ArcGIS.Carto.IMap map) { 15 this.fMain = fMain; 16 this.map = map; 17 InitializeComponent(); 18 } 19 20 /// <summary> 21 /// 查询指定矩形范围内的点要素. 22 /// </summary> 23 /// <param name="envelope">指定的矩形范围.</param> 24 /// <param name="featureClass">待查询的要素.</param> 25 /// <returns>返回结果的游标</returns> 26 public ESRI.ArcGIS.Geodatabase.IFeatureCursor GetAllFeaturesFromPointSearchInGeoFeatureLayer(ESRI.ArcGIS.Geometry.IEnvelope envelope, ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass) { 27 if (featureClass == null) 28 return null; 29 ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass(); 30 spatialFilter.Geometry = envelope; 31 spatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects; 32 spatialFilter.GeometryField = featureClass.ShapeFieldName; 33 return featureClass.Search(spatialFilter, false); 34 } 35 36 private void fQueryInvelopePoints_Load(object sender, EventArgs e) { 37 Engine.App_Code.Layer_Assist.InitLayers(map, cbox_layer); 38 } 39 40 private void btn_query_Click(object sender, EventArgs e) { 41 lsbox.Items.Clear(); //清空原始数据. 42 ESRI.ArcGIS.Carto.ILayer lyr = Engine.App_Code.Layer_Assist.GetLayerByName(map, cbox_layer.Text); 43 if (lyr is ESRI.ArcGIS.Carto.IFeatureLayer) { //矢量数据. 44 ESRI.ArcGIS.Carto.IFeatureLayer fLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)lyr; 45 ESRI.ArcGIS.Geometry.IEnvelope selEnvelope = fMain.MainInvelopeValue; 46 //获取矩形范围内的要素. 47 ESRI.ArcGIS.Geodatabase.IFeatureCursor fCur = GetAllFeaturesFromPointSearchInGeoFeatureLayer(selEnvelope, fLyr.FeatureClass); 48 ESRI.ArcGIS.Geodatabase.IFeatureClass fc = fLyr.FeatureClass; 49 ESRI.ArcGIS.Geodatabase.IFeature f = null; 50 string v = ""; 51 string k = ""; 52 k = "name"; 53 fMain.mapCtrl_main.ActiveView.Refresh(); //高亮显示前. 54 try { 55 while ((f = fCur.NextFeature()) != null) { 56 map.SelectFeature(fLyr, f); //高亮显示. 57 v = Convert.ToString(f.get_Value(f.Fields.FindField("name"))); 58 lsbox.Items.Add(k + " : " + v); 59 } 60 } 61 catch (System.Exception ex) { 62 MessageBox.Show(ex.Message + "不支持查询的图层或查询字段错误:NAME"); 63 } 64 fMain.mapCtrl_main.ActiveView.Refresh(); //高亮显示后. 65 } 66 } 67 } 68 }
6,运行时,画好矩形轮廓后,在主视图控件类中调用即可:
1 if (mainInvelope == null || mainInvelope.IsEmpty) { 2 MessageBox.Show("画轮廓为空"); 3 return; 4 } 5 if (fInvelopePoints == null) 6 fInvelopePoints = new fQueryInvelopePoints(this, mapCtrl_main.Map); 7 fInvelopePoints.FormClosed += (s, ea) => { 8 fInvelopePoints = null; 9 }; 10 fInvelopePoints.Show(this);
其中 fInvelopePoints 为定义在主视图类中的查询窗口的成员变量:
1 //查询窗口. 2 private fQueryInvelopePoints fInvelopePoints = null;
附
在主视图中画矩形轮廓如图:
查询后,弹出查询结果如图: