較為完整的鷹眼實現,跟大家共用一下

來源:互聯網
上載者:User
主要實現了一下幾點:1.載入mxd檔案同步;OnMapReplaced事件2.載入圖層檔案同步;OnItemAdded事件3.TOC中圖層勾選狀態與鷹眼中圖層可見狀態同步;ContentsChanged事件4.鷹眼視圖中右鍵框選主視圖中顯示地區;5.鷹眼視圖中左鍵單擊確定矩形框中心;6.TOC右鍵菜單刪除圖層;
 
 
  using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ESRI.ArcGIS.Carto;using System.Diagnostics;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.Controls;namespace DEMO1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        IActiveViewEvents_Event pAE;        private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)        {            axMapControl2.ClearLayers();//每次主視圖地圖更換時,清空鷹眼中原有圖層            IMap pMap = e.newMap as IMap;//參數e是object類型,要進行介面轉換                        int n = pMap.LayerCount;            for (int i = n - 1; i >= 0; i--)            {                axMapControl2.AddLayer(pMap.get_Layer(i));            }            this.pAE = e.newMap as IActiveViewEvents_Event;            pAE.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(this.OnItemAdded);            pAE.ContentsChanged += new IActiveViewEvents_ContentsChangedEventHandler(pAE_ContentsChanged);        }        void pAE_ContentsChanged()        {            IBasicMap map = null;            ILayer layer = null;            Object other = null;            Object index = null;            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;            axTOCControl1.GetSelectedItem(ref item, ref map, ref layer, ref other, ref index);            ILayer player;            for (int i = 0; i < axMapControl2.LayerCount; i++)            {                player = axMapControl2.get_Layer(i);                if (player.Name == layer.Name)                {                    if (layer.Visible == true)                        player.Visible = true;                    else                        player.Visible = false;                    axMapControl2.Refresh();                    break;                }            }        }        private void Form1_Load(object sender, EventArgs e)        {            //用代碼設定夥伴控制項更保險            axTOCControl1.SetBuddyControl(axMapControl1);            axToolbarControl1.SetBuddyControl(axMapControl1);            //禁用鷹眼MapControl的滾輪放大縮小功能            axMapControl2.AutoMouseWheel = false;            IMap pMap;            pMap = axMapControl1.Map;//主視圖             pAE = pMap as IActiveViewEvents_Event;            pAE.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(this.OnItemAdded);            pAE.ContentsChanged+=new IActiveViewEvents_ContentsChangedEventHandler(pAE_ContentsChanged);        }        private void OnItemAdded(object item)        {            ILayer pLayer;            pLayer = item as ILayer;            axMapControl2.AddLayer(pLayer,0);//鷹眼視圖            axMapControl2.Refresh();        }        private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)        {            try            {                ESRI.ArcGIS.Geometry.IEnvelope envelope = (ESRI.ArcGIS.Geometry.IEnvelope)e.newEnvelope;                ESRI.ArcGIS.Carto.IGraphicsContainer gc = axMapControl2.Map as ESRI.ArcGIS.Carto.IGraphicsContainer;                ESRI.ArcGIS.Carto.IActiveView ac = gc as ESRI.ArcGIS.Carto.IActiveView;                //在繪製前,清除axMapControl2中的內容                gc.DeleteAllElements();                ESRI.ArcGIS.Carto.IElement elment = new ESRI.ArcGIS.Carto.RectangleElementClass();                elment.Geometry = envelope;                //設定鷹眼中的紅線                //產生一個線符號對象                ESRI.ArcGIS.Display.ILineSymbol outLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();                outLineSymbol.Width = 2;                outLineSymbol.Color = GetColor(255, 0, 0, 255);                //設定顏色屬性                //設定填充符號的屬性                ESRI.ArcGIS.Display.IFillSymbol fillsymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();                fillsymbol.Color = GetColor(9, 0, 0, 0);                fillsymbol.Outline = outLineSymbol;                ESRI.ArcGIS.Carto.IFillShapeElement fillshapeelement = elment as ESRI.ArcGIS.Carto.IFillShapeElement;                fillshapeelement.Symbol = fillsymbol;                gc.AddElement((ESRI.ArcGIS.Carto.IElement)fillshapeelement, 0);                ac.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null);            }            catch(Exception eCatch)            {                MessageBox.Show(eCatch.ToString()+"\n鷹眼中繪製紅色矩形框出錯");            }        }        private ESRI.ArcGIS.Display.IRgbColor GetColor(int r, int g, int b, int t)        {            ESRI.ArcGIS.Display.IRgbColor rgb = new ESRI.ArcGIS.Display.RgbColorClass();            rgb.Red = r;            rgb.Green = g;            rgb.Blue = b;            rgb.Transparency = (byte)t;            return rgb;        }        //右鍵畫矩形選定主視圖顯示範圍        private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)        {            if (e.button == 2)            {                try                {                    IPoint pPoint =new PointClass();                    pPoint.X = e.mapX;                    pPoint.Y = e.mapY;                    IEnvelope pEnvelop = axMapControl2.TrackRectangle();                    axMapControl1.Extent = pEnvelop;                }                catch                {                }            }        }        private void axMapControl2_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)        {            if (e.button == 1)            {                try                {                    IPoint pPoint = new PointClass();                    pPoint.X = e.mapX;                    pPoint.Y = e.mapY;                                        IEnvelope pEnvelop = axMapControl1.Extent as IEnvelope;                    pEnvelop.CenterAt(pPoint);                    IGraphicsContainer pGraphicContainer = axMapControl2.Map as IGraphicsContainer;                    IActiveView pAV = pGraphicContainer as IActiveView;                    pGraphicContainer.DeleteAllElements();                    ESRI.ArcGIS.Carto.IElement elment = new ESRI.ArcGIS.Carto.RectangleElementClass();                    elment.Geometry = pEnvelop;                    //設定鷹眼中的紅線                    //產生一個線符號對象                    ESRI.ArcGIS.Display.ILineSymbol outLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();                    outLineSymbol.Width = 2;                    outLineSymbol.Color = GetColor(255, 0, 0, 255);                    //設定顏色屬性                    //設定填充符號的屬性                    ESRI.ArcGIS.Display.IFillSymbol fillsymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();                    fillsymbol.Color = GetColor(9, 0, 0, 0);                    fillsymbol.Outline = outLineSymbol;                    ESRI.ArcGIS.Carto.IFillShapeElement fillshapeelement = elment as ESRI.ArcGIS.Carto.IFillShapeElement;                    fillshapeelement.Symbol = fillsymbol;                    pGraphicContainer.AddElement((ESRI.ArcGIS.Carto.IElement)fillshapeelement, 0);                    pAV.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null);                }                catch                {                }            }        }        private void axMapControl2_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)        {            if (e.button == 1)            {                try                {                    IPoint pPoint = new PointClass();                    pPoint.X = e.mapX;                    pPoint.Y = e.mapY;                    IEnvelope pEnvelop = axMapControl1.Extent as IEnvelope;                    pEnvelop.CenterAt(pPoint);                    axMapControl1.Extent = pEnvelop;                }                catch                {                                    }            }        }        private void 刪除圖層ToolStripMenuItem_Click(object sender, EventArgs e)        {            if (SelectedLayer != null)            {                axMapControl1.Map.DeleteLayer(SelectedLayer);                axMapControl2.Map.DeleteLayer(SelectedLayer);                SelectedLayer = null;            }        }        ILayer SelectedLayer;        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)        {            if (e.button == 2)            {                IBasicMap map = null;                ILayer layer = null;                Object other = null;                Object index = null;                esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;                axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);                                if (item == esriTOCControlItem.esriTOCControlItemLayer)                {                    SelectedLayer = layer;                    TOCMenuStrip1.Show(axTOCControl1, e.x, e.y);                }            }        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.