Take the 2-minute tour ×

I added this to my form's constructor code:

this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

But it still shows ugly artifacts when it loads the controls, whenever they change (the form is a calendar, it changes often).

What do I need to do differently?

share|improve this question

1 Answer 1

up vote 32 down vote accepted

This only has an effect on the form itself, not the child controls. If you have a lot of them then the time they need to take turns painting themselves becomes noticeable, it leaves a rectangular hole where the control goes that doesn't get filled up until the child control gets it turn.

What you'd need to combat this is double-buffering the entire form and the controls. That's an option available since Windows XP which made the WS_EX_COMPOSITED style flag available. Paste this into your form to turn it on:

protected override CreateParams CreateParams {
  get {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
    return cp;

It doesn't speed up the painting at all, but the form snaps onto the screen after a delay. Eliminating the visible paint artifacts. Really fixing the delay requires not using controls. Which you'd do by using the OnPaint method to draw the 'controls' and making the OnMouseClick event smart about what 'control' got clicked by the user.

share|improve this answer
Brilliant, thank you! –  Motig Sep 15 '10 at 15:28
WS_EX_COMPOSITED is only working on classic theme under win7/vista, not on aero theme. Anybody knows solution in this case? –  DKhanh Dec 18 '11 at 17:05
@DKhanh - this worked for me on Aero –  simon.d Jun 7 '12 at 22:30
It works like a charm (not a refernce to Windows 8 charms, but I'm hoping it does work on Windows 8, too, as well as XP, if needed). –  B. Clay Shannon Aug 21 '12 at 0:06
This works fine on Win8. –  EricLaw Mar 19 '13 at 22:46

Your Answer


