views:

932

answers:

3

After creating a new form, I usually perform this ritual:

  1. Change the name into something meaningful;
  2. Type a Caption;
  3. Change the position property (DefaultPosOnly is hardly ever what users expect);
  4. Set ShowHint to true;
  5. Set DoubleBuffered to true;

I've been wondering for a while why the default value is 'False'. To me it just looks low-tech and crappy, and on my new machine I don't notice any difference in performance.

Is doublebuffering problematic on older machines, VNC, Remote Desktop or in Virtual Machines maybe?

Do you leave it on or off? Any recommendations?

+23  A: 

As you probably know, a double buffer normally involves creating an off-screen memory buffer the same size as the visual component. Writing/drawing is performed on this buffer and when complete, the entire buffer is "swapped" so that it is now painted on the visual component.

(Note: "swapping" may consist of simply changing the address a pointer points to, or may actually involve copying a chunk of memory such as using BitBlt, memcpy etc)

Therefore a reasonable amount of memory allocated to support this process for each component it is enabled for. If your application has many windows or and/or components there would be a not insignificant amount of memory allocated. If you do not require smooth visual updates/scrolling, why waste this memory?

Of course there is also an argument that today most computers have plenty of memory to spare, so why worry. However I still don't see this as a reason to default to enabling Double Buffering if you don't need it.

If manually setting DoubleBuffered to true is a pain for you, you could always create your own custom control/component that inherits from the built-in control, and sets DoubleBuffered (and other properties) to your required defaults.

Ash
+1 nice answer. Nitpick: replace "quickly copied" with "swapped" or something else that doesn't give the impression there is an actually buffer copy going on. Usually buffer swaps are simple pointer swaps.
Martinho Fernandes
Good point, I've updated the answer. Of course in Windows Forms.NET there is also a "pretend" double buffering that actually is just a memory copy.
Ash
When `DoubleBuffered` is true, a control responds to paint messages like this: It creates a bitmap, paints to the bitmap, calls `BitBlt` to copy the bitmap onto the window, and then destroys the bitmap. It's not just a simple pointer swap.
Rob Kennedy
@Tob, I've added a note to the answer about pointer swaps and memory copies.
Ash
**@Rob**, sorry about the type in your name!
Ash
+7  A: 

On a modern OS which does desktop compositing double buffering may actually decrease performance. Rendering is performed into an off-screen bitmap anyway, so using double buffering leads to an extra copying for no benefit at all on those systems. So unless the VCL is smart enough to ignore the double buffering in that case (don't know whether it does, would need to check) it may actually be better to not set it unconditionally.

Edit:

I checked, and in both Delphi 2007 and Delphi 2009 the TWinControl.WMPaint method does not use double buffering when DwmCompositionEnabled returns True. Nice.

mghie
+11  A: 

Double buffering is to be avoided when doing Remote Desktop of some sort, since the whole bitmap of the control/form has to be sent over the network to do the BitBlt. see this blog post...

François
+1 Awesome. So obvious the minute you point it out, but it had never occurred to me that this would be the case. That'll cause me to rethink a few of my programs.... thanks for pointing this out!
robsoft
+1 agree with what's mentioned above!