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

TestProject.zip
다운로드

▶ MainForm.cs

using System;
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 bool selectingArea = false;

        /// <summary>
        /// 시작 포인트
        /// </summary>
        private Point startPoint;
        
        /// <summary>
        /// 종료 포인트
        /// </summary>
        private Point endPoint;

        #endregion

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

        #region 생성자 - MainForm()

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

            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 픽처 박스 마우스 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.selectingArea = true;
            this.startPoint    = e.Location;
            this.endPoint      = e.Location;

            this.pictureBox.Cursor = Cursors.Cross;

            this.pictureBox.Refresh();
        }

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

        /// <summary>
        /// 픽처 박스 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            this.endPoint = 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.selectingArea = false;

            this.pictureBox.Cursor = Cursors.Default;

            Rectangle rectangle = GetRectangle(this.startPoint, this.endPoint);
        }

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

        /// <summary>
        /// 픽처 박스 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            if(this.selectingArea)
            {
                using(Pen pen = new Pen(Color.Yellow, 2))
                {
                    e.Graphics.DrawRectangle(pen, GetRectangle(this.startPoint, this.endPoint));

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

                    e.Graphics.DrawRectangle(pen, GetRectangle(this.startPoint, this.endPoint));
                }
            }
            else
            {
                DrawSmiley(e.Graphics, this.endPoint, 50);
            }
        }

        #endregion

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

        #region 사각형 구하기 - GetRectangle(point1, point2)

        /// <summary>
        /// 사각형 구하기
        /// </summary>
        /// <param name="point1">포인트 1</param>
        /// <param name="point2">포인트 2</param>
        /// <returns>사각형</returns>
        private Rectangle GetRectangle(Point point1, Point point2)
        {
            int x = Math.Min(point1.X, point2.X);
            int y = Math.Min(point1.Y, point2.Y);

            int width  = Math.Abs(point1.X - point2.X);
            int height = Math.Abs(point1.Y - point2.Y);

            return new Rectangle(x, y, width, height);
        }

        #endregion
        #region 스마일리 그리기 - DrawSmiley(graphics, centerPoint, radius)

        /// <summary>
        /// 스마일리 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="centerPoint">중심 포인트</param>
        /// <param name="radius">반경</param>
        private void DrawSmiley(Graphics graphics, PointF centerPoint, float radius)
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;

            using(Pen thickPen = new Pen(Color.Red, 2))
            {
                RectangleF rectangle = new RectangleF
                (
                    centerPoint.X - radius,
                    centerPoint.Y - radius,
                    2 * radius,
                    2 * radius
                );

                graphics.FillEllipse(Brushes.Yellow, rectangle);

                graphics.DrawEllipse(thickPen, rectangle);

                rectangle = new RectangleF
                (
                    centerPoint.X - 0.6f * radius,
                    centerPoint.Y - 0.6f * radius,
                    0.4f * radius,
                    0.6f * radius
                );

                graphics.FillEllipse(Brushes.White, rectangle);

                graphics.DrawEllipse(Pens.Black, rectangle);

                rectangle = new RectangleF
                (
                    centerPoint.X - 0.4f * radius,
                    centerPoint.Y - 0.5f * radius,
                    0.2f * radius,
                    0.4f * radius
                );

                graphics.FillEllipse(Brushes.Black, rectangle);

                rectangle = new RectangleF
                (
                    centerPoint.X + 0.2f * radius,
                    centerPoint.Y - 0.6f * radius,
                    0.4f * radius,
                    0.6f * radius
                );

                graphics.FillEllipse(Brushes.White, rectangle);

                graphics.DrawEllipse(Pens.Black, rectangle);

                rectangle = new RectangleF
                (
                    centerPoint.X + 0.4f * radius,
                    centerPoint.Y - 0.5f * radius,
                    0.2f * radius,
                    0.4f * radius
                );

                graphics.FillEllipse(Brushes.Black, rectangle);

                rectangle = new RectangleF
                (
                    centerPoint.X - 0.15f * radius,
                    centerPoint.Y - 0.15f * radius,
                    0.3f * radius,
                    0.5f * radius
                );

                graphics.FillEllipse(Brushes.LightBlue, rectangle);

                graphics.DrawEllipse(Pens.Blue, rectangle);

                float smileRadius = radius * 0.7f;

                rectangle = new RectangleF
                (
                    centerPoint.X - smileRadius,
                    centerPoint.Y - smileRadius,
                    2 * smileRadius,
                    2 * smileRadius
                );

                thickPen.Color = Color.Green;

                thickPen.Width = 3;

                graphics.DrawArc(thickPen, rectangle, 20, 140);
            }
        }

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

댓글을 달아 주세요