첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ Graphics 클래스 : DrawCurve 메소드를 사용해 곡선 그리기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip



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

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 포인트 리스트

        /// </summary>

        private List<Point> pointList = new List<Point>();

 

        /// <summary>

        /// 텐션

        /// </summary>

        private float tension = 0.5f;

 

        /// <summary>

        /// 그리기 여부

        /// </summary>

        private bool isDrawing = true;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.tensionScrollBar.Scroll     += tensionScrollBar_Scroll;

            this.canvasPictureBox.Paint      += canvasPictureBox_Paint;

            this.canvasPictureBox.MouseClick += canvasPictureBox_MouseClick;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 텐션 스크롤바 스크롤시 처리하기 - tensionScrollBar_Scroll(sender, e)

 

        /// <summary>

        /// 텐션 스크롤바 스크롤시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void tensionScrollBar_Scroll(object sender, ScrollEventArgs e)

        {

            this.tension = this.tensionScrollBar.Value / 10f;

 

            this.tensionLabel.Text = tension.ToString();

 

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

        #region 캔버스 픽처 박스 마우스 클릭시 처리하기 - canvasPictureBox_MouseClick(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 마우스 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void canvasPictureBox_MouseClick(object sender, MouseEventArgs e)

        {

            if(this.isDrawing)

            {

                if(e.Button == MouseButtons.Left)

                {

                    this.pointList.Add(e.Location);

                }

                else

                {

                    this.isDrawing = false;

                }

            }

            else

            {

                this.isDrawing = true;

 

                this.pointList = new List<Point>();

 

                this.pointList.Add(e.Location);

            }

 

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

        #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.SmoothingMode = SmoothingMode.AntiAlias;

 

            if(this.pointList.Count > 1)

            {

                using(Pen pen = new Pen(Color.Blue))

                {

                    if(this.isDrawing)

                    {

                        pen.DashPattern = new float[] { 5, 5 };

                    }

 

                    e.Graphics.DrawCurve(pen, this.pointList.ToArray(), this.tension);

                }

            }

 

            if(this.isDrawing && (this.pointList.Count > 0))

            {

                const int SIDE = 4;

 

                foreach(Point point in this.pointList)

                {

                    Rectangle rectangle = new Rectangle

                    (

                        point.X - SIDE,

                        point.Y - SIDE,

                        2 * SIDE,

                        2 * SIDE

                    );

 

                    e.Graphics.FillRectangle(Brushes.White, rectangle);

 

                    e.Graphics.DrawRectangle(Pens.Black, rectangle);

                }

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요