visual c# form update results in flickering

前端 未结 10 1101
眼角桃花
眼角桃花 2020-12-17 21:18

I have a .net app that I\'ve written in c#. On some forms I frequent update the display fields. In some cases every field on the form (textboxes, labels, picturebox, etc) ha

10条回答
  •  清歌不尽
    2020-12-17 21:44

    I know this question is old, but may be it will others searching for it in the future.

    DoubleBuffering doesn't always work well. To force the form to never flicker at all (but sometimes causes drawing issues):

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

    To stop flickering when a user resizes a form, but without messing up the drawing of controls (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;
    }
    

提交回复
热议问题