如何避免屏幕闪烁
本文关键字:闪烁 屏幕 何避免 | 更新日期: 2023-09-27 18:12:33
我正在制作一个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);
}
i also tried this
protected override CreateParams CreateParams
{
get
{
CreateParams handleParam = base.CreateParams;
handleParam.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return handleParam;
}
}
它把我的整个屏幕背景变成黑色。
但问题仍然是一样的,有人能告诉我如何解决这个问题,我在哪里做错了?
没有更多的继续,我的直觉告诉你,你要么在这些区域添加大量的数据,要么有大量的调整大小。
尝试在任何地方更新屏幕(添加行到listviews/boxes/等)或调整屏幕大小,或任何其他将导致屏幕重绘。例:
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原因发生异常,你希望你的窗口布局,不管你对异常做了什么。