Window weight painting when using GDI +

Source: Internet
Author: User

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 );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.