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

TestProject.zip
다운로드

▶ MouseHelper.cs

using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 마우스 헬퍼
    /// </summary>
    public class MouseHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 선 그리기 여부
        /// </summary>
        public static bool DrawLine = true;

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 시작점
        /// </summary>
        private Point startPoint = new Point();

        /// <summary>
        /// 종료점
        /// </summary>
        private Point endPoint = new Point();

        /// <summary>
        /// 사각형
        /// </summary>
        private Rectangle rectangle = new Rectangle();

        #endregion

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

        #region 마우스 DOWN 핸들러 - MouseDownHandler(sender, e)

        /// <summary>
        /// 마우스 DOWN 핸들러
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        public void MouseDownHandler(object sender, MouseEventArgs e)
        {
            rectangle.X      = e.X;
            rectangle.Y      = e.Y;
            rectangle.Width  = 0;
            rectangle.Height = 0;

            startPoint.X     = e.X;
            startPoint.Y     = e.Y;
            endPoint         = startPoint;
        }

        #endregion
        #region 마우스 이동시 핸들러 - MouseMoveHandler(sender, e)

        /// <summary>
        /// 마우스 이동시 핸들러
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        public void MouseMoveHandler(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                Panel drawPanel = sender as Panel;

                if(DrawLine)
                {
                    ControlPaint.DrawReversibleLine(drawPanel.PointToScreen(startPoint), drawPanel.PointToScreen(endPoint), Color.Black);

                    endPoint = new Point(e.X, e.Y);

                    ControlPaint.DrawReversibleLine(drawPanel.PointToScreen(startPoint), drawPanel.PointToScreen(endPoint), Color.Black);
                }
                else
                {
                    ControlPaint.DrawReversibleFrame(drawPanel.RectangleToScreen(rectangle), Color.Black, FrameStyle.Dashed);

                    rectangle.Width  = e.X - rectangle.X;
                    rectangle.Height = e.Y - rectangle.Y;

                    ControlPaint.DrawReversibleFrame(drawPanel.RectangleToScreen(rectangle), Color.Black, FrameStyle.Dashed);
                }
            }
        }

        #endregion
        #region 마우스 UP 핸들러 - MouseUpHandler(sender, e)

        /// <summary>
        /// 마우스 UP 핸들러
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        public void MouseUpHandler(object sender, MouseEventArgs e)
        {
            Panel drawPanel = sender as Panel;

            Graphics graphics = drawPanel.CreateGraphics();

            Pen pen = new Pen(Color.Blue, 2);

            if(DrawLine)
            {
                ControlPaint.DrawReversibleLine(drawPanel.PointToScreen(startPoint), drawPanel.PointToScreen(endPoint), Color.Black);

                graphics.DrawLine(pen, startPoint, endPoint);
            }
            else
            {
                ControlPaint.DrawReversibleFrame(drawPanel.RectangleToScreen(rectangle), Color.Black, FrameStyle.Dashed);

                graphics.DrawRectangle(pen, rectangle);
            }

            graphics.Dispose();
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System;
using System.Windows.Forms;

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

        #region 생성자 - MainForm()

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

            MaximizeBox = false;
        }

        #endregion

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

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            MouseHelper mouseHelper = new MouseHelper();

            this.drawPanel.MouseDown += mouseHelper.MouseDownHandler;
            this.drawPanel.MouseMove += mouseHelper.MouseMoveHandler;
            this.drawPanel.MouseUp   += mouseHelper.MouseUpHandler;
        }

        #endregion
        #region 라인 그리기 라디오 버튼 체크 변경시 처리하기 - drawLineRadioButton_CheckedChanged(sender, e)

        /// <summary>
        /// 라인 그리기 라디오 버튼 체크 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void drawLineRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if(this.drawLineRadioButton.Checked == true)
            {
                MouseHelper.DrawLine = true;
            }
        }

        #endregion
        #region 사각형 그리기 라디오 버튼 체크 변경시 처리하기 - drawRectangleRadioButton_CheckedChanged(sender, e)

        /// <summary>
        /// 사각형 그리기 라디오 버튼 체크 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void drawRectangleRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if(this.drawRectangleRadioButton.Checked == true)
            {
                MouseHelper.DrawLine = false;
            }
        }

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

댓글을 달아 주세요