visual c# form update results in flickering

前端 未结 10 1041
眼角桃花
眼角桃花 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:35

    i had the same problem with OpenGLES, which is how i found this thread. of course i realize u are not using ogl, but maybe this helps u anyway ;)

    protected override void OnPaintBackground(PaintEventArgs e) { }

    0 讨论(0)
  • 2020-12-17 21:38

    You didn't research this well. There is a DoubleBuffered property in every Form. Try setting that to true. If you havn't overloaded anything on the form painting, then everything should work.

    0 讨论(0)
  • 2020-12-17 21:42

    You could try to call this.SuspendLayout(); before you start your update and this.ResumeLayout(false); when you have finished setting all the values in this way it should prevent the form from writing values one at a time.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-17 21:44

    It could also be caused by your coding, not the absence of doublebuffering. I came here just now with a similar problem but realised it's because:

    1. I set a frame to invisible when an item is not selected.
    2. In between user selections, the index is cleared by the ListView control.
    3. I'm bound to the SelectedIndexChanged event

    In other words:

    • User clicks item 1
      ~ SelectedIndexChanged(1)
    • User clicks item 2
      ~ SelectedIndexChanged(-1) <---- This causes the flicker
      ~ SelectedIndexChanged(2)

    So what's the solution? How to avoid thousands of needless ListView.SelectedIndexChanged events?

    0 讨论(0)
  • 2020-12-17 21:45

    This worked for me.

    http://www.syncfusion.com/faq/windowsforms/search/558.aspx

    Basically it involves deriving from the desired control and setting the following styles.

    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
    SetStyle(ControlStyles.DoubleBuffer, true); 
    
    0 讨论(0)
提交回复
热议问题