鹰眼窗口实现的基本思路:
(1)在布局空间控件需要两个MapControl控件,一个作为主视图,一个作为鹰眼视图;
(2)在主视图和鹰眼视图两个视图控件中显示数据应该一致。
(3)在主视图当中当前显示的地图范围,需要用红色矩形框在鹰眼视图中标会出来。当主视图显示的地图范围发生变化时,鹰眼图中红色框的位子也要发生变化。
(4)当用户鼠标在鹰眼视图中移动或者改变红色框大小或者位子时候,主视图的地图范围也要相应的发生变化。
其中涉及几个事件,具体代码如下:
private void button1_Click(object sender, EventArgs e)
{
loadMapDocument();
}
private void loadMapDocument()
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerArrowHourglass;
axMapControl1.LoadMxFile(filePath);
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filePath + "不是有效文档");
}
}
private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
onMapReplace();
}
private void onMapReplace()
{
IMap pMap = axMapControl1.Map;
for (int i = 0; i < pMap.LayerCount; ++i)
{
axMapControl2.AddLayer(pMap .get_Layer (i));
}
axMapControl2.Extent = axMapControl2.FullExtent;
}
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
IEnvelope pEnvelope = axMapControl1.Extent as IEnvelope;
pEnvelope.CenterAt(pPoint);
axMapControl1.Extent = pEnvelope;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
else if (e.button ==2)
{
IEnvelope pEnvelop = axMapControl2.TrackRectangle();
axMapControl1.Extent = pEnvelop;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button != 1)
return;
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
axMapControl1.CenterAt(pPoint);
axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
IActiveView pActiveVie = pGraphicsContainer as IActiveView;
pGraphicsContainer.DeleteAllElements();
IRectangleElement pRecElement = new RectangleElementClass();
IElement pEle = pRecElement as IElement;
IEnvelope pEnv;
pEnv = e.newEnvelope as IEnvelope;
pEle.Geometry = pEnv;
IRgbColor pColor = new RgbColorClass();
pColor.Red = 200;
pColor.Blue = 200;
pColor.Green = 0;
pColor.Transparency = 255;
ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Width = 2;
pLineSymbol.Color = pColor;
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pColor.Transparency = 0;
pFillSymbol.Color = pColor;
pFillSymbol.Outline = pLineSymbol;
IFillShapeElement pFillShapeElement = pRecElement as IFillShapeElement;
pFillShapeElement.Symbol = pFillSymbol;
pGraphicsContainer.AddElement(pEle, 0);
axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}