views:

299

answers:

1

I am working on a windows form project and having some problem with UserControl Double Buffering. I created a usercontrol and has a background image, then on top of it I have few radio buttons and labels. Radio buttons and labels are all having transparent background as color. However, when I show and hide the User control, I can see the flickering on those labels and radio buttons that has transparent background.

And I tried

Me.SetStyle(ControlStyles.DoubleBuffer _
Or ControlStyles.AllPaintingInWmPaint _
Or ControlStyles.UserPaint _
Or ControlStyles.SupportsTransparentBackColor, _
True)

After initializeComponent() to enable double buffer on this user control, but it doesn’t seem to work.

A: 

This is not a source of flicker that double-buffering can solve. When the UC repaints itself, it draws the background image, leaving holes where the controls go. The controls then each get to paint themselves, filling in the hole by first asking the UC to draw itself again to produce the background pixels, then draw themselves on top. The temporary hole is what you see as flicker.

You can make it less objectionable by allowing the UC to draw itself in the client area of the controls so the background is already set correctly. Paste this code in the UserControl class:

protected override CreateParams CreateParams {
  get {
    var parms = base.CreateParams;
    parms.Style &= ~0x02000000;  // Turn off WS_CLIPCHILDREN
    return parms;
  }
}

This doesn't make the painting any faster and may have side-effects. If that is still a problem then you need to make the BackgroundImage draw faster. Prescale it to the client size of the user control so it can be drawn without rescaling. Use the PixelFormat.Format32bppArgb format for the bitmap, it is about 10x faster than any other one on most video adapters.

Hans Passant
this solved my prolem, thank you.