如何避免屏幕闪烁?

时间:2013-05-08 13:10:59

标签: c# winforms flicker

我正在制作一个Windows窗体应用程序,主要是我的屏幕分为3个部分,如

===========================================================================
                    Top panel (it doesn't flicker)
===========================================================================
||         || 'it has a panel & panel contains a table layout,this tabble layout'
||         || 'has a picture box and a label, picture & text of label is'           
||         ||'changed on the click of side bar menu' (PROB: this flickers a lot)
||side bar ||==============================================================
||(doesn't ||'this part also has a panel and panel contains different table'
||flicker) ||'layouts and on the click of a menu, related table layout is shown and'  
||         ||'some of the parts of table layout are created dynamically.'
||         ||           
||         ||                (PROB: this flickers a lot)
||         ||

我搜索了很多,到处找到了这个解决方案,我试过这个

public constructor()
    {
        InitializeComponent();
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.DoubleBuffered = true;
        DoubleBuffered = true;
        SetStyle(ControlStyles.UserPaint |
                      ControlStyles.AllPaintingInWmPaint |
                      ControlStyles.ResizeRedraw |
                      ControlStyles.ContainerControl |
                      ControlStyles.OptimizedDoubleBuffer |
                      ControlStyles.SupportsTransparentBackColor
                      , true);
    }

我也试过这个

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

它将我的屏幕的整个背景更改为黑色。

但问题仍然存在,有人可以告诉我如何解决这个问题以及我在哪里做错了? 非常感谢。

1 个答案:

答案 0 :(得分:2)

如果没有更多的话,我的直觉就是你要么在这些区域添加大量数据,要么正在进行大量的调整。

在更新屏幕的任何地方尝试此操作(向listviews / boxes / etc添加行)或调整屏幕大小,或任何其他会导致屏幕重绘的内容。 例如:

public void something_resize(object sender, EventArgs e)
{
    try
    {
        this.SuspendLayout(); 

        // Do your update, add data, redraw, w/e. 
        // Also add to ListViews and Boxes etc in Batches if you can, not item by item.  
    }
    catch
    {
    }
    finally
    {
        this.ResumeLayout(); 
    }
} 

将ResumeLayout()调用放在finally块中很重要,因为如果因为w / e原因发生异常,您希望窗口布局,无论您对异常执行什么操作。