2007. december 13., csütörtök

How to eliminate flickering without using LockWindowUpdate(Handle)


Problem/Question/Abstract:

I have an application in which the user drags TImage descendants around on a background image. When an image is dropped, I have to run through all the current images finding out where they are, and then arrange their z-order appropriately. When I do this, there's significant flickering. I've tried calling LockWindowUpdate(Handle) before the operation, then LockWindowUpdate(0) at the end, but several repaint operations still seem to take place at once. I'd like to be able to repaint the whole form once only, or failing that, limit the repaint to a specific area of the form (so that all my buttons etc, which aren't involved in any of this, don't have to flicker too).

Answer:

Below is a fragment of code implementing reference counted form redraw locking. I use it in my apps where any form is derived from TLwForm (subclass of TForm). It suggests locking not limited to one window as it's the case with LockWindowUpdate. The approach can be applied not to the form as the whole but, via iteration, to all its TWinControl children.

var
  FLockFormUpdatePile: integer;

procedure TLwForm.LockFormUpdate;
begin
  if FLockFormUpdatePile = 0 then
    Perform(WM_SetRedraw, 0, 0);
  inc(FLockFormUpdatePile);
end;

procedure TLwForm.UnlockFormUpdate;
begin
  dec(FLockFormUpdatePile);
  if FLockFormUpdatePile = 0 then
  begin
    Perform(WM_SetRedraw, 1, 0);
    RedrawWindow(Handle, nil, 0, RDW_FRAME + RDW_INVALIDATE +
      RDW_ALLCHILDREN + RDW_NOINTERNALPAINT);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése