过实现鹰眼图这个功能来进一步学习MapControl控件

们通过实现鹰眼图这个功能来进一步学习MapControl控件。在实现鹰眼图之前,我们需 要接口有更深入的了解。

变主动为被动-出接口(OutBound interface)

COM编程类似客户端和服务器端的两层结构,COM所建立的是一个软件模块与另一个软 件模块之间的链接, 当这种链接建立之后, 模块之间就可以通过被称之为Interface“接口 ” 的机制来进行通信。在绝大部分情况下, 客户应用程序与组件的通信过程是单向的, 客户创 建组件对象, 然后客户通过接口调用对象所提供的功能, 在适当的时候再把对象释放掉。在这种交互过程中, 客户总是主动的, 而组件总是处于被动状态, 通过自身暴露给客户的接口监听客户的请求, 一旦接收到客户的请求便做出反应,这些反应的“幕后“,也就是代码是被屏蔽掉的,我们是看不到这些接口内的方法是如何实现的。这样的接口称为入接口InBound Interface,但是对于一个全面交互过程来说, 这样的单向通信往往是不能满足实际的需要, 组件对象也要主动与客户进行通信, 因此, 与入接口相对应, 对象也可以提供出接口OutBound interface,对象通过这些出接口与客户进行通信。之所以把这些接口称为出接口, 其原因在于这些接口并不由 COM服务器端的对象实现, 而是由客户程序自己来实现, 客户实现这些接口, 服务器端调用此接口的成员函数, 即调用了客户自定义的函数, 这时组件对象变成了客户端的客户。也就是说出接口的实现是由我们自己实现,而被服务器调用,这样的接口,我们往往称之为事件接口,这些接口里面定义了一些如OnMouseUp,OnMouseMove等函数,当相应事件发生的时候,由服务器去执行这个事件里面的内容。

鹰眼图的实现
鹰眼图的实现用到控件如下::
控件名称        控件类型      备注
axMapControl1              主图
axMapControl2              鸟瞰图
axToolbarControl1    
axTOCControl1    
分析:鹰眼图的操作主要由以下几个动作,当在一个控件中移动一幅图的时候另一控件中的图也发生变化,当在主控件中重新加载一幅图的时候,另外一个控件的图也发生相应的变化,同时我们在鸟瞰的控件中加入一红色边框,注意这个其实是一个面,只是填充的颜色是透明的而已。通过分析我们知道,我们要添加两个MapControl控件,名字分别是axMapControl1和axMapControl2,其中axMapControl1为主图,而axMapControl1为鸟瞰图。 对于名称为axMapControl1的MapControl控件,只需要在axMapControl1的OnExtentUpdated和OnMapReplaced中分别添加以下代码:

/// <summary>
        /// 鹰眼图的实现axMapControl1主图,axMapControl2鸟瞰图
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            //得到新范围
            IEnvelope pEnvelope = (IEnvelope)e.newEnvelope;

            IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
            IActiveView pActiveView = pGraphicsContainer as IActiveView;
            //绘制前清除axMapControl2中的任何图形元素
            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;

            //设置颜色属性
            pColor = new RgbColorClass();
            pColor.Red = 255;
            pColor.Green = 0;
            pColor.Blue = 0;
            pColor.Transparency = 0;

            //设置填充符号的属性
            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Color = pColor;
            pFillSymbol.Outline = pOutline;

            IFillShapeElement pFillShapeEle = pElement as IFillShapeElement;
            pFillShapeEle.Symbol = pFillSymbol;
            pGraphicsContainer.AddElement((IElement)pFillShapeEle,0);
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

        }

        private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
        {
            if (axMapControl1.LayerCount > 0)
            {
                axMapControl2.Map = new MapClass();
                for (int i = 0; i <= axMapControl1.Map.LayerCount - 1; i++)
                {
                    axMapControl2.AddLayer(axMapControl1.get_Layer(i));
                }
                axMapControl2.Extent = axMapControl1.Extent;
                axMapControl2.Refresh();
            }
        }

        private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (axMapControl2.Map.LayerCount > 0)
            {
                if (e.button == 1)
                {
                    IPoint pPoint = new PointClass();

                    pPoint.PutCoords(e.mapX, e.mapY);
                    axMapControl1.CenterAt(pPoint);

                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                 }
                else if (e.button == 2)
                {
                    IEnvelope pEnv = axMapControl2.TrackRectangle();
                    axMapControl1.Extent = pEnv;
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); 
                }
            }
        }

        private void axMapControl2_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
        {
            if (e.button == 1)
            {
                IPoint pPoint = new PointClass();
                pPoint.PutCoords(e.mapX, e.mapY);
                axMapControl1.CenterAt(pPoint);

                axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
        }

但是代码部分有错。

原文地址:https://www.cnblogs.com/qiushuixizhao/p/3234573.html