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

Community
  • 1
  • 1
tjsmith
  • 729
  • 7
  • 21
  • What are you doing when the form loads? Some code whould be nice – γηράσκω δ' αεί πολλά διδασκόμε Mar 20 '15 at 04:10
  • The sample code in the msdn post can replicate the problem(I think it creates a bunch of buttons). Basically whenever there are a lot of controls in a form they can cause the form to flicker when it loads, my code is less drastic than the msdn code since I have less controls on my form but the same problem occurs for me. I think the msdn post may have fixed the problem for older operating systems, but I see no mention of anyone testing it on windows 8/8.1 – tjsmith Mar 20 '15 at 05:30
  • _I have less controls on my form_ - Meaning how many?? Created where? before or after the designer's `this.ResumeLayout(false); this.PerformLayout();`? – TaW Mar 20 '15 at 08:09
  • Just 4 or 5 controls, but the form has some 3rd party controls that have theming options which adjust the back color of the form and its controls, which might make it a little heavy. I apply the different theming options at run time so I see a flicker as the form changes from, for example, grey to dark grey background or if I put the code above, my form appears as a black box initially. If I could get the sample code in the msdn link to work without flickering or a black box on win8.1, then I'm sure that the same fix would work with my code. – tjsmith Mar 20 '15 at 10:20

1 Answers1

2

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;
    }

}
tjsmith
  • 729
  • 7
  • 21