代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CGTest001
{
public partial class Form1 : Form
{
public Form1()
{
//SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
InitializeComponent();
}
public List<Point> points = new List<Point>();
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Black);
Font f = new Font("宋体", 10, FontStyle.Bold);
foreach (Point p in points)
{
g.DrawEllipse(pen, p.X, p.Y,2, 2);
string str = $"[x:{p.X},y:{p.Y}]";
g.DrawString(str, f, pen.Brush, p.X + 5, p.Y + 5);
}
//g.DrawLine(pen, new Point(1, 1), new Point(100, 100));
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
points.Add (e.Location);
Console.WriteLine($"point;{e.Location}");
this.Invalidate(); //通知重绘
}
}
}
结果演示: