Flickering in a Windows Forms app

后端 未结 5 1439
无人及你
无人及你 2020-12-03 05:06

I have an app that has a ton of controls on it. And it has a massive amount of flicker, particularly on startup.

I applied this fix to it.

    prote         


        
5条回答
  •  时光取名叫无心
    2020-12-03 05:49

    I know this question is a little old, but better late than never. I used your original example you linked to come up with one that toggles it on when resizing, then toggles it back off to draw everything else perfectly. Hopefully it helps others searching for a solution to this problem. As the OP knows, DoubleBuffering alone properties don't solve flickering issues.

    Here's a work-around to stop flickering when a user resizes a form, but without messing up the drawing of controls such as DataGridView, NumericUpDown, etc. Provided your form name is "Form1":

    int intOriginalExStyle = -1;
    bool bEnableAntiFlicker = true;
    
    public Form1()
    {
        ToggleAntiFlicker(false);
        InitializeComponent();
        this.ResizeBegin += new EventHandler(Form1_ResizeBegin);
        this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
    }
    
    protected override CreateParams CreateParams
    {
        get
        {
            if (intOriginalExStyle == -1)
            {
                intOriginalExStyle = base.CreateParams.ExStyle;
            }
            CreateParams cp = base.CreateParams;
    
            if (bEnableAntiFlicker)
            {
                cp.ExStyle |= 0x02000000; //WS_EX_COMPOSITED
            }
            else
            {
                cp.ExStyle = intOriginalExStyle;
            }
    
            return cp;
        }
    } 
    
    private void Form1_ResizeBegin(object sender, EventArgs e)
    {
        ToggleAntiFlicker(true);
    }
    
    private void Form1_ResizeEnd(object sender, EventArgs e)
    {
        ToggleAntiFlicker(false);
    }
    
    private void ToggleAntiFlicker(bool Enable)
    {
        bEnableAntiFlicker = Enable;
        //hacky, but works
        this.MaximizeBox = true;
    }
    

提交回复
热议问题