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

TestProject.zip
다운로드

▶ Segment.cs

using System.Drawing;

namespace TestProject
{
    /// <summary>
    /// 세그먼트
    /// </summary>
    public class Segment
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 펜
        /// </summary>
        public Pen Pen;

        /// <summary>
        /// 시작점
        /// </summary>
        public Point Point1;
        
        /// <summary>
        /// 종료점
        /// </summary>
        public Point Point2;

        #endregion

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

        #region 생성자 - Segment(pen, point1, point2)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="pen">펜</param>
        /// <param name="point1">시작점</param>
        /// <param name="point2">종료점</param>
        public Segment(Pen pen, Point point1, Point point2)
        {
            Pen    = pen;
            Point1 = point1;
            Point2 = point2;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 그리기 - Draw(graphics)

        /// <summary>
        /// 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        public void Draw(Graphics graphics)
        {
            graphics.DrawLine(Pen, Point1, Point2);
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

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<Segment> segmentList = new List<Segment>();

        /// <summary>
        /// 신규 세그먼트
        /// </summary>
        private Segment newSegment = null;

        #endregion

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

        #region 생성자 - MainForm()

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

            this.canvasPictureBox.Paint     += canvasPictureBox_Paint;
            this.canvasPictureBox.MouseDown += canvasPictureBox_MouseDown;
            this.canvasPictureBox.MouseMove += canvasPictureBox_MouseMove;
            this.canvasPictureBox.MouseUp   += canvasPictureBox_MouseUp;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

        /// <summary>
        /// 캔버스 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.canvasPictureBox.BackColor);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            foreach(Segment segment in this.segmentList)
            {
                segment.Draw(e.Graphics);
            }

            if(this.newSegment != null)
            {
                this.newSegment.Draw(e.Graphics);
            }
        }

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

        /// <summary>
        /// 캔버스 픽처 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void canvasPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.newSegment = new Segment(Pens.Blue, e.Location, e.Location);

            this.canvasPictureBox.Refresh();
        }

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

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

            this.canvasPictureBox.Refresh();
        }

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

        /// <summary>
        /// 캔버스 픽처 박스 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void canvasPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if(this.newSegment == null)
            {
                return;
            }

            this.newSegment.Pen = Pens.Black;

            this.segmentList.Add(newSegment);

            this.newSegment = null;

            this.canvasPictureBox.Refresh();
        }

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

댓글을 달아 주세요