728x90
반응형
728x170
▶ MainForm.cs
using System;
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<Point> startPointList = new List<Point>();
/// <summary>
/// 종료 포인트 리스트
/// </summary>
private List<Point> endPointList = new List<Point>();
/// <summary>
/// 그리기 여부
/// </summary>
private bool isDrawing = false;
/// <summary>
/// 신규 시작 포인트
/// </summary>
private Point newStartPoint;
/// <summary>
/// 신규 종료 포인트
/// </summary>
private Point newEndPoint;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.useCutCheckBox.CheckedChanged += useCutCheckBox_CheckedChanged;
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 잘라내기 사용 체크 박스 체크 변경시 처리하기 - useCutCheckBox_CheckedChanged(sender, e)
/// <summary>
/// 잘라내기 사용 체크 박스 체크 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void useCutCheckBox_CheckedChanged(object sender, EventArgs e)
{
this.pictureBox.Refresh();
}
#endregion
#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.isDrawing = true;
this.newStartPoint = e.Location;
this.newEndPoint = e.Location;
}
#endregion
#region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e)
/// <summary>
/// 픽처 박스 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if(!this.isDrawing)
{
return;
}
this.newEndPoint = 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.isDrawing = false;
if(this.newStartPoint == this.newEndPoint)
{
return;
}
this.startPointList.Add(this.newStartPoint);
this.endPointList.Add(this.newEndPoint);
this.pictureBox.Refresh();
}
#endregion
#region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e)
/// <summary>
/// 픽처 박스 페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen backgroundPen = null;
if(this.useCutCheckBox.Checked)
{
backgroundPen = new Pen(pictureBox.BackColor, 8);
}
using(Pen foregroundPen = new Pen(Color.Blue, 4))
{
for(int i = 0; i < this.startPointList.Count; i++)
{
DrawLine
(
e.Graphics,
backgroundPen,
foregroundPen,
this.startPointList[i],
this.endPointList[i]
);
}
if(this.isDrawing)
{
foregroundPen.Color = Color.Red;
DrawLine
(
e.Graphics,
backgroundPen,
foregroundPen,
this.newStartPoint,
this.newEndPoint
);
}
}
if(backgroundPen != null)
{
backgroundPen.Dispose();
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 선 그리기 - DrawLine(graphics, backgroundPen, foregroundPen, startPoint, endPoint)
/// <summary>
/// 선 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="backgroundPen">배경 펜</param>
/// <param name="foregroundPen">전경 펜</param>
/// <param name="startPoint">시작 포인트</param>
/// <param name="endPoint">종료 포인트</param>
private void DrawLine(Graphics graphics, Pen backgroundPen, Pen foregroundPen, Point startPoint, Point endPoint)
{
if(backgroundPen != null)
{
graphics.DrawLine(backgroundPen, startPoint, endPoint);
}
graphics.DrawLine(foregroundPen, startPoint, endPoint);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] Graphics 클래스 : DrawRectangle 메소드를 사용해 사각형 그리기 (0) | 2020.08.07 |
---|---|
[C#/WINFORM] Graphics 클래스 : FillEllipse 메소드를 사용해 포인트 그리기 (0) | 2020.08.07 |
[C#/WINFORM] 벡터 각도 구하기 (0) | 2020.08.07 |
[C#/WINFORM] 확대/축소된 이미지를 특정 크기로 자르기 (0) | 2020.08.07 |
[C#/WINFORM] Graphics 클래스 : FillPolygon 메소드 사용하기 (0) | 2020.08.07 |
[C#/WINFORM] 미로 길 찾기 (애니메이션) (0) | 2020.08.05 |
[C#/WINFORM] 미로 길 찾기 (0) | 2020.08.05 |
[C#/WINFORM] ImageAttributes 클래스 : 이미지 수정하기 (0) | 2020.08.04 |
[C#/WINFORM] 라운드 다각형 그리기 (0) | 2020.08.03 |
[C#/WINFORM] 기준선을 사용해 이미지 회전하기 (0) | 2020.08.03 |
댓글을 달아 주세요