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.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
namespace AE03_09
{
    public partial class Form1 : Form
    {
        int flag = 0;//״̬
        int zt = 0;
        IPoint pPoint1 = new PointClass();
        IPoint pPoint2 = new PointClass();
        public Form1()
        {
            InitializeComponent();
        }

        private void ԲĹToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 1;//Բ
        }
        //ɫ
        public IRgbColor getRGB(int r,int g,int b,byte t)
        {
            IRgbColor pRgbColor = new RgbColorClass();
            pRgbColor.Red = r;
            pRgbColor.Green = g;
            pRgbColor.Blue = b;
            pRgbColor.Transparency = t;//͸
            return pRgbColor;
        }
        //Բ
        public void drawCircle(IPoint point,double r)
        {
            //ԲĶ
            IConstructCircularArc pConstructCiercularArc = new CircularArcClass();//Բ
            pConstructCiercularArc.ConstructCircle(point, r, false);
            ICircularArc pArc = pConstructCiercularArc as ICircularArc;
            ISegment pSegment = pArc as ISegment;//
            ISegmentCollection pSegmentCollection = new RingClass();//ͨISegmentCollectionRing
            object o = Type.Missing;//ʼ
            //Segment
            pSegmentCollection.AddSegment(pSegment, ref o, ref o);
            IRing pRing = pSegmentCollection as IRing;//ӿڲѯ
            pRing.Close();
            //polygon
            IGeometryCollection pGeometryColl = new PolygonClass();
            pGeometryColl.AddGeometry(pRing, ref o, ref o);
            //һCircleEelment
            IElement pElement = new CircleElementClass();
            pElement.Geometry = pGeometryColl as IGeometry;
            //Ż
            ISimpleLineSymbol pSimpleLineSymbol = null;
            pSimpleLineSymbol = new SimpleLineSymbolClass();//߿ʽ
            pSimpleLineSymbol.Color = getRGB(255, 0, 0, 1);
            pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDashDotDot;
            pSimpleLineSymbol.Width = 1;
            ISimpleFillSymbol pSimpleFillSymbol = null;
            pSimpleFillSymbol = new SimpleFillSymbolClass();//ʽ
            pSimpleFillSymbol.Color = getRGB(100, 100, 100, 1);
            pSimpleFillSymbol.Outline = pSimpleLineSymbol;
            pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
            IFillShapeElement pFillShapeElement = null;
            pFillShapeElement = pElement as IFillShapeElement;//shapeԪʽ
            pFillShapeElement.Symbol = pSimpleFillSymbol;
            IGraphicsContainer pGraphicsContaniner = axMapControl1.ActiveView.GraphicsContainer;
            pGraphicsContaniner.AddElement(pElement, 0);
            axMapControl1.Refresh();       
        }
        //Բ
        public void drawEllipse(IEnvelope env)
        {

            IConstructEllipticArc pConstructEllipticArc = new EllipticArcClass();//Բ
            pConstructEllipticArc.ConstructEnvelope(env);//ͨ߹Բ
            //pConstructEllipticArc.ConstructQuarterEllipse(pPoint1, pPoint2, true);Բ淽
            IEllipticArc pEllipticArc = pConstructEllipticArc as IEllipticArc;//Բ
            ISegment pSegment1 = pEllipticArc as ISegment;
            object o1 = Type.Missing;
            ISegmentCollection pSegmentCollection1 = new RingClass();
            pSegmentCollection1.AddSegment(pSegment1, ref o1, ref o1);//Բ
            IRing pRing1 = pSegmentCollection1 as IRing;
            pRing1.Close();//Բ
            IGeometryCollection pGeometryCollecton = new PolygonClass();
            pGeometryCollecton.AddGeometry(pRing1, ref o1, ref o1);
            IElement pElement1 = null;
            pElement1 = new EllipseElementClass();
            pElement1.Geometry = pGeometryCollecton as IGeometry;
            //Ż
            ISimpleLineSymbol pSimpleLineSymbol = null;
            pSimpleLineSymbol = new SimpleLineSymbolClass();//߿ʽ
            pSimpleLineSymbol.Color = getRGB(255, 0, 0, 1);
            pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDashDotDot;
            pSimpleLineSymbol.Width = 1;
            ISimpleFillSymbol pSimpleFillSymbol = null;
            pSimpleFillSymbol = new SimpleFillSymbolClass();//ʽ
            pSimpleFillSymbol.Color = getRGB(100, 100, 100, 1);
            pSimpleFillSymbol.Outline = pSimpleLineSymbol;
            pSimpleFillSymbol = new SimpleFillSymbolClass();//ʽ
            pSimpleFillSymbol.Color = getRGB(100, 100, 100, 1);
            pSimpleFillSymbol.Outline = pSimpleLineSymbol;
            pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
            IFillShapeElement pFillShapeElement = null;
            pFillShapeElement = pElement1 as IFillShapeElement;//shapeԪʽ
            pFillShapeElement.Symbol = pSimpleFillSymbol;
            IGraphicsContainer pGraphicsContaniner1 = axMapControl1.ActiveView.GraphicsContainer;
            pGraphicsContaniner1.AddElement(pElement1, 0);
            axMapControl1.Refresh();
        
        }

