2011. február 17., csütörtök

How to prevent a TPaintBox from flickering


Problem/Question/Abstract:

I need a control similar to a gauge, but a bit different, so I thought that I can use a TPaintBox. In the PaintBox.OnPaint method I create a bitmap and at the end of procedure I call PaintBox.Canvas.Draw(0,0,bmp); (the same is used in Delphi's TGauge control). When I need to show a new value I call PaintBox.Refresh. It does work, however it flickers when repainting.

Answer:

First, you should not create a bitmap in the OnPaint event. This event is called every time the paintbox must be updated, not only when it first becomes visible.

To avoid flicker, set the control's style to csOpaque to avoid background painting.

paintbox1.controlstyle := paintbox1.controlstyle + [csopaque];

or don't call Refresh (or repaint, for that matter). Call the procedure that does the actual drawing stuff (for example, PaintBox1.OnPaint(Self)).

Nincsenek megjegyzések:

Megjegyzés küldése