Search code examples
c#winformsdouble-buffering

WinForms Flickering after Maximizing - Double Buffering


I have a "Master" form with navigation buttons on the left side, and a panel on the right that I use as a holder for child forms. It's pretty simple but the code for this is below.

public void showForm(Form form)
{
    // Disposed Prior Form & Load New
    form.Dock = DockStyle.Fill;
    form.TopLevel = false;
    pnlMain.Controls.Clear();
    pnlMain.Controls.Add(form);
    form.Show();
}

The Main issue I'm having is that everything works fine, I get virtually no flicker using the code below when navigating from form to form, but as soon as I maximize my "Master" form (which in turn enlarges the "Child" form)... even if I un-maximize and go back to the original size it starts flickering like crazy when I navigate to a new form. It's as if after maximizing it completely disregards the code put in place to fix the flickering in the first place. I should also mention that the "Child" Form has it's own form draw event which is why I have these things in place to reduce the flickering, it's normally not an issue until maximized.

public void drawBackgroundChild(PaintEventArgs e, Form form)
{
   // prevents error on resize
   if (form.ClientRectangle.Width == 0 || form.ClientRectangle.Height == 0)
       return;

   using (LinearGradientBrush brush = new LinearGradientBrush(form.ClientRectangle,
                                                                     UserSettings.secondaryColor1,
                                                                      UserSettings.secondaryColor2,
                                                                      90F))
            {
                e.Graphics.FillRectangle(brush, form.ClientRectangle);
            }
}



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

I have tried the code above as well as the code below multiple times on the "Master" and "Child" Forms. Tested it out a bunch of different ways but no luck. Does anyone know if resizing the Windows Form disables Double Buffering or something to this extent?

 DoubleBuffered = true;
 this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);

Solution

  • Had to remove any sort of double buffering / createparams etc. from the master form, and put it exclusively into the child forms....

    protected override CreateParams CreateParams
    {
        get
           {
             var cp = base.CreateParams;
             cp.ExStyle |= 0x02000000;    // Turn on WS_EX_COMPOSITED
             return cp;
           }
    }
    
    
    frmChild()
    {
        ResizeRedraw = true;
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer |
                      ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
    }