C#中如何实现清除已画的图形
先贴代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(pbMain_MouseWheel);
this.MouseDown += new MouseEventHandler(pbMain_MouseDown);
this.MouseUp += new MouseEventHandler(pbMain_MouseUp);
blackPen = new Pen(Color.Black, 1);
}
private int R = 20;
private Pen blackPen;
private Point centerPoint = new Point();
private bool bStart = false;
private void pbMain_MouseDown(object sender, MouseEventArgs e)
{
centerPoint.X = e.X;
centerPoint.Y = e.Y;
bStart = true;
}
private void pbMain_MouseUp(object sender, MouseEventArgs e)
{
bStart = false;
}
private void pbMain_MouseWheel(object sender, MouseEventArgs e)
{
if (bStart)
{
int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
R += numberOfTextLinesToMove;
if(R < 0) R = 0;
if(R > 60) R = 60;
Graphics g = this.CreateGraphics();
g.DrawEllipse(blackPen, pointTopLeft.X - R, pointTopLeft.Y - R, 2 * R, 2 * R);
}
}
}
//////////////////////////////////////////////////////////////////////
简化在程序如上:按下鼠标右键(不松开),此时,滚动鼠标滚轮,将在窗口内绘制多个不同直径在同心圆。
我的问题是:如何才能始终只画一个圆?
考虑到在复杂背景下画圆的情况,如背景是一幅图,不能使用Graphics的Clear函数清除背景。