Hello!
I have an application that paint graphics in VB.NET.
Each graphic is painted after a long calculation process.
My problem is when another windows pass over my graphics
or I resize the window all graphs disapear.
How can I maintain the graphics?
I use to draw the graphics lines, boxes of the namespace
System.Drawing
Thanks for the answer
Actual drawing should take place in the OnPaint event handler of the
object you're painting onto (eg. a Form). When you require complex
drawing, as you seem to indicate, it's best to pre-process as much as
possible somewhere else, so only a very limited amount of calculation
is performed in the OnPaint. A normal place to do this is in the same
place you calculate the resizing of the Form (control, or whatever).
Having said that, you can find the part of the Form that has been
uncovered by another window by looking at the invalidated rectangle.
You can just redraw the contents of that rectangle to preserve the
output. However, sometimes this itself is so complex that the amount
of time it takes to workout what the contents of the invalidated
rectangle should be, that it would actually be quicker to redraw the
whole page.
So, to summarise (and this is for all Windows applications, not just
VB.NET):
1) Perform as much of the calculation as possible in Form_Resize. This
can include complex mathematical computation, scale factors etc.
2) Fonts etc. (and their heights etc.) can be loaded or discovered in
Form_Load, although it is sometimes useful to put this into
Form_Resize instead.
eg. When a font or some text changes, it might be necessary to
calculate the size of the rectangle it takes to draw - a natural place
for this is in a separate function (called from Form_Resize and in the
property handler of the Font in question).
3) Perform all drawing in the Form_Paint method. This will be all the
Graphics.Draw* methods etc. Ideally, you should concentrate on the
implementation here to make it operate as fast as possible. Also, you
should try to avoid things like painting the same point more than
once. On virtually all occasions, you can abandon the handling of clip
rectangles (ie. be lazy) to no noticeable decrease in performance.
4) The code for (1) and (2) can be called by yourself at any time. The
Form_Resize method is called by the framework before the first Paint
event is fired.
Rgds,