C#
(C#) 마우스로 그림 그리기
코딩ABC
2023. 6. 10. 08:49
반응형
마우스를 움직이면 그림을 그리는 C# 코드입니다.
1. 프로젝트를 생성합니다.
- Windows Forms 앱(.NET Framework)
2. 폼에 Panel 1개를 배치합니다.
3. 코드를 작성합니다.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private bool isDrawing;
private Point previousPoint;
public Form1()
{
InitializeComponent();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
using (Graphics graphics = panel1.CreateGraphics())
{
Pen pen = new Pen(Color.Black, 1);
graphics.DrawLine(pen, previousPoint, e.Location);
}
previousPoint = e.Location;
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
previousPoint = e.Location;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
}
}
(실행 결과)
반응형