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
| public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private struct Line
{
public Point Start;
public Point End;
public Line(Point start, Point end)
{
this.Start = start;
this.End = end;
}
}
private List<Line> lines = new List<Line>();
private Point start, end;
private bool draw = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
draw = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
lines.Add(new Line(start, end));
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (draw)
{
end = e.Location;
this.Invalidate(this.ClientRectangle);
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
foreach (Line line in lines)
g.DrawLine(Pens.Black, line.Start, line.End);
if (draw)
g.DrawLine(Pens.Red , start, end);
} |
Partager