首页 > 代码库 > 鹰眼的创建,弥补教材中鹰眼的不足之处
鹰眼的创建,弥补教材中鹰眼的不足之处
在Arcengine的教程中,几乎都有创建鹰眼的教程,但是全是千篇一律,而且还有不足之处,比如在添加或者删除图层时,主MapCtrol的视图会发生变化,但鹰眼却不会触发事件,所以本文在原教程的基础上添加了图层添加和删除的监听事件。
1、首先是mapcontrol1的视图范围发生变化时 触发的事件
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)//当主视图的显示范围发生变化时,引发的事件 鹰眼
{
IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
pGraphicsContainer.DeleteAllElements();
IRectangleElement pRectangle = new RectangleElementClass();
IElement pEle = pRectangle as IElement;
IEnvelope pEnv = e.newEnvelope as IEnvelope;
pEle.Geometry = pEnv;
//设置颜色
IRgbColor rgb = new RgbColorClass();
rgb.Red = 200;
rgb.Green = 0;
rgb.Blue = 0;
rgb.Transparency = 255;
//产生一个线符号
ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Width = 2;
pLineSymbol.Color = rgb;
//设置填充符号的属性
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
rgb.Transparency=0;
pFillSymbol.Color = rgb;
pFillSymbol.Outline = pLineSymbol;
IFillShapeElement pfillshape = pRectangle as IFillShapeElement;
pfillshape.Symbol = pFillSymbol;
pGraphicsContainer.AddElement(pEle, 0);
axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
2、在加载mxd时触发的事件,但在添加图层时不会触发
private void axMapControl1_OnMapReplaced_1(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
//鹰眼
IMap pMap = axMapControl1.Map;
for (int i = 0; i < pMap.LayerCount; i++)
{
axMapControl2.Map.AddLayer(pMap.get_Layer(i));
}
axMapControl2.Extent = axMapControl2.FullExtent;
}
3、mapcontrol2的move与down事件
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)//点击鹰眼所引发的事件
{
if (e.button == 1)
{
IPoint pt = new PointClass();
pt.X = e.mapX;
pt.Y = e.mapY;
//IEnvelope pEnvelope = axMapControl1.Extent as IEnvelope;
//pEnvelope.CenterAt(pt);
//axMapControl1.Extent = pEnvelope;
axMapControl1.CenterAt(pt);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
if (e.button == 2)
{
IEnvelope pEnvelope = axMapControl2.TrackRectangle();
axMapControl1.Extent = pEnvelope;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)//在鹰眼上移动引发的事件
{
if (e.button == 1)
{
IPoint pt = new PointClass();
pt.X = e.mapX;
pt.Y = e.mapY;
axMapControl1.CenterAt(pt);
axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
4、最后就是监听图层的添加与删除事件
private IActiveViewEvents_Event itemEvent;//声明增删图层的事件
在Form_Load中添加以下代码:
itemEvent = (IActiveViewEvents_Event)axMapControl1.Map;
itemEvent.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(iae_ItemAdded);
itemEvent.ItemDeleted += new IActiveViewEvents_ItemDeletedEventHandler(iae_ItemDeleted);
最后定义添加删除图层的方法:
private void iae_ItemAdded(object m) //添加图层 触发的事件
{
for (int i = 0; i < axMapControl1.Map.LayerCount; i++)
{
axMapControl2.AddLayer(axMapControl1.get_Layer(i));
}
axMapControl2.Extent = axMapControl1.Extent;
axMapControl2.Refresh();
}
private void iae_ItemDeleted(object c) // //删除图层 触发的事件
{
axMapControl2.Map.DeleteLayer(m_Layer); // m_Layer 是根据HitTest方法得到的
axMapControl2.Extent = axMapControl1.Extent;
axMapControl2.Refresh();
}
如此,在增删图层时,鹰眼也会随之变化
鹰眼的创建,弥补教材中鹰眼的不足之处