最近,看到打听鹰眼问题的朋友特别多。AE+C#的鹰眼在网上代码已经很多了,我今天也放一个版本上 来,主要有以下几个功能:
1.主地图窗口视图范围改变后,鹰眼窗口绘制一个红色的框显示当前区域;
2.主地图比例尺发生改变后,鹰眼地图的比例尺也发生改变,只是改变的程度将小而已;(这和网上其 它代码不同,借鉴google地图鹰眼的显示效果)
3.在鹰眼地图上,用鼠标左键可以移动红色的区域框,松开鼠标后,主窗口地图视图范围更新为移动 后的框的范围;
4.在鹰眼地图上,用鼠标右键可以拉一个框,主窗口视图范围更新为这个框的范围。
好了,话不多话,全代码放出:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using ESRI.ArcGIS.Carto;
9 using ESRI.ArcGIS.Geometry;
10 using ESRI.ArcGIS.Display;
11 using ESRI.ArcGIS.esriSystem;
12
13 namespace WindowsFormsApplication19
14 {
15 public partial class Form1 : Form
16 {
17 public Form1()
18 {
19 InitializeComponent();
20 }
21
22
23 private void Form1_Load(object sender, EventArgs e)
24 {
25 AddLayerToOverViewMap(); //加入地图到鹰眼里
26 }
27
28
29 /// <summary>
30 /// 主地图更换时,更新鹰眼窗口
31 /// </summary>
32 /// <param name="sender"></param>
33 /// <param name="e"></param>
34 private void MainMapControl_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
35 {
36 AddLayerToOverViewMap();
37 }
38 /// <summary>
39 /// 把地图加到鹰眼里
40 /// </summary>
41 private void AddLayerToOverViewMap()
42 {
43 OverViewMapControl.ClearLayers();
44 for (int i = 0; i < MainMapControl.LayerCount; i++)
45 {
46 IObjectCopy objectcopy = new ObjectCopyClass();
47 object toCopyLayer = MainMapControl.get_Layer (i);
48 object copiedLayer = objectcopy.Copy (toCopyLayer);
49 ILayer C = (new FeatureLayerClass()) as ILayer;
50 object toOverwriteLayer = C;
51 objectcopy.Overwrite(copiedLayer, ref toOverwriteLayer);
52 OverViewMapControl.AddLayer(C, i);
53 }
54 }
55
56 /// <summary>
57 /// 主地图窗口视图范围发生改变后,更新鹰眼上的红色矩形框
58 /// </summary>
59 /// <param name="sender"></param>
60 /// <param name="e"></param>
61 private void MainMapControl_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
62 {
63 try
64 {
65 IGraphicsContainer pGraphicsContainer = OverViewMapControl.Map as IGraphicsContainer;
66 IActiveView pAv = pGraphicsContainer as IActiveView;
67 pGraphicsContainer.DeleteAllElements();
68 IRectangleElement pRecElement = new RectangleElementClass();
69 IElement pEle = pRecElement as IElement;
70 pEle.Geometry = e.newEnvelope as IEnvelope;
71
72 IEnvelope mm = new EnvelopeClass();
73 mm.XMax = pEle.Geometry.Envelope.XMax + 3;
74 mm.XMin = pEle.Geometry.Envelope.XMin - 3;
75 mm.YMax = pEle.Geometry.Envelope.YMax + 3;
76 mm.YMin = pEle.Geometry.Envelope.YMin - 3;
77 OverViewMapControl.Extent = mm;
78
79 IRgbColor pColor = new RgbColorClass();
80 pColor.Red = 200;
81 pColor.Green = 0;
82 pColor.Blue = 0;
83 pColor.Transparency = 255;
84
85 //产生一个线符号对象
86 ILineSymbol pLineSymbol = new SimpleLineSymbolClass ();
87 pLineSymbol.Width = 2;
88 pLineSymbol.Color = pColor;
89
90 //设置填充符号的属性
91 IFillSymbol pFillSymbol = new SimpleFillSymbolClass ();
92
93 // 设置透明颜色
94 pColor.Transparency = 0;
95 pFillSymbol.Color = pColor;
96 pFillSymbol.Outline = pLineSymbol;
97
98 //
99 IFillShapeElement pFillShapeElement = pRecElement as IFillShapeElement;
100 pFillShapeElement.Symbol = pFillSymbol;
101
102 pGraphicsContainer.AddElement(pEle, 0);
103 OverViewMapControl.ActiveView.PartialRefresh (esriViewDrawPhase.esriViewGraphics, null, null);
104
105 //
106 }
107 catch (Exception ey)
108 {
109 }
110 }
111
112 /// <summary>
113 /// 鹰眼窗口,用鼠标右键拉框,更改主窗口当前视图范围
114 /// </summary>
115 /// <param name="sender"></param>
116 /// <param name="e"></param>
117 private void OverViewMapControl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
118 {
119 #region 鹰眼
120 if (e.button == 2)
121 {
122 try
123 {
124 IPoint pPt = new PointClass();
125 pPt.X = e.mapX;
126 pPt.Y = e.mapY;
127 IEnvelope pEnvelope = OverViewMapControl.TrackRectangle();
128 MainMapControl.Extent = pEnvelope;
129 }
130 catch
131 { }
132 }
133 #endregion
134 }
135
136 /// <summary>
137 /// 鹰眼窗口,用鼠标左键移动视图范围
138 /// </summary>
139 /// <param name="sender"></param>
140 /// <param name="e"></param>
141 private void OverViewMapControl_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
142 {
143
144 if (e.button == 1)
145 {
146 try
147 {
148 IPoint pPt = new PointClass();
149 pPt.X = e.mapX;
150 pPt.Y = e.mapY;
151 //
152 IEnvelope pEnvelope = MainMapControl.Extent as IEnvelope;
153 pEnvelope.CenterAt(pPt);
154 IActiveView pAv;
155 IGraphicsContainer pGraphicsContainer = OverViewMapControl.Map as IGraphicsContainer;
156 pAv = pGraphicsContainer as IActiveView;
157 pGraphicsContainer.DeleteAllElements();
158 IRectangleElement pRecElement = new RectangleElementClass();
159 IElement pEle = pRecElement as IElement;
160 pEle.Geometry = pEnvelope;
161 //颜色
162 IRgbColor pColor = new RgbColorClass();
163 pColor.Red = 200;
164 pColor.Green = 0;
165 pColor.Blue = 0;
166 pColor.Transparency = 255;
167
168 //产生一个线符号对象
169 ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
170 pLineSymbol.Width = 2;
171 pLineSymbol.Color = pColor;
172
173 //设置填充符号的属性
174 IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
175
176 // 设置透明颜色
177 pColor.Transparency = 0;
178 pFillSymbol.Color = pColor;
179 pFillSymbol.Outline = pLineSymbol;
180
181 //
182 IFillShapeElement pFillShapeElement = pRecElement as IFillShapeElement;
183 pFillShapeElement.Symbol = pFillSymbol;
184
185 pGraphicsContainer.AddElement(pEle, 0);
186 OverViewMapControl.ActiveView.PartialRefresh (esriViewDrawPhase.esriViewGraphics, null, null);
187
188 }
189 catch
190 {
191 }
192 }
193
194
195 }
196
197 /// <summary>
198 /// 鹰眼窗口,左键移动视图框后,更新主窗口
199 /// </summary>
200 /// <param name="sender"></param>
201 /// <param name="e"></param>
202 private void OverViewMapControl_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
203 {
204 #region 鹰眼
205 if (e.button == 1)
206 {
207 try
208 {
209 IPoint pPt = new PointClass();
210 pPt.X = e.mapX;
211 pPt.Y = e.mapY;
212
213 IEnvelope pEnvelope = MainMapControl.Extent as IEnvelope;
214 pEnvelope.CenterAt(pPt);
215 MainMapControl.Extent = pEnvelope;
216 }
217 catch
218 {
219 }
220 }
221 #endregion
222 }
223
224 }
225 }
226