Using the WS_EX_COMPOSITE window style to eliminate flicker on Windows XP

As I continue to work through the various flicker issues on individual controls in the VCL I thought it worth mentioning a bit more “global” way to tackle the “flicker” problem. If you’re running Windows XP there is a window style called WS_EX_COMPOSITED which you’ll find documented on MSDN under CreateWindowEx here. This style bit instructs the OS to apply double buffered painting for you. Here is how you would add this to a Delphi VCL form:

1 type 2 TMyForm = class(TForm) 3 protected 4 procedure CreateParams(var Params: TCreateParams); override; 5 end; 6 7 ... 8 9 procedure TMyForm.CreateParams(var Params: TCreateParams); 10 begin 11 inherited; 12 // This only works on Windows XP and above 13 if CheckWin32Version(5, 1) then 14 Params.ExStyle := Params.ExStyle or WS_EX_COMPOSITED; 15 end; 16

You should be able to use this on any 32-bit version of Delphi as long as your app is running on Windows XP.

The downside is that depending on your application this change could have rather serious performance implications during a window resize particularly with aligned controls. The upside of course is that it’s flicker free! 🙂

I’ve read that there may be some issues related to using this window style with GDI+ so be sure to look for that if it affects you.

There is no comment on the MSDN as to how this works under Windows Vista but I’d guess there may be some differences.

[UPDATE: May, 2008] Related posts:

Quick Tip: FullRepaint and fixing flicker in a Delphi VCL app
Quick Tip #2: Fixing flicker caused by WM_ERASEBKGND in a Delphi VCL app

6 thoughts on “Using the WS_EX_COMPOSITE window style to eliminate flicker on Windows XP

  1. glob,
    Yes, I’ve read that article and I’d hope Windows would resolve it for you if you decided to use WS_EX_COMPOSITE since it seems like that should be possible without any additional coding. You’ll notice the MSDN doesn’t mention the remote desktop situation at all so I guess it’s up to the developer to do the testing.

  2. There seems to be a problem with caption button painting too; if you have a visual style applied the buttons won’t react to to MouseOver.

  3. This appears to be a great workaround for QC37403 (ALT Key press causes controls to disappear under Themes in Vista and XP):
    procedure TCustomCheckBox.CreateParams(var Params: TCreateParams);
    […]
    begin
    inherited CreateParams(Params);
    if CheckWin32Version(5, 1) then
    Params.ExStyle := Params.ExStyle or WS_EX_COMPOSITED;

  4. This seems to work without trouble on Vista 64, though with a TImage, the flicker is gone, but glitches are still visible. I suspect these are simply the result of asynchronous repaint with respect to vertical refresh.

  5. Bill,
    FYI, I’ve seen numerous visual glitches on WinXP 64 that aren’t present in XP Win32 and it wouldn’t surprise me if the same were true for Vista, so it might be worth a quick check.

Comments are closed.