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 System.IO;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;//ʾ


namespace AE_k_12._19
{
    public partial class Form1 : Form
    {
        MapDocument pMapDocument;

        int state = 0;
        IGeometry pIGeometry;
        public Form1()
        {
            InitializeComponent();
        }

        //MapDocumentĵ
        private void loadMxdDocument()
        {
            pMapDocument = new MapDocument();
            try
            {
                OpenFileDialog pOpenFileDialog = new OpenFileDialog();
                pOpenFileDialog.Title = "򿪵ͼĵ";
                pOpenFileDialog.Filter = "map documents(*.mxd)|*.mxd";
                pOpenFileDialog.ShowDialog();
                string filePath = pOpenFileDialog.FileName;
                pMapDocument.Open(filePath, "");
                for (int i = 0; i < pMapDocument.MapCount; i++)
                {
                    axMapControl1.Map = pMapDocument.get_Map(i);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("ʧ" + e.ToString());
            }
        }

        private void saveasfile()
        {
            SaveFileDialog psave = new SaveFileDialog();
            psave.Title = "ͼĵ";
            psave.Filter = "map documents(*.mxd)|*.mxd";
            psave.ShowDialog();
            string filesavePath = psave.FileName;
            try
            {
                pMapDocument.SaveAs(filesavePath, true, true);
                MessageBox.Show("ɹ");
            }
            catch (Exception e)
            {
                MessageBox.Show("ʧ" + e.ToString());
            }
        }
        private void mxdļToolStripMenuItem_Click(object sender, EventArgs e)
        {
            loadMxdDocument();
        }

        private void ΪToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveasfile();
        }

      
        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            state = 1;
        }
        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            state = 2;
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            state = 3;
        }
        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            switch (state)
            { 
                case 1:
                    IPoint pPoint = axMapControl1.ToMapPoint(e.x, e.y);
                    pIGeometry = pPoint as IGeometry;
                    IRgbColor pIRgbColor =getColor(255,0,0);
                    drawShape(pIGeometry, pIRgbColor);
                   
                    break;
                case 2:
                    pIGeometry = axMapControl1.TrackLine();
                    IRgbColor pIRgbColor1 = getColor(255, 25, 50);
                    drawShape(pIGeometry, pIRgbColor1);
                    break;
                case 3:
                    pIGeometry = axMapControl1.TrackPolygon();
                    IRgbColor pIRgbColor2 = getColor(255, 150, 0);
                    drawShape(pIGeometry, pIRgbColor2);

                    break;

            }
        }


       
        //ͼηŻ
        public void drawShape(IGeometry piGeometry, IRgbColor iRgbColor)
        {
            object Symbol = null;
            if (piGeometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                ISimpleMarkerSymbol pISimpleMarkerSymbol = new SimpleMarkerSymbolClass();
                pISimpleMarkerSymbol.Color = iRgbColor;
                pISimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
                pISimpleMarkerSymbol.Size = 10;
                Symbol = pISimpleMarkerSymbol;
                axMapControl1.DrawShape(piGeometry, ref Symbol);
            }
            else if (piGeometry.GeometryType == esriGeometryType.esriGeometryPolyline)
            {
                ISimpleLineSymbol pISimpleLineSymbol = new SimpleLineSymbolClass();
                pISimpleLineSymbol.Color = iRgbColor;
                pISimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                pISimpleLineSymbol.Width = 1;
                Symbol = pISimpleLineSymbol;
                axMapControl1.DrawShape(piGeometry, ref Symbol);

            }
            else if (piGeometry.GeometryType == esriGeometryType.esriGeometryPolygon)
            {
                ISimpleFillSymbol pISimpleFillSymbol = new SimpleFillSymbolClass();
                pISimpleFillSymbol.Color = iRgbColor;
                pISimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSCross;
                Symbol = pISimpleFillSymbol;
                axMapControl1.DrawShape(piGeometry, ref Symbol);

            
            }
        }

        public IRgbColor getColor(int r, int g, int b)
        {
            IRgbColor pIRgbColor = new RgbColorClass();
            pIRgbColor.Red = r;
            pIRgbColor.Green = g;
            pIRgbColor.Blue = b;
            return pIRgbColor;
        }
       

       
    }
}
