调整窗体大小时,在WinForm UI上闪烁

时间:2018-09-24 07:43:12

标签: c# winforms resize

当我调整Winform UI(具有许多子控件)的大小时,会发生闪烁。 我使用了下面的代码,该代码不适用于Resize。

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style &= ~0x02000000;// Turn off WS_CLIPCHILDREN
        return cp;
    }
}

2 个答案:

答案 0 :(得分:0)

创建一个面板,该面板将成为表单表单中所有子级的根。 OnResizeBegin() 方法,调用 Control SuspendLayout();在表单中。 OnResizeEnd(),调用控件 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;
}

希望这会有所帮助。

答案 1 :(得分:0)

我认为您应该更改ExStyle而不是Style以获得双重缓冲效果
另外,您应该使用|=代替&=

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

如果您的表单上仍有某些部分持续闪烁,那么this post可能会有所帮助