如何避免屏幕闪烁?

问题描述:

我想提出一个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; 
} 
} 

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

但仍然问题仍然是一样的,有人能告诉我如何解决这个问题,我在哪里做错了? 非常感谢。

+8

屏幕不闪烁没有更新。所以请向我们展示重复更新闪烁部分的代码。 – 2013-05-08 13:13:28

+2

什么控件类型是闪烁的?你在做什么这些问题控制? EG:加载100,000条记录以供显示等等...... – Justin 2013-05-08 13:13:44

+1

你对这个表格做什么?我的意思是:在哪个动作闪烁?如果你制作了一个WinForm,比如面板等3个区域,并在这些面板上放置了一些控件,它就不应该闪烁。无论是点击面板还是拖动窗口。 – Michael 2013-05-08 13:13:58

没有更多的事情要做,我的直觉告诉你要么在这些区域添加大量数据,要么进行大量调整。

在任何更新屏幕(向列表视图/框/等添加行)或调整屏幕大小或导致屏幕重绘的任何其他位置尝试此操作。 例如:

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块,因为如果发生了瓦特/电子理由例外,你希望你的窗口布局,不管你用异常怎么办。

+0

我试过这个,但它不工作..你能告诉我这是什么原因吗? – Mogli 2013-05-09 05:16:53

+0

是的,最终它的工作。我不知道为什么它以前不工作。 – Mogli 2013-06-06 04:48:32