첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 시작 포인트 리스트
        /// </summary>
        private List<Point> startPointList = new List<Point>();

        /// <summary>
        /// 종료 포인트 리스트
        /// </summary>
        private List<Point> endPointList = new List<Point>();

        /// <summary>
        /// 그리기 여부
        /// </summary>
        private bool isDrawing = false;

        /// <summary>
        /// 신규 시작 포인트
        /// </summary>
        private Point newStartPoint;

        /// <summary>
        /// 신규 종료 포인트
        /// </summary>
        private Point newEndPoint;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.useCutCheckBox.CheckedChanged += useCutCheckBox_CheckedChanged;
            this.pictureBox.MouseDown          += pictureBox_MouseDown;
            this.pictureBox.MouseMove          += pictureBox_MouseMove;
            this.pictureBox.MouseUp            += pictureBox_MouseUp;
            this.pictureBox.Paint              += pictureBox_Paint;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 잘라내기 사용 체크 박스 체크 변경시 처리하기 - useCutCheckBox_CheckedChanged(sender, e)

        /// <summary>
        /// 잘라내기 사용 체크 박스 체크 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void useCutCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            this.pictureBox.Refresh();
        }

        #endregion
        #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.isDrawing = true;

            this.newStartPoint = e.Location;
            this.newEndPoint   = e.Location;
        }

        #endregion
        #region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if(!this.isDrawing)
            {
                return;
            }

            this.newEndPoint = e.Location;

            this.pictureBox.Refresh();
        }

        #endregion
        #region 픽처 박스 마우스 UP 처리하기 - pictureBox_MouseUp(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            this.isDrawing = false;

            if(this.newStartPoint == this.newEndPoint)
            {
                return;
            }

            this.startPointList.Add(this.newStartPoint);
            this.endPointList.Add(this.newEndPoint);

            this.pictureBox.Refresh();
        }

        #endregion
        #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e)

        /// <summary>
        /// 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            Pen backgroundPen = null;

            if(this.useCutCheckBox.Checked)
            {
                backgroundPen = new Pen(pictureBox.BackColor, 8);
            }

            using(Pen foregroundPen = new Pen(Color.Blue, 4))
            {
                for(int i = 0; i < this.startPointList.Count; i++)
                {
                    DrawLine
                    (
                        e.Graphics,
                        backgroundPen,
                        foregroundPen,
                        this.startPointList[i],
                        this.endPointList[i]
                    );
                }

                if(this.isDrawing)
                {
                    foregroundPen.Color = Color.Red;

                    DrawLine
                    (
                        e.Graphics,
                        backgroundPen,
                        foregroundPen,
                        this.newStartPoint,
                        this.newEndPoint
                    );
                }
            }

            if(backgroundPen != null)
            {
                backgroundPen.Dispose();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 선 그리기 - DrawLine(graphics, backgroundPen, foregroundPen, startPoint, endPoint)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="backgroundPen">배경 펜</param>
        /// <param name="foregroundPen">전경 펜</param>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        private void DrawLine(Graphics graphics, Pen backgroundPen, Pen foregroundPen, Point startPoint, Point endPoint)
        {
            if(backgroundPen != null)
            {
                graphics.DrawLine(backgroundPen, startPoint, endPoint);
            }

            graphics.DrawLine(foregroundPen, startPoint, endPoint);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요