【发布时间】:2016-07-12 22:49:36
【问题描述】:
为什么画完圆圈后颜色会变? ,其实我画的是圆圈,但我的问题是每次双击后,下一个圆圈的颜色都会从蓝色变为背景色。
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();
}
}
【问题讨论】:
-
在using 语句中在循环外创建笔。例如
using (var pen = new Pen(Color.Tomato, 2){ /* Rest of Code*/ }。using语句以正确的方式调用对象上的Dispose方法,并且(当您如前所示使用它时)它还会导致对象本身在调用Dispose时立即超出范围。在using块内,对象是只读的,不能修改或重新分配。
标签: c# winforms picturebox