为什么在绘制圆圈后颜色发生变化?实际上,我画了圆圈,但是我的问题是,每次双击后,下一个圆圈的颜色会从蓝色变为背景颜色。为什么画圆后他们的颜色会改变?
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(pic_Paint);
}
public Point positionCursor { get; set; }
private List<Point> points = new List<Point>();
public int circleNumber { get; set; }
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
positionCursor = this.PointToClient(new Point(Cursor.Position.X - 25, Cursor.Position.Y - 25));
points.Add(positionCursor);
pictureBox1.Invalidate();
}
private void pic_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
foreach (Point pt in points)
{
Pen p = new Pen(Color.Tomato, 2);
g.FillEllipse(Brushes.Blue, positionCursor.X, positionCursor.Y, 20, 20);
g.DrawEllipse(p, pt.X, pt.Y, 20, 20);
p.Dispose();
}
}
创建循环外的笔,在[使用](https://msdn.microsoft.com/en-us//library/yh598w02.aspx )声明。例如'using(var pen = new Pen(Color.Tomato,2){/ * Rest of Code * /}'。'using'语句以正确的方式调用对象上的Dispose方法,如前所述,它也会导致对象本身在调用Dispose后立即超出范围,在'using'块中,对象是只读的,不能被修改或重新分配。 –