728x90
반응형
728x170
▶ 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
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 포인트 리스트
/// </summary>
private List<Point> pointList = new List<Point>();
/// <summary>
/// 텐션
/// </summary>
private float tension = 0.5f;
/// <summary>
/// 그리기 여부
/// </summary>
private bool isDrawing = true;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.tensionScrollBar.Scroll += tensionScrollBar_Scroll;
this.canvasPictureBox.Paint += canvasPictureBox_Paint;
this.canvasPictureBox.MouseClick += canvasPictureBox_MouseClick;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 텐션 스크롤바 스크롤시 처리하기 - tensionScrollBar_Scroll(sender, e)
/// <summary>
/// 텐션 스크롤바 스크롤시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void tensionScrollBar_Scroll(object sender, ScrollEventArgs e)
{
this.tension = this.tensionScrollBar.Value / 10f;
this.tensionLabel.Text = tension.ToString();
this.canvasPictureBox.Refresh();
}
#endregion
#region 캔버스 픽처 박스 마우스 클릭시 처리하기 - canvasPictureBox_MouseClick(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseClick(object sender, MouseEventArgs e)
{
if(this.isDrawing)
{
if(e.Button == MouseButtons.Left)
{
this.pointList.Add(e.Location);
}
else
{
this.isDrawing = false;
}
}
else
{
this.isDrawing = true;
this.pointList = new List<Point>();
this.pointList.Add(e.Location);
}
this.canvasPictureBox.Refresh();
}
#endregion
#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.SmoothingMode = SmoothingMode.AntiAlias;
if(this.pointList.Count > 1)
{
using(Pen pen = new Pen(Color.Blue))
{
if(this.isDrawing)
{
pen.DashPattern = new float[] { 5, 5 };
}
e.Graphics.DrawCurve(pen, this.pointList.ToArray(), this.tension);
}
}
if(this.isDrawing && (this.pointList.Count > 0))
{
const int SIDE = 4;
foreach(Point point in this.pointList)
{
Rectangle rectangle = new Rectangle
(
point.X - SIDE,
point.Y - SIDE,
2 * SIDE,
2 * SIDE
);
e.Graphics.FillRectangle(Brushes.White, rectangle);
e.Graphics.DrawRectangle(Pens.Black, rectangle);
}
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] ColorMatrix 클래스 : 이미지 불투명도 조정하기 (0) | 2020.07.13 |
---|---|
[C#/WINFORM] 호(arc) 그리기/이동하기/수정하기 (0) | 2020.07.12 |
[C#/WINFORM] 호(arc) 위의 마우스 위치 여부 구하기 (0) | 2020.07.12 |
[C#/WINFORM] 안티-알리아싱으로 그리는 경우 투명도 사용하기 (0) | 2020.07.11 |
[C#/WINFORM] 이미지 나선 그리기 (0) | 2020.07.10 |
[C#/WINFORM] 사인(Sine)과 코사인(Cosine)을 사용해 원과 타원 그리기 (0) | 2020.07.09 |
[C#/WINFORM] Bitmap 클래스 : 투명한 구멍을 갖는 비트맵 만들기 (0) | 2020.07.05 |
[C#/WINFORM] PictureBox 클래스 : 사용자 그리기 및 확대/축소하기 (0) | 2020.07.05 |
[C#/WINFORM] PictureBox 클래스 : 사용자 선 그리기 (0) | 2020.07.05 |
[C#/WINFORM] CommonOpenFileDialog 클래스 : 표준 윈도우즈 대화 상자를 사용해 폴더 선택하기 (0) | 2020.07.05 |
댓글을 달아 주세요