AE 鹰眼同步

  跟着书做的,看起来比较粗糙,但还是能实现基本的鹰眼功能。

  点滴记录学习,早日出人头地!

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 同步鹰眼
{
    public partial class Form1 : Form
    {

        #region 定义全局变量
        /// <summary>
        /// 鹰眼地图上的矩形框可移动的标志
        /// </summary>
        private bool bCanDrag;              //鹰眼地图上的矩形框可移动的标志

        /// <summary>
        /// 记录在移动鹰眼地图上的矩形框时鼠标的位置
        /// </summary>
        private IPoint pMoveRectPoint;      //记录在移动鹰眼地图上的矩形框时鼠标的位置

        /// <summary>
        /// 记录数据视图的Extent(范围)
        /// </summary>
        private IEnvelope pEnv;             //记录数据视图的Extent(范围)

        #endregion

        public Form1()
        {
            InitializeComponent();
            //绑定控件
            axTOCControl.SetBuddyControl(mainMapControl);
            axToolbarControl1.SetBuddyControl(mainMapControl);

            EagleEyeMapControl.Extent = mainMapControl.FullExtent;//初始化,鹰眼视图显示全部地图
            pEnv = EagleEyeMapControl.Extent;
            DrawRectangle(pEnv);                //画框
        }

        private void mainMapControl_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
        {
            SynchronizeEagleEye();//Synchronize 使...同步
        }

        #region 鹰眼同步
        /// <summary>
        /// 鹰眼同步
        /// </summary>
        private void SynchronizeEagleEye()
        {
            if (EagleEyeMapControl.LayerCount > 0)
            {
                EagleEyeMapControl.ClearLayers();
            }
            //设置鹰眼和坐标系统一致
            EagleEyeMapControl.SpatialReference = mainMapControl.SpatialReference;//SpatialReference 参考空间
            for (int i = mainMapControl.LayerCount - 1; i >= 0; i -- )
            {
                //使鹰眼视图与数据视图的图层上下顺序保持一致
                ILayer pLayer = mainMapControl.get_Layer(i);
                if (pLayer is IGroupLayer || pLayer is ICompositeLayer)//判断是否兼容,可以转化
                {
                    ICompositeLayer pCompositeLayer = (ICompositeLayer)pLayer;
                    for (int j = pCompositeLayer.Count - 1; j >= 0; j--)
                    {
                        ILayer pSubLayer = pCompositeLayer.get_Layer(j);
                        IFeatureLayer pFeatureLayer = pSubLayer as IFeatureLayer;
                        if (pFeatureLayer != null)
                        //由于鹰眼地图比较小,所以不添加过滤点图层
                        {
                            if (pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint
                               && pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryMultipoint) //添加Geodatabase引用
                            {
                                EagleEyeMapControl.AddLayer(pLayer);
                            }
                        }
                    }
                }
                else
                {
                    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                    if (pFeatureLayer != null)
                    {
                        if (pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint
                            && pFeatureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryMultipoint)
                        {
                            EagleEyeMapControl.AddLayer(pLayer);
                        }
                    }
                }
                //设置鹰眼地图全图显示
                EagleEyeMapControl.Extent = mainMapControl.FullExtent;
                pEnv = mainMapControl.Extent as IEnvelope;
                DrawRectangle(pEnv);//
                EagleEyeMapControl.ActiveView.Refresh();//刷新
            }
        }        
        #endregion


        //移动矩形框
        private void EagleEyeMapControl_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
        {
            if (e.mapX > pEnv.XMin && e.mapY > pEnv.YMin && e.mapX < pEnv.XMax && e.mapY < pEnv.YMax)
            {
                //EagleEyeMapControl.MousePointer = esriControlsMousePointer.esriPointerHand;//如果鼠标移动到矩形框中时,鼠标变成小手,表示可以拖动//MousePointer鼠标指针
                Cursor = Cursors.Hand;//鼠标转换成小手的效果,比上一行(114)的效果好一些
                if (e.button == 2)//若按下鼠标右键,将鼠标演示设置为默认样式
                {
                    EagleEyeMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
                }
                else
                {
                    EagleEyeMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;//与其它位置将鼠标设置为默认的样式
                }
                if (bCanDrag)
                {
                    double Dx, Dy;//记录鼠标移动距离
                    Dx = e.mapX - pMoveRectPoint.X;
                    Dy = e.mapY - pMoveRectPoint.Y;//根据偏移量更改pEnv位置
                    pEnv.Offset(Dx, Dy);//Offset()  moves the sides x units horizontally 水平方向移动x个单位
                    pMoveRectPoint.PutCoords(e.mapX, e.mapY);
                    DrawRectangle(pEnv);
                    mainMapControl.Extent = pEnv;
                }
            }
        }

        private void EagleEyeMapControl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)  //按下鼠标不放
        {
            if (EagleEyeMapControl.Map.LayerCount > 0)//判断鹰眼控件中是否有图层
            {
                if (e.button == 1)                      //按下鼠标左键移动矩形框
                {
                    if (e.mapX > pEnv.XMin && e.mapY > pEnv.YMin && e.mapX < pEnv.XMax && e.mapY < pEnv.YMax)//如果指针落在鹰眼的矩形框中,标记可移动//(.mapX/Y)为鼠标xy坐标
                    {
                        bCanDrag = true;                //Drag 拖动            
                    }
                    pMoveRectPoint = new PointClass();      //MoveRectPoint 移动矩形点//PointClass 报错,修改报错引用嵌入操作为false
                    pMoveRectPoint.PutCoords(e.mapX, e.mapY);//记录点击的第一个坐标
                }
                else if (e.button == 2)                 //按下鼠标右键绘制矩形框
                {
                    IEnvelope pEnvelope = EagleEyeMapControl.TrackRectangle();//TrackRectangle 跟踪矩形
                    IPoint pTempPiont = new PointClass();
                    pTempPiont.PutCoords(pEnvelope.XMin + pEnvelope.Width / 2, pEnvelope.YMin + pEnvelope.Height / 2);//PutCoords 设置坐标//coordinate坐标  //设置或记录坐标XY,矩形左侧+二分之一宽即为X方向中心点,Y轴同理                  
                    mainMapControl.CenterAt(pTempPiont);//矩形框的高度和数据视图的高宽不一定成正比,这里做一个中心调整
                }
            }
        }

        private void EagleEyeMapControl_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)      //鼠标抬起时触发//感觉更是click事件....
        {
            if (e.button == 1 && pMoveRectPoint != null)   //按下鼠标左键且矩形框位置发生变化
            {
                if (e.mapX == pMoveRectPoint.X && e.mapY == pMoveRectPoint.Y)//
                {
                    mainMapControl.CenterAt(pMoveRectPoint);//中心调整
                }
                bCanDrag = false;
            }
        }

        private void mainMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            //得到当前视图范围
            pEnv = (IEnvelope)e.newEnvelope;
            DrawRectangle(pEnv);
        }

        #region 鹰眼矩形框
        /// <summary>
        /// 鹰眼矩形框
        /// </summary>
        /// <param name="pEnvelope"></param>
        private void DrawRectangle(IEnvelope pEnvelope)
        {
            //在绘制前,清除鹰眼中I之前绘制的矩形框
            IGraphicsContainer pGraphicsContainer = EagleEyeMapControl.Map as IGraphicsContainer;
            IActiveView pActiveView = pGraphicsContainer as IActiveView;
            pGraphicsContainer.DeleteAllElements();
            //得到当前视图范围
            IRectangleElement pRectangleElement = new RectangleElementClass();//RectangleElement 矩形元素
            IElement pElement = pRectangleElement as IElement;
            pElement.Geometry = pEnvelope;//Envelope 包装;Geometry 几何
            //设置矩形框(实质为中间透明度面)
            IRgbColor pRgbColor = new RgbColorClass();//若出现无法嵌入互操作报错。引用Display嵌入操作改为False
            pRgbColor = GetRgbColor(255, 0, 0);//红色
            pRgbColor.Transparency = 255;             
            ILineSymbol pOutLine = new SimpleLineSymbolClass();
            pOutLine.Width = 2; //线条宽2
            pOutLine.Color = pRgbColor;

            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
            pRgbColor = new RgbColorClass();
            pRgbColor.Transparency = 0;             //Transparency 透明度为 0
            pFillSymbol.Color = pRgbColor;
            pFillSymbol.Outline = pOutLine;

            //向鹰眼中添加矩形框
            IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
            pFillShapeElement.Symbol = pFillSymbol;
            pGraphicsContainer.AddElement((IElement)pFillShapeElement, 0);//Graphics 图形
            //刷新
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);//PartialRefresh 局部刷新
        }
        #endregion

        #region 获取RGB颜色
        /// <summary>
        /// 获取RGB颜色
        /// </summary>
        /// <param name="intR"></param>
        /// <param name="intG">绿</param>
        /// <param name="intB"></param>
        /// <returns></returns>
        private IRgbColor GetRgbColor(int intR, int intG, int intB)
        {
            IRgbColor pRgbColor = null;
            if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
            {
                return pRgbColor;
            }
            pRgbColor = new RgbColorClass();//若出现无法嵌入互操作...,引用 DIsplay,属性,嵌入互操作类型 选择 False
            pRgbColor.Red = intR;
            pRgbColor.Green = intG;
            pRgbColor.Blue = intB;
            return pRgbColor;
        }
        #endregion

        #region 加载地图
        private void btnAddMX_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog pOpenFileDialog = new OpenFileDialog();
                pOpenFileDialog.CheckFileExists = true;
                pOpenFileDialog.Title = "加载地图文档";
                pOpenFileDialog.Filter = "AcrMap文档(*.mxd)|*.mxd|ArcMap(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf";
                pOpenFileDialog.Multiselect = false;
                pOpenFileDialog.RestoreDirectory = true;
                if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string pFileName = pOpenFileDialog.FileName;
                    if (pFileName == "")
                    {
                        return;
                    }
                    if (mainMapControl.CheckMxFile(pFileName))
                    {
                        ClearAllData();
                        mainMapControl.LoadMxFile(pFileName);
                    }
                    else
                    {
                        MessageBox.Show(pFileName + "是无效的地图文档!", "提示!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("地图文档打开失败!" + ex.Message);
            }
        }
        #endregion

        #region ClearAllData
        private void ClearAllData()
        {
            if (mainMapControl.Map != null && mainMapControl.Map.LayerCount > 0)
            {
                //新建mainMapControl中Map
                IMap dataMap = new MapClass();
                dataMap.Name = "Map";
                mainMapControl.DocumentFilename = string.Empty;     //DocumentFilename  文档文件名字
                mainMapControl.Map = dataMap;

                //新建EagleEyeMapControl中Map
                IMap eagleEyeMap = new MapClass();
                eagleEyeMap.Name = "eagleEyeMap";
                EagleEyeMapControl.DocumentFilename = string.Empty;
                EagleEyeMapControl.Map = eagleEyeMap;
            }
        }
        #endregion
    }
}
原文地址:https://www.cnblogs.com/jiang2020/p/12593870.html