using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using XTransMobile;
using System.Runtime.InteropServices;

namespace iTripLog.Edit
{
    public partial class Editor : Form
    {
         //布尔型变量，是否正在绘图
        private bool isDrawing = false;
        //绘图时记录鼠标的位置
        private Point startPoint, oldPoint;
        //枚举类型，各种绘图工具
        private enum drawTools
        {
            Pen = 0, Line, Ellipse, Rectangle, String, Rubber, None
        };
        //当前使用的工具
        private drawTools drawTool = drawTools.None;
        //当前编辑的文件名
        private string editFileName;
        //进行操作的位图
        private Image theImage;
        //绘制位图的Graphics实例
        private Graphics ig;
        //绘图使用的色彩
        private Color foreColor = Color.Black;
        private Color backColor = Color.White;

        private int LineWidth = 2;


        public Editor()
        {
            InitializeComponent();
        }

        //鼠标按下，开始绘图
        private void Editor_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (drawTool == drawTools.String)
            {
                strInput inputBox = new strInput();
                if (inputBox.ShowDialog() == DialogResult.OK)
                {
                    Graphics g = this.CreateGraphics();
                    Font theFont = this.Font;
                    g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                    ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                }
            }
            if (e.Button == MouseButtons.Left)
            {
                //如果开始绘制，则开始记录鼠标位置
                if ((isDrawing = !isDrawing) == true)
                {
                    startPoint = new Point(e.X, e.Y);
                    oldPoint = new Point(e.X, e.Y);
                }
            }

        }

        //鼠标移动
        private void Editor_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Graphics g;
            g = this.CreateGraphics();

            if (isDrawing)
            {
                switch (drawTool)
                {
                    case drawTools.None:
                        break;
                    case drawTools.Pen:
                        //从上一个点到当前点绘制线段
                        Point newPoint = new Point(e.X, e.Y);
                        g.DrawLine(new Pen(foreColor, LineWidth), oldPoint.X, oldPoint.Y, newPoint.X, newPoint.Y);
                        ig.DrawLine(new Pen(foreColor, LineWidth), oldPoint.X, oldPoint.Y, newPoint.X, newPoint.Y);
                        oldPoint.X = e.X;
                        oldPoint.Y = e.Y;
                        break;
                    case drawTools.Line:
                        //首先恢复此次操作之前的图像，然后再添加Line
                        Point newPoint2 = new Point(e.X, e.Y);
                        this.Editor_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawLine(new Pen(foreColor, LineWidth), startPoint.X, startPoint.Y, newPoint2.X, newPoint2.Y);
                        break;
                    case drawTools.Ellipse:
                        //首先恢复此次操作之前的图像，然后再添加Ellipse
                        this.Editor_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawEllipse(new Pen(foreColor, LineWidth), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                        break;
                    case drawTools.Rectangle:
                        //首先恢复此次操作之前的图像，然后再添加Rectangle
                        this.Editor_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
                        g.DrawRectangle(new Pen(foreColor, LineWidth), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                        break;
                    case drawTools.String:
                        break;
                    case drawTools.Rubber:
                        //用背景色绘制宽线段
                        Point newPoint4 = new Point(e.X, e.Y);
                        g.DrawLine(new Pen(backColor, 20), oldPoint.X, oldPoint.Y, newPoint4.X, newPoint4.Y);
                        ig.DrawLine(new Pen(backColor, 20), oldPoint.X, oldPoint.Y, newPoint4.X, newPoint4.Y);
                        oldPoint.X = e.X;
                        oldPoint.Y = e.Y;
                        break;
                }
            }
        }

        //鼠标松开，结束绘制
        private void Editor_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDrawing = false;
            switch (drawTool)
            {
                case drawTools.Line:
                    Point newPoint3 = new Point(e.X, e.Y);
                    ig.DrawLine(new Pen(foreColor, LineWidth), startPoint.X, startPoint.Y, newPoint3.X, newPoint3.Y);
                    break;
                case drawTools.Ellipse:
                    ig.DrawEllipse(new Pen(foreColor, LineWidth), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                    break;
                case drawTools.Rectangle:
                    ig.DrawRectangle(new Pen(foreColor, LineWidth), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
                    break;
            }
        }

        private void Editor_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            //将Image中保存的图像，绘制出来
            Graphics g = this.CreateGraphics();
            if (theImage != null)
            {
                g.Clear(Color.White);
                g.DrawImage(theImage, this.ClientRectangle.X, this.ClientRectangle.Y);
            }
        }

        private void PButtonPen_Click(object sender, EventArgs e)
        {
            drawTool = drawTools.Pen;
        }

        private void PButtonLine_Click(object sender, EventArgs e)
        {
            drawTool = drawTools.Line;
        }

        private void PButtonRect_Click(object sender, EventArgs e)
        {
            drawTool = drawTools.Rectangle;
        }

        private void PButtonEllipse_Click(object sender, EventArgs e)
        {
            drawTool = drawTools.Ellipse;
        }

        private void PButtonWord_Click(object sender, EventArgs e)
        {
            drawTool = drawTools.String;
        }

        private void PButtonEraser_Click(object sender, EventArgs e)
        {
            drawTool = drawTools.Rubber;
        }

        private void menuItemBack_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void menuItemFNew_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(backColor);
            //创建一个Bitsmap
            theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            editFileName = "新建文件";
            //修改窗口标题
            this.Text = "MyDraw\t" + editFileName;
            ig = Graphics.FromImage(theImage);
            ig.Clear(backColor);
        }

