In Delphi's VCL framework there is a property on TWinControl called DoubleBuffered which can help reduce or eliminate window flicker. To see a simple example of it's effectiveness try the following:
- Create a new VCL application
- Drop a TMemo control
- Set Align on Memo1 to alClient
- Using the Lines property add some text to the memo
- Run the application and resize the form
Notice how the text in the control flickers. Now add an OnCreate event to Form1 and put the following code:
Memo1.DoubleBuffered := True;
Run the application again and resize the form. Notice that the flicker is gone. In a future release we'll publish the DoubleBuffered property to allow you to set it directly from the Object Inspector but until then writing a little code is necessary.
You might be wondering why isn't DoubleBuffered set to true by default? The reason is performance though the average PC's processing power has significantly increased since we added this feature to the VCL. A machine that's capable of running Windows Vista with the Areo Glass UI will easily be able to handle DoubleBuffering.
Finally, let's to the reason I wrote this post which is handling WM_ERASEBKGND. If you've written your own components that handle their own painting you should consider the affect of DoubleBuffering in your message handling code. An example is in the default implementation of TWinControl's WMERASEBKGND method which contains the following:
{ Only erase background if we're not doublebuffering or painting to memory. }
if not FDoubleBuffered or
(TMessage(Message).wParam = TMessage(Message).lParam) then
FillRect(Message.DC, ClientRect, FBrush.Handle);
Consider doing something similar in your code to support DoubleBuffering. I have been working on reducing flicker within the Delphi IDE itself and there were a few WM_ERASEBKGND handlers which were not written with DoubleBuffering in mind (but that's fixed now). We're not quite down to a flick-free IDE but it's getting very close.
[UPDATE Feb 1]: Fix the property name in the first sentence, it's "DoubleBuffered" not "DoubleBuffering"
[UPDATE May, 2008] Related posts:
Quick Tip: FullRepaint and fixing flicker in a Delphi VCL app
Using the WS_EX_COMPOSITE window style to eliminate flicker on Windows XP