none
마이크로소프트개지랄 RRS feed

  • 질문

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    namespace 이우승_과제16
    {
        public partial class Form1 : Form
        {
            public Pen mypen;
            public Point now;
            public Point start;
            public ArrayList save;
            private int darwmode;
            private bool fillFlag;
            private Pen backPen;
            private Brush myBrush;
            ArrayList save1;

            int[] count = new int[500];
            int i = 0;
            int totalcount = 0;
            int k;
           
            public Form1()
            {
                InitializeComponent();
                darwmode = 1;
                mypen = new Pen(Color.Black);
                save = new ArrayList();
                save1 = new ArrayList();
               
            }

            private void Form1_Load(object sender, EventArgs e)
            {

                SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                SetStyle(ControlStyles.UserPaint, true);
                this.ResizeRedraw = true;     
            }

            private void 선ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                darwmode = 1;
                Invalidate();
                Update();
            }
           
            private void 원ToolStripMenuItem_Click(object sender, EventArgs e)
            {               
                darwmode = 2;
                Invalidate();
                Update();         
               
            }     
           
           
            private void 사각형ToolStripMenuItem_Click(object sender, EventArgs e)
            {
               
                darwmode = 3;
                Invalidate();
                Update();
               
            }

            private void 곡선ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                darwmode = 4;
                Invalidate();
                Update();          
            }
           

            private void 화면삭제ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                save.Clear();
                save1.Clear();
                fillFlag = false;
                Invalidate();
                this.Update();
            }

            private void 취소ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (save.Count > 0)
                {
                    for (k = 1; k <= count[i - 1]; k++)
                    {
                        save.RemoveAt(totalcount - k);
                    }
                    for (k = 1; k <= count[i - 1]; k++)
                    {
                        totalcount--;
                    }

                    Invalidate();
                    this.Update();
                    i--;
                    count[i] = 0;
                    if (i < 0)
                        i = 0;
                    if (totalcount < 0)
                        totalcount = 0;
                }

            }

            private void 종료ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Close();
            }

            private void 펜색ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                colorPen.ShowDialog();
                mypen.Color = colorPen.Color;
            }

            private void 색선택ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                fillFlag = true;
                colorBrush.ShowDialog();
                myBrush = new SolidBrush(colorBrush.Color);
            }

            private void 널브러시ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                fillFlag = false;
            }

            private void 굵은선ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                mypen.Width = 5;
            }

            private void 보통선ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                mypen.Width = 3;
            }

            private void 가는선ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                mypen.Width = 1;
            }

            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left) return;
                else
                {
                    now = new Point(e.X, e.Y);
                    start = now;
                    if (darwmode != 2 && darwmode != 5)
                    {
                        count[i]++;
                    }
                }
            }

            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {

                if (e.Button != MouseButtons.Left) return;

                else
                {
                    now = new Point(e.X, e.Y);
                    Graphics g = this.CreateGraphics();
                    Invalidate();
                    this.Update();

                    Rectangle rect;
                    switch (darwmode)
                    {
                        case 1:
                            g.DrawLine(mypen, start, now);
                            count[i] = 1;
                            break;

                        case 2:
                            g.DrawLine(mypen, start, now);
                            DrawData inputData = new DrawData(start, now, mypen.Color, darwmode, mypen.Width);
                            save.Add(inputData);
                            SaveData sd = new SaveData(start, now, mypen.Color, darwmode, mypen.Width);
                            save1.Add(sd);
                            start = now;
                            count[i]++;
                            break;
                        case 3:
                            rect = new Rectangle(start.X, start.Y, now.X - start.X, now.Y - start.Y);
                            if (fillFlag) g.FillEllipse(myBrush, rect);
                            g.DrawEllipse(mypen, rect);
                            count[i] = 1;
                            break;
                        case 4:
                            rect = new Rectangle(start.X, start.Y, now.X - start.X, now.Y - start.Y);
                            if (fillFlag) g.FillRectangle(myBrush, rect);
                            g.DrawRectangle(mypen, rect);
                            count[i] = 1;
                            break;
                        case 5:
                            g.DrawLine(backPen, start, now);
                            DrawData inputData2 = new DrawData(start, now, backPen.Color, darwmode, backPen.Width);
                            save.Add(inputData2);
                            sd = new SaveData(start, now, backPen.Color, darwmode, backPen.Width);
                            save1.Add(sd);
                            start = now;
                            count[i]++;
                            break;
                    }
                    g.Dispose();
                }

            }

            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                DrawData inputdata;
                SaveData inputdata3;
                switch (darwmode)
                {
                    case 1:
                        inputdata = new DrawData(start, now, mypen.Color, darwmode, mypen.Width);
                        save.Add(inputdata);
                        inputdata3 = new SaveData(start, now, mypen.Color, darwmode, mypen.Width);
                        save1.Add(inputdata3);
                        break;
                    case 2:
                        break;
                    case 3:
                        inputdata = new DrawData(start, now, mypen.Color, colorBrush.Color, fillFlag, darwmode, mypen.Width);
                        save.Add(inputdata);
                        inputdata3 = new SaveData(start, now, mypen.Color, colorBrush.Color, fillFlag, darwmode, mypen.Width);
                        save1.Add(inputdata3);
                        break;
                    case 4:
                        inputdata = new DrawData(start, now, mypen.Color, colorBrush.Color, fillFlag, darwmode, mypen.Width);
                        save.Add(inputdata);
                        inputdata3 = new SaveData(start, now, mypen.Color, colorBrush.Color, fillFlag, darwmode, mypen.Width);
                        save1.Add(inputdata3);
                        break;
                    case 5:
                        break;
                }
                totalcount += count[i];

                i++;
                Invalidate();

            }


            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Graphics g = e.Graphics;
                foreach (DrawData outdata in save)
                {
                    outdata.drawData(e.Graphics);
                }
                           
            }

            private void 저장ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                saveFileDialog1.ShowDialog();
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, save1);
                fs.Close();
                save1.Clear();
            }

            private void 열기ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                save.Clear();
                save1.Clear();
                openFileDialog1.ShowDialog();
                FileStream fs1 = new FileStream(openFileDialog1.FileName, FileMode.Open);
                BinaryFormatter bf1 = new BinaryFormatter();
                save1 = (ArrayList)bf1.Deserialize(fs1);
                fs1.Close();
                DrawData inputdata;

                foreach (SaveData outdata in save1)
                {
                    switch (outdata.darwmode)
                    {
                        case 1:
                            inputdata = new DrawData(outdata.startPoint, outdata.nowPoint, outdata.pcolor,
                            outdata.darwmode, outdata.width);
                            save.Add(inputdata);
                            break;
                        case 2:
                            inputdata = new DrawData(outdata.startPoint, outdata.nowPoint, outdata.pcolor,
                            outdata.darwmode, outdata.width);
                            save.Add(inputdata);
                            break;
                        case 3:
                            inputdata = new DrawData(outdata.startPoint, outdata.nowPoint, outdata.pcolor,
                            outdata.bcolor, outdata.fill, outdata.darwmode, outdata.width);
                            save.Add(inputdata);
                            break;
                        case 4:
                            inputdata = new DrawData(outdata.startPoint, outdata.nowPoint, outdata.pcolor,
                            outdata.bcolor, outdata.fill, outdata.darwmode, outdata.width);
                            save.Add(inputdata);
                            break;
                        case 5:
                            inputdata = new DrawData(outdata.startPoint, outdata.nowPoint, outdata.pcolor,
                            outdata.darwmode, outdata.width);
                            save.Add(inputdata);
                            break;
                    }
                }
                Invalidate();
            }

            [Serializable]
            public class SaveData
            {
                public Point startPoint;
                public Point nowPoint;
                public Color pcolor;
                public Color bcolor;
                public bool fill;
                public int darwmode;
                public float width;
                public SaveData(Point x, Point y, Color p, int dMode, float w)
                {
                    startPoint = x;
                    nowPoint = y;
                    pcolor = p;
                    darwmode = dMode;
                    width = w;
                }

                public SaveData(Point x, Point y, Color p, Color b, bool f, int dMode, float w)
                {
                    startPoint = x;
                    nowPoint = y;
                    pcolor = p;
                    bcolor = b;
                    fill = f;
                    darwmode = dMode;
                    width = w;
                }

            }     


        }
    }

     

    2009년 12월 9일 수요일 오전 1:13