c#鹰眼地图的实现(理解消息传递机制)

思路:axMapControl1加载主图时,触发其OnMapReplaced事件,将加载的图层同样加载到鹰眼控件即axMapControl2中,当主图的extent变化例如放大缩小事件,会触发主图的OnExtentUpdated事件,此时设置矩形边框的颜色等参数。另外需要在点击鹰眼图层时(OnMouseDown事件)使得主图范围随之变化。

#region  鹰眼实现代码
   private void axMapControl1_OnMapReplaced_1(object  sender,IMapControlEvents2_OnMapReplacedEvent e)
   //这个函数不会刷新,但是只对地图文档有用,shp无效
   {
            for (int i = axMapControl1.Map.LayerCount - 1; i >= 0; i--)
             // if (axMapControl1.Map.get_Layer(i).Visible==true)
                  axMapControl2.Map.AddLayer(axMapControl1.Map.get_Layer(i));
                  axMapControl2.Extent = axMapControl2.FullExtent;
                  axMapControl2.Refresh();

    }
    //互动
    private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
    {           
                IPoint pPoint = new PointClass();
                pPoint.PutCoords(e.mapX, e.mapY);
                axMapControl1.CenterAt(pPoint);
                axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);          
     }
    private void axMapControl1_OnExtentUpdated_1(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
    {
        IEnvelope pEnvelope = (IEnvelope)e.newEnvelope;//外接矩形
        IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;//告诉矩形在哪里画
        IActiveView pActiveView = pGraphicsContainer as IActiveView;//激活
        pGraphicsContainer.DeleteAllElements();
        IRectangleElement pRectangleEle = new RectangleElementClass();
        IElement pElement = pRectangleEle as IElement;
        pElement.Geometry = pEnvelope;     //分别设置矩形的边框颜色 和 填充颜色           
         IRgbColor pColor = new RgbColorClass();
        pColor.Red = 255;
        pColor.Green = 0;
        pColor.Blue = 0;
        pColor.Transparency = 255;
        ILineSymbol pOutline = new SimpleLineSymbolClass();
        pOutline.Width = 3;
        pOutline.Color = pColor;
       IRgbColor eColor = new RgbColorClass();
        eColor.Red = 255;
        eColor.Green = 0;
        eColor.Blue = 0;
        eColor.Transparency = 0;
        SimpleFillSymbolClass pFillSymbol = new SimpleFillSymbolClass();
        pFillSymbol.Color = eColor;
        pFillSymbol.Outline = pOutline;
        IFillShapeElement pFillShapeEle = pElement as IFillShapeElement;
        pFillShapeEle.Symbol = pFillSymbol;            pGraphicsContainer.AddElement((IElement)pFillShapeEle, 0);
        pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);  
    }
    #endregion

猜你喜欢

转载自blog.csdn.net/qq_39967296/article/details/89440955