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

namespace Paint_CS
{
    public partial class Form1 : Form
    {
        private int x_md, y_md;
        Pen myPen = new Pen(Color.LightBlue);
        private bool bPaint;
        Graphics g;

        private Pen erasePen;
        private Point ptsStart;
        private Point ptsPrevious;
        private Point ptsCurrent;

        public Form1()
        {
            InitializeComponent();

            #if DEBUG
                MinimizeBox = false;
            #else       
                MinimizeBox = true;
            #endif
            
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            // Начинаем рисование
            bPaint = true;

            if (mnuLines.Checked)
            {
                ptsStart.X = e.X;
                ptsStart.Y = e.Y;
                ptsPrevious = ptsStart;
            }

            if (mnuPaint.Checked)
            {
                // координаты мыши при нажатии
                x_md = e.X;
                y_md = e.Y;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (bPaint)
            {
                if (mnuLines.Checked)
                {
                    ptsCurrent.X = e.X;
                    ptsCurrent.Y = e.Y;

                    g = CreateGraphics();
                    g.DrawLine(erasePen, ptsStart.X, ptsStart.Y, ptsPrevious.X, ptsPrevious.Y);
                    g.DrawLine(myPen, ptsStart.X, ptsStart.Y, ptsCurrent.X, ptsCurrent.Y);

                    ptsPrevious = ptsCurrent;
                    g.Dispose();
                }

                if (mnuPaint.Checked)
                {
                    g = CreateGraphics();
                    int x_mm = e.X;
                    int y_mm = e.Y;
                    g.DrawLine(myPen, x_md, y_md, x_mm, y_mm);

                    x_md = x_mm;
                    y_md = y_mm;
                    g.Dispose();
               }
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            bPaint = false;
        }

        private void mnuClear_Click(object sender, EventArgs e)
        {
            g = CreateGraphics();
            g.Clear(this.BackColor);
            g.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            erasePen = new Pen(this.BackColor);
        }

        private void mnuPaint_Click(object sender, EventArgs e)
        {
            mnuPaint.Checked = !mnuPaint.Checked;
            mnuLines.Checked = !mnuLines.Checked;
        }

        private void mnuGreenPen_Click(object sender, EventArgs e)
        {
            myPen.Color = Color.Green;
        }

        private void mnuRedPen_Click(object sender, EventArgs e)
        {
            myPen.Color = Color.Red;
        }
    }
}