■ PictureBox 클래스를 사용해 사용자 선을 그리는 방법을 보여준다.
▶ Segment.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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 } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
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 } } |