The Invalidate method informs Windows that the entire surface of the form should be repainted. The most important thing is that Invalidate does not enforce a painting operation immediately. Windows simply stores the request and will respond to it only
after the current procedure has been completely executed and as soon as there are no other events pending in the system. Windows deliberately delays the painting operation because it is one of the most time-consuming operations. At times, with this delay,
it is possible to paint the form only after multiple changes have taken place, avoiding multiple consecutive calls to the (slow) paint method.
• The Update method asks Windows to update the contents of the form, repainting it
immediately. However, remember that this operation will take place only if there is an invalid area. This happens if the Invalidate method has just been called or as the result
of an operation by the user. If there is no invalid area, a call to Update has no effect at all. For this reason, it is common to see a call to Update just after a call to Invalidate.
This is exactly what is done by the two Delphi methods, Repaint and Refresh.
• The Repaint method calls Invalidate and Update in sequence. As a result, it activates the OnPaint event immediately. There is a slightly different version of this method called Refresh. For a form the effect is the same; for components it might be slightly different.
When you need to ask the form for a repaint operation, you should generally call Invalidate,
following the standard Windows approach. This is particularly important when you need to
request this operation frequently, because if Windows takes too much time to update the
screen, the requests for repainting can be accumulated into a simple repaint action. The
wm_Paint message in Windows is a sort of low-priority message. To be more precise, if a
request for repainting is pending but other messages are waiting, the other messages are handled before the system actually performs the paint action.
On the other hand, if you call Repaint several times, the screen must be repainted each time before Windows can process other messages, and because paint operations are computationally intensive, this can actually make your application less responsive. There are times,
however, when you want the application to repaint a surface as quickly as possible. In these less-frequent cases, calling Repaint is the way to go.
Another important consideration is that during a paint operation Windows redraws only the
so-called update region, to speed up the operation. For this reason if you invalidate only a
portion of a window, only that area will be repainted. To accomplish this you can use the
InvalidateRect and InvalidateRegion functions. Actually, this feature is a double-edged sword. It is a very powerful technique, which can improve speed and reduce the flickering caused by frequent repaint operations. On the other hand, it can also produce incorrect output.
A typical problem is when only some of the areas affected by the user operations are actually modified, while others remain as they were even if the system executes the source code that is supposed to update them. In fact, if a painting operation falls outside the update region, the system ignores it, as if it were ou