프로그램/C# - Study / / 2010. 10. 27. 11:59

C# - Ex06 - 그림판2

반응형





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;

namespace Ex06
{
    enum ShapeType
    {
        Ellipse,
        Rectangle,
        Line
    }

    public partial class MainForm : Form
    {
        private ShapeType type = ShapeType.Ellipse;
        private int thickness = 1;
        private Color lineColor = Color.Black;
        private Color fillColor = Color.Orange;

        //Paint를 하기 위해 추가
        private LinkedList<ShapeInfo> shapes = new LinkedList<ShapeInfo>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void SelectShape(object sender, EventArgs e)
        {
            ToolStripMenuItem mi = (ToolStripMenuItem)sender;

            //MessageBox.Show(mi.Text);

            //switch (mi.Text)
            //{
            //    case "원": break;
            //    case "사각형": break;
            //    case "선": break;
            //}

            //설정한tag 값을 읽는다.
            switch (mi.Tag.ToString())
            {
                //SelectNumber
                case "1":
                    type = ShapeType.Ellipse;
                    break;
                case "2":
                    type = ShapeType.Rectangle;
                    break;
                case "3":
                    type = ShapeType.Line;
                    break;

            }
        }

        private void SelectColor(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            ToolStripMenuItem mi = (ToolStripMenuItem)sender;
            switch (mi.Tag.ToString())
            {
                case "1":
                    dlg.Color = lineColor;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        lineColor = dlg.Color;
                    }
                    break;

                case "2":
                    dlg.Color = fillColor;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        fillColor = dlg.Color;
                    }
                    break;
            }
        }

        private void SelectLineThickness(object sender, EventArgs e)
        {
            ToolStripMenuItem mi = (ToolStripMenuItem)sender;

            thickness = int.Parse(mi.Tag.ToString());
        }

        private Point start;

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            start = new Point(e.X, e.Y);
        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            //Paint를 하기 위해 추가&변경
            ShapeInfo info = new ShapeInfo
            {
                Type = type,
                Start = start,
                End = new Point(e.X, e.Y),
                FillColor = fillColor,
                LineColor = lineColor,
                Thickness = thickness
            };
            shapes.AddLast(info);

            Graphics grfx = CreateGraphics();
            DrawShape(grfx, info);
            grfx.Dispose();//비관리형 자원 해제

           
        }
        //Paint를 하기 위해 메서드 추가
        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics grfx = e.Graphics;

            foreach (ShapeInfo info in shapes)
            {               
                DrawShape(grfx, info);
            }
        }

        private void DrawShape(Graphics grfx, ShapeInfo info)
        {
            SolidBrush brush = new SolidBrush(info.FillColor);
            Pen pen = new Pen(info.LineColor, info.Thickness);
            //Rectangle rect = new Rectangle(
            // info.Start.X, info.Start.Y,
            // info.End.X - info.Start.X,
            // info.End.Y - info.Start.Y);
            //Paint를 하기 위해 변경
            Rectangle rect =
                 GetPositiveRectangle(info.Start, info.End);

            switch (info.Type)
            {
                case ShapeType.Ellipse:
                    grfx.FillEllipse(brush, rect);
                    grfx.DrawEllipse(pen, rect);
                    break;

                case ShapeType.Rectangle:
                    grfx.FillRectangle(brush, rect);
                    grfx.DrawRectangle(pen, rect);
                    break;

                case ShapeType.Line:
                    grfx.DrawLine(pen,
                        info.Start, info.End);
                    break;
            }
        }
        //Paint를 하기 위해 메서드 추가( 사각형 자유롭게 그리기 위해)
        private Rectangle GetPositiveRectangle(Point start, Point end)
        {
            Rectangle rect = new Rectangle
            {
                X = end.X > start.X ? start.X : end.X,
                Y = end.Y > start.Y ? start.Y : end.Y,
                Width = Math.Abs(end.X - start.X),
                Height = Math.Abs(end.Y - start.Y)
            };
            return rect;
        }

    }
}

 

반응형

'프로그램 > C# - Study' 카테고리의 다른 글

C# - Ex07 - Brush 다루기  (0) 2010.10.28
C# - Ex06 - 그림판3  (0) 2010.10.27
C# - Ex06 - 그림판  (0) 2010.10.27
C# - Ex05 - 메모장  (0) 2010.10.26
C# - Ex04 - 탐색기 만들기  (3) 2010.10.22
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유