        private void menuItemFOpen_Click(object sender, EventArgs e)
        {
            openFileDg.Filter = "Image Files(*.bmp;*.wmf;*.ico;*.cur;*.jgp)|*.bmp;*.wmf;*.ico;*.cur;*.jpg";

            if (openFileDg.ShowDialog() == DialogResult.OK)
            {
                //修改窗口标题
                this.Text = "MyDraw\t" + openFileDg.FileName;
                editFileName = openFileDg.FileName;
                Bitmap bp = new Bitmap(editFileName);
                theImage = Image.FromHbitmap(bp.GetHbitmap());
                Graphics g = this.CreateGraphics();
                g.DrawImage(theImage, this.ClientRectangle.X, this.ClientRectangle.Y);
                ig = Graphics.FromImage(theImage);
                ig.DrawImage(theImage, this.ClientRectangle.X, this.ClientRectangle.Y);
            }

        }

        public void changeBackground(String fileName)
        {
            editFileName = fileName;
            Bitmap bp = new Bitmap(editFileName);
            theImage = Image.FromHbitmap(bp.GetHbitmap());
            Graphics g = this.CreateGraphics();
            g.DrawImage(theImage, this.ClientRectangle.X, this.ClientRectangle.Y);
            ig = Graphics.FromImage(theImage);
            ig.DrawImage(theImage, this.ClientRectangle.X, this.ClientRectangle.Y);

        }

        private void menuItemFSave_Click(object sender, EventArgs e)
        {
            saveFileDg.Filter = "图像(*.bmp)|*.bmp";
            saveFileDg.FileName = editFileName;
            if (saveFileDg.ShowDialog() == DialogResult.OK)
            {
                theImage.Save(saveFileDg.FileName, ImageFormat.Bmp);
                this.Text = "MyDraw\t" + saveFileDg.FileName;
                editFileName = saveFileDg.FileName;
            }
        }

        private void menuItemFQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuItemSet_Click(object sender, EventArgs e)
        {
            //set default mode
        }

        private void PButtonColor_Click(object sender, EventArgs e)
        {
            int[] rawCustom = new int[0x10];
            // 申请一段内存，提供从非托管内存访问托管对象的方法，防止垃圾回收器回收托管对象;
            // 如果没有这样的句柄，则在该对象代表非托管客户端完成工作以前，有可能被垃圾回收器回收
            // 这里强调静态性
            GCHandle custHandle = GCHandle.Alloc(rawCustom, GCHandleType.Pinned);
            ColorDialog.CHOOSECOLOR chsColor = new ColorDialog.CHOOSECOLOR();
            // 设置依托窗口
            chsColor.hwndOwner = ColorDialog.GetActiveWindow();
            ColorDialog.ColorFlags flag = ColorDialog.ColorFlags.AnyColor;
            // 设置Dialog类型
            chsColor.Flags = Convert.ToUInt32(flag);
            // 定制的颜色值(当然您也可以通过custHandle[index] = Color.FromArgb...的方式对每个对象赋值)
            chsColor.lpCustColors = custHandle.AddrOfPinnedObject();
            // 这里有一点要说明， 如果点击'OK'，则进入if内；否则如果点击'X'则不会进入if语句内
            if (ColorDialog.CreateColorDialog(chsColor))
            {
                // 获得颜色值
                this.foreColor = ConvertFromWin32(chsColor.rgbResult);
                string strShow = string.Format("0x{0:X8}", chsColor.rgbResult);
                System.Diagnostics.Debug.WriteLine(strShow);

                this.Invalidate();
            }

            // 记得一定要释放!
            custHandle.Free();

        }


        // 转换颜色值
        public Color ConvertFromWin32(int win32Color)
        {
            return Color.FromArgb(win32Color & 0xff, (win32Color >> 8) & 0xff, (win32Color >> 0x10) & 0xff);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.LineWidth = 1;
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.LineWidth = 2;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.LineWidth = 3;
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            this.LineWidth = 4;
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            this.LineWidth = 5;
        }
       


    }
}