问题
I am trying to move a drawn graphics item, I can draw them just fine. but when I try to move it, it just draws another item over the original. I have tried Invalidate, and pictureBox1.Invalidate. still no luck just prints both items to the pictureBox. Thanks for any suggestions.
private void button1_Click(object sender, EventArgs e)
{
count++;
button1.Text = "Move";
if (count == 1)
{
car();
}
else if (count == 2)
{
car1();
}
}
public void car()
{
Invalidate();
g = pictureBox1.CreateGraphics();
g.DrawEllipse(pen1, 50, 125, 30, 30);
g.DrawEllipse(pen1, 150, 125, 30, 30);
g.DrawRectangle(pen2, 45, 75, 140, 50);
g.DrawLine(pen2, 65, 75, 80, 35);
g.DrawLine(pen2, 80, 35, 140, 35);
g.DrawLine(pen2, 140, 35, 160, 75);
// Create string to draw.
String drawString = "Price: "+(cost).ToString("C");
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(50, 95);
// Draw string to screen.
g.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
public void car1()
{
Invalidate();
g = pictureBox1.CreateGraphics();
g.DrawEllipse(pen1, 50 + m, 125, 30 , 30 );
g.DrawEllipse(pen1, 150 + m, 125, 30 , 30);
g.DrawRectangle(pen2, 45 + m, 75, 140, 50 );
g.DrawLine(pen2, 65 + m, 75, 80 + m, 35);
g.DrawLine(pen2, 80 + m, 35, 140 + m, 35);
g.DrawLine(pen2, 140 + m, 35, 160 + m, 75);
// Create string to draw.
String drawString = "Price: " + (cost).ToString("C");
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(50 + m, 95);
// Draw string to screen.
g.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
回答1:
Move your code to the Paint event of the picturebox and use the graphics object from the available arguments.
private void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(Color.White);
if (count == 1)
DrawCar1(e.Graphics);
if (count == 2)
DrawCar2(e.Graphics);
}
public void DrawCar1(Graphics g)
{
g.DrawEllipse(pen1, 50, 125, 30, 30);
g.DrawEllipse(pen1, 150, 125, 30, 30);
// etc.
}
public void DrawCar2(Graphics g)
{
// etc.
}
private void button1_Click(object sender, EventArgs e)
{
count++;
button1.Text = "Move";
pictureBox.Invalidate();
}
Your code never cleared the PictureBox, just the form, but it's a mistake to use CreateGraphics()
because that is just a temporary canvas. Any drawing using that method gets wiped out when you minimize the form, for instance.
Your code should probably be refactored into having a "Car" class. Having a Car1 and a Car2 drawing routine isn't very practical. If you want to keep moving it, you don't want to keep adding more Car# routines. Create a single drawing routine for the car and keep the location information in the "Car" class so you know "where" you want the car drawn.
来源:https://stackoverflow.com/questions/9521507/c-sharp-invalidate-troubles