1

When I resize the Winform UI (which has lot of child controls), flickering happens. I used the below code, which is not working for Resize.

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style &= ~0x02000000;// Turn off WS_CLIPCHILDREN
        return cp;
    }
}
Mahesh
  • 823
  • 1
  • 11
  • 29
  • 1
    Have you tried [turning on double-buffering](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-reduce-graphics-flicker-with-double-buffering-for-forms-and-controls) for the parent form? – Matthew Watson Sep 24 '18 at 07:44
  • Yes. I have tried. No luck. – Mahesh Sep 24 '18 at 07:45
  • Do you have any code in the resize event of the form ? If so then show it – GuidoG Sep 24 '18 at 11:26

2 Answers2

0

Create a Panel which will be the root of all children in your Form, and in your Form.OnResizeBegin() method, call Control.SuspendLayout(); in Form.OnResizeEnd(), call Control.ResumeLayout().

class MainForm: Form {
    public MainForm()
    {
        this.Build();
    }

    void Build()
    {
        this.root =  new Panel { Dock = DockStyle.Fill };
        // create all controls and add them to root

        this.Controls.Add( root );
        this.ResizeBegin += (obj, args) => this.OnResizeBegin();
        this.ResizeEnd += (obj, args) => this.OnResizeEnd();
    }

    void OnResizeBegin()
    {
        this.root.SuspendLayout();
    }

    void OnResizeEnd()
    {    
        this.root.ResumeLayout( true );
    }

    Panel root;
}

Hope this helps.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • 2
    "I could not keep them in a panel as i have multiple user controls located different parts of the form." I don't think that's even possible. The **Form** is basically a regular panel with *Fill*, *Top*, *Bottom*, etc. regions. How is that you cannot put all controls hanging from a single **Panel**?. – Baltasarq Sep 24 '18 at 10:01
0

I think you should change the ExStyle not the Style to get the double buffered effect
Also you should use |= in stead of &=

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

If there are still parts on your form that keep flickering then maybe this post can help

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • Is there any code executed when resizing the form ? Also have you checked out the link in my answer ? – GuidoG Sep 24 '18 at 11:54
  • 1
    What do you mean by `with WS_EX_COMPOSITED my form gets event on some button clicks` ? – GuidoG Sep 24 '18 at 11:58
  • With WS_EX_COMPOSITED my form gets flickering on some button clicks. When i changed the code from 'cp.ExStyle |= 0x02000000;' to 'cp.Style &= ~0x02000000;' flcikering on button clicks is reduced visibly. But still i could see flickering on resize with turn off clip children. I have gone through the link. I have a overlapped controls in the form in one place. – Mahesh Sep 24 '18 at 12:01
  • For resizing there is some code but even after commenting and trying few things like suspend and resume layout on resize begin and reisze end did not help – Mahesh Sep 24 '18 at 12:03
  • 1
    weird. clip children is not double buffering but it can indeed help with flickering. But double buffering should resolve any flickering. Is there any code in paint or in the resize event ? – GuidoG Sep 24 '18 at 12:03
  • Maybe there are anchor's setup on some controls that keep resizing containers while resizing the form ? – GuidoG Sep 24 '18 at 12:09
  • Turing on Double buffering did not show any impact i have tested it. My form is having a button bar in which there will be list of controls where each control is having a label and images which are stretched with resizing. Also it has menu bar ,tool bar, and a grid where around 50-100 rows with 6-9 columns content gets updated. Actually on toolstrip we are overriding it with a small Logo placed over it. Flickering is only on logo control background and in button bar area. – Mahesh Sep 24 '18 at 12:10
  • So test it without any override on the toolstrip and without the logo and without the stretching. One of these seems so cause it – GuidoG Sep 24 '18 at 12:34