        //ԲĹ

        public void drawCircleArc(IPoint point,double r)
        { 
          //Բ
            IPoint pPoint = point;
            IConstructCircularArc pConstructCircularArc = new CircularArcClass();//Բ
            pConstructCircularArc.ConstructBearingRadiusAngle(pPoint,0,false,r,Math.PI*4/3);
            ICircularArc pCircularArc = pConstructCircularArc as ICircularArc;//תԲ
            ISegment pSegment = pCircularArc as ISegment;//תɲ֣
            ISegmentCollection pSegmentCollection = new RingClass();//
            object o = Type.Missing;
            pSegmentCollection.AddSegment(pSegment,ref o,ref o);
            IRing pRing = pSegmentCollection as IRing;//תɻ
            IGeometryCollection pGeometryCollection = new PolygonClass();
            pGeometryCollection.AddGeometry(pSegmentCollection as IGeometry,ref o,ref o);
            //Բʾ
            IElement pElement = new CircleElementClass();
            pElement.Geometry = pGeometryCollection as IGeometry;
            IGraphicsContainer pGraphicsContainer = axMapControl1.ActiveView.GraphicsContainer;
            pGraphicsContainer.AddElement(pElement,0);
            axMapControl1.Refresh();
        }

        //

        public void drawCircleArc2(IPoint point, double r)
        {
            //Բ
            IPoint pPoint = point;
            IConstructCircularArc pConstructCircularArc = new CircularArcClass();//Բ
            pConstructCircularArc.ConstructBearingRadiusAngle(pPoint, 0, false, r, Math.PI);
            //pConstructCircularArc.ConstructCircle(pPoint,r,false);
            ICircularArc pCircularArc = pConstructCircularArc as ICircularArc;//תԲ
            ISegment pSegment = pCircularArc as ISegment;//תɲ֣
            ISegmentCollection pSegmentCollection = new PolylineClass();//
            object o = Type.Missing;
            pSegmentCollection.AddSegment(pSegment, ref o, ref o);
            IGeometry pGeo = pSegmentCollection as IGeometry;
            //Բʾ
            IElement pElement = new LineElementClass();
            pElement.Geometry = pGeo;
            IGraphicsContainer pGraphicsContainer = axMapControl1.ActiveView.GraphicsContainer;
            pGraphicsContainer.AddElement(pElement, 0);
            axMapControl1.Refresh();
        }

        //Բ

