Using GDI + about the problem of the form

xiaoxiao2021-03-31  218

Use GDI to draw forms (FORM) (it is the operation of painting, using Graphics), the code is as follows:

Private void Button1_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 above circle is gone, that is, after the minimization, the original circle is not, because the things painted with Graphics are only temporary, so it is impossible to have been in the form, how to solve this What about the problem?

The three methods are given below:

The first: use thread, schedule to perform the drawing program.

The second: Write the above-mentioned drawing program in Pain, will find that the image will not be refreshed, but there is a problem, that is, when it is When you change the location of the image, the image will not change. Only after refreshing will see change, refresh is also very simple, use refresh (), the following is the complete code, where x, y is global variable, initial value Both 0;

INT x = 0, y = 0;

Private void Button1_Click (Object Sender, System.Eventargs E)

{

X = 10;

Y = 10;

THIS.REFRESH ();

}

Private void form1_paint (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 to override the ONPAINT method, the principle, and the form of the Paint event, if you put this.refresh () in form1_paint, you will see that the form is refreshed in stopping, and The first one is the same as Thread, and it is not the same, and the truth is the same. The following is given the code of Override:

Private void Button1_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);

}

转载请注明原文地址:https://www.9cbs.com/read-130825.html

New Post(0)