Use GDI + to draw forms (such as circles and Graphics). The Code is as follows:
Private void button#click (object sender, System. EventArgs e)
{
System. Drawing. Graphics g = this. CreateGraphics ();
G. FillEllipse (Brushes. red, 100,100, 50, 50 );
}
When the form is refreshed, the circle above will disappear. That is to say, after the minimum, the circle will be restored, because the Graphics painting is only temporary, so it cannot be on the form all the time. How can this problem be solved?
Here are three solutions:
First: Use thread to regularly execute the drawing program.
Second: use the form Paint event to write the above painting program in the Paint, you will find that the image will not be lost because of refreshing, but there is another problem, this event is to be driven. That is to say, when you change the position of the image, the image will not change. Only after refreshing will you see the changes. Refresh is also very simple. Refresh () the following is the complete code. x and y are global variables and the initial values are all 0;
Int x = 0, y = 0;
Private void button#click (object sender, System. EventArgs e)
{
X + = 10;
Y + = 10;
This. Refresh ();
}
Private void formatepaint (object sender, System. Windows. Forms. PaintEventArgs e)
{
System. Drawing. Graphics g = this. CreateGraphics ();
G. FillEllipse (Brushes. Red, x, y, 50, 50 );
}
The third method is the override OnPaint method. The principle is the same as that of the form painting event. put Refresh () in Form1_Paint, you will see that the form is constantly refreshed, and the principle of the first thread is the same. The principle is the same. The override code is given below:
Private void button#click (object sender, System. EventArgs e)
{
X + = 10;
Y + = 10;
This. Refresh ();
}
Protected override void OnPaint (PaintEventArgs e)
{
System. Drawing. Graphics g = this. CreateGraphics ();
G. FillEllipse (Brushes. Red, x, y, 50, 50 );
}