Search code examples
c#winformsflickerdoublebuffered

WinForms flickers when loading on Windows 8.1


I have been experiencing some flickering effects when my Windows Forms are initially loaded on Windows 8.1.

At first I tried some solutions involving enabling DoubleBuffered, but that did not seem to fix the problem. I later came across the solution below that many people said fixed all their problems. However, when I tried the fix on my Windows 8.1 computer, it now flickers with a black box.

To further research this, I tried the example code from the MSDN link below. However this also shows a black box when the form is loading. I have tried messing around with the Windows 8.1 visual settings in 'Advanced System Settings'->'Advanced'->'Performance Settings'->'Visual Effects' to see if Aero or similar transparency features had any effect on this, but I still get a flashing black box.

None of the comments in the various threads for this 'fix' seem to be recent. I was wondering if this 'fix' is supposed to apply to Windows 8/8.1 and if there is anything else I can try to get the form and its controls to all appear at once without any flicker.

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

https://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting?forum=winforms

How to fix the flickering in User controls


Solution

  • I was able to get my form and all its controls to display all at once using the code below:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Shown += Form1_Shown;
            this.Opacity = 0;
            SampleExpensiveCreateControlOperation();
        }
    
        private void SampleExpensiveCreateControlOperation()
        {
            for (int ix = 0; ix < 30; ++ix)
            {
                for (int iy = 0; iy < 30; ++iy)
                {
                    Button btn = new Button();
                    btn.Location = new Point(ix * 10, iy * 10);
                    btn.BackColor = Color.Red;
                    this.Controls.Add(btn);
                }
            }
        }
    
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Refresh();
            this.Opacity = 1;
        }
    
    }