728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 이미지 나선 그리기 (0) | 2020.07.10 |
---|---|
[C#/WINFORM] Graphics 클래스 : DrawCurve 메소드를 사용해 곡선 그리기 (0) | 2020.07.09 |
[C#/WINFORM] 사인(Sine)과 코사인(Cosine)을 사용해 원과 타원 그리기 (0) | 2020.07.09 |
[C#/WINFORM] Bitmap 클래스 : 투명한 구멍을 갖는 비트맵 만들기 (0) | 2020.07.05 |
[C#/WINFORM] PictureBox 클래스 : 사용자 그리기 및 확대/축소하기 (0) | 2020.07.05 |
[C#/WINFORM] CommonOpenFileDialog 클래스 : 표준 윈도우즈 대화 상자를 사용해 폴더 선택하기 (0) | 2020.07.05 |
[C#/WINFORM] ListView 클래스 : 항목 인덱스 구하기 (0) | 2020.06.08 |
[C#/WINFORM] ListView 클래스 : 선택 항목 리스트 구하기 (0) | 2020.06.08 |
[C#/WINFORM] Control 클래스 : 컨트롤 비트맵 구하기 (0) | 2020.06.05 |
[C#/WINFORM] Bitmap 클래스 : 회색조 비트맵 구하기 (0) | 2020.06.04 |
댓글을 달아 주세요