        public void drawEllipticArc(IPoint point1, IPoint point2)
        {
            //Բ
            IPoint pPoint1 = point1;
            IPoint pPoint2 = point2;
            IConstructEllipticArc pConstructEllipticArc=new EllipticArcClass();
            pConstructEllipticArc.ConstructQuarterEllipse(pPoint1, pPoint2, true);//Բ淽
            IEllipticArc pCircularArc = pConstructEllipticArc as IEllipticArc;//תԲ
            ISegment pSegment = pCircularArc as ISegment;//תɲ֣
            ISegmentCollection pSegmentCollection = new PolylineClass();//
            object o = Type.Missing;
            pSegmentCollection.AddSegment(pSegment, ref o, ref o);
            IGeometry pGeo = pSegmentCollection as IGeometry;
            //Բʾ
            IElement pElement = new LineElementClass();
            pElement.Geometry = pGeo;
            IGraphicsContainer pGraphicsContainer = axMapControl1.ActiveView.GraphicsContainer;
            pGraphicsContainer.AddElement(pElement, 0);
            axMapControl1.Refresh();
        }


        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            switch(flag)
            {
                case 1://ԲĹ
                    IEnvelope pEnvelope = axMapControl1.TrackRectangle();
                    double x = (pEnvelope.Envelope.XMax - pEnvelope.Envelope.XMin) / 2 + pEnvelope.Envelope.XMin;
                    double y = (pEnvelope.Envelope.YMax - pEnvelope.Envelope.YMin) / 2 + pEnvelope.Envelope.YMin;
                    double r = (pEnvelope.Envelope.YMax - pEnvelope.Envelope.YMin) / 2;
                    IPoint pPoint = new PointClass();//Բ
                    pPoint.X = x;
                    pPoint.Y = y;
                    drawCircle(pPoint,r);
                    break;
                case 2://Բ
                    ////IPoint pPoint1 = new PointClass();
                    ////IPoint pPoint2 = new PointClass();
                    //if (zt == 0)
                    //{
                    //    pPoint1.PutCoords(e.mapX, e.mapY);
                    //    zt = 1;
                    //}
                    //else
                    //{
                    //    pPoint2.PutCoords(e.mapX, e.mapY);
                    //    zt = 0;
                    //}
                    ////pPoint2.PutCoords(80, 120);
                    ////pPoint2.PutCoords(330, 300);

                    //IEnvelope pEnvelope = new EnvelopeClass();
                    //pEnvelope.LowerLeft = pPoint1;
                    //pEnvelope.LowerRight = pPoint2;
                    IEnvelope pEnvelope1 = axMapControl1.TrackRectangle();
                    drawEllipse(pEnvelope1);
                    break;
                case 3://Բ
                    //IPoint pPoint1 = new PointClass();
                    //pPoint1.PutCoords(e.mapX,e.mapY);
                    //double r1 = 200;
                     pEnvelope = axMapControl1.TrackRectangle();
                     x = (pEnvelope.Envelope.XMax - pEnvelope.Envelope.XMin) / 2 + pEnvelope.Envelope.XMin;
                     y = (pEnvelope.Envelope.YMax - pEnvelope.Envelope.YMin) / 2 + pEnvelope.Envelope.YMin;
                     r = (pEnvelope.Envelope.YMax - pEnvelope.Envelope.YMin) / 2;
                    IPoint pPoint1 = new PointClass();//Բ
                    pPoint1.X = x;
                    pPoint1.Y = y;
                    drawCircleArc(pPoint1,r);
                    break;
                case 4://Բ
                    IPoint pPoint2 = new PointClass();
                    pPoint2.PutCoords(e.mapX, e.mapY);
                    double r2 = 200;
                    drawCircleArc2(pPoint2, r2);
                    break;
                case 5:
                    IPoint px1 = new PointClass();
                    IPoint px2 = new PointClass();
                    //px1.PutCoords(105, 120);
                    //px2.PutCoords(600, 335);
                    //drawEllipticArc(px1, px2);


                    pEnvelope = axMapControl1.TrackRectangle();
                    px1.X = pEnvelope.Envelope.XMin;
                    px1.Y = pEnvelope.Envelope.YMin;
                    px2.X = pEnvelope.Envelope.XMax;
                    px2.Y = pEnvelope.Envelope.YMax;
                    drawEllipticArc(px1, px2);
                    break;
            }
        }
       

        private void ԲĹToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 2;//Բ
        }

        private void ԲĹToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 3;//Բ
        }

        private void ĹToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 4;//
        }

        private void ԲߵĹToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 5;//Բ
        }
        
    }
}
