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

C# - Ex06 - 그림판

반응형



MpuseDown 이벤트와 MouseUP 이벤트 를 해준다

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;

        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 Point start2;

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

        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            Graphics grfx = CreateGraphics();
            Pen pen = new Pen(lineColor, thickness);
            SolidBrush brush = new SolidBrush(fillColor);
            Rectangle rect =
                new Rectangle(start.X, start.Y,
                    e.X - start.X, e.Y - start.Y);
            switch (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,
                        start, new Point(e.X, e.Y));
                    break;
            }
            grfx.Dispose();//비관리형 자원 해제
        }


    }
}

 

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유