using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
namespace EngineWindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int flag = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pWorkSpace = pWorkspaceFactory.OpenFromFile(Application.StartupPath+"\\实验一数据",0);
            IFeatureWorkspace pFeatureWorkspace= pWorkSpace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass("塔林林场");
            IFeatureLayer pFeatureLayer=new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            axMapControl1.AddLayer(pFeatureLayer as ILayer);
            axMapControl2.AddLayer(pFeatureLayer as ILayer);
        }

        private void extendToolStripMenuItem_Click(object sender, EventArgs e)//fangda 
        {
            flag = 1;
            axMapControl1.MousePointer = esriControlsMousePointer.esriPointerZoomIn;
        }

        private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 2;
            axMapControl1.MousePointer = esriControlsMousePointer.esriPointerZoomOut;
        }

        private void 移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 3;
            axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPan;
        }

        private void 全图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl1.Extent = axMapControl1.FullExtent;
        }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (flag == 1)
                axMapControl1.Extent = axMapControl1.TrackRectangle();
            else if(flag==3)
                axMapControl1.Pan();
            else if (flag == 2)
            {
                IEnvelope pEnvelope = axMapControl1.Extent;
                pEnvelope.Expand(2, 2, true);
                axMapControl1.Extent = pEnvelope;
            }
        }

        private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            IEnvelope envelope = (IEnvelope)e.newEnvelope;
            IGraphicsContainer graphicsContainer = axMapControl2.Map as IGraphicsContainer;
            IActiveView activeView = graphicsContainer as IActiveView;
            //在绘图前,清除axMapControl2中的任何图形元素
            graphicsContainer.DeleteAllElements();
            IElement element = new RectangleElementClass();
            element.Geometry = envelope;
            //设置鹰眼图中的红线
            //产生一个线符号对象
            ILineSymbol outLineSymbol = new SimpleLineSymbolClass();
            outLineSymbol.Width = 2;
            outLineSymbol.Color = GetColor(255, 0, 0, 255);
            //设置颜色属性
            //设置填充符号的属性
            IFillSymbol fillSymbol = new SimpleFillSymbolClass();
            fillSymbol.Color = GetColor(9, 0, 0, 0);
            fillSymbol.Outline = outLineSymbol;
            IFillShapeElement fillShapeElement = element as IFillShapeElement;
            fillShapeElement.Symbol = fillSymbol;
            graphicsContainer.AddElement((IElement)fillShapeElement, 0);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }
        private IRgbColor GetColor(int r, int g, int b, int t)
        {
            IRgbColor rgbColor = new RgbColorClass();
            rgbColor.Red = r;
            rgbColor.Green = g;
            rgbColor.Blue = b;
            rgbColor.Transparency = (byte)t;
            return rgbColor;
        }

        private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            IPoint point = new PointClass();
            point.PutCoords(e.mapX, e.mapY);
            axMapControl1.CenterAt(point);
        }
    }
}