【问题标题】:How to avoid screen flickering?如何避免屏幕闪烁?
【发布时间】:2013-05-02 17:46:27
【问题描述】:

我正在制作一个 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;
}
}

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

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

【问题讨论】:

  • 没有更新屏幕不会闪烁。所以请向我们展示重复更新闪烁部分的代码。
  • 闪烁的控件类型是什么?你在用这些问题控制做什么? EG:加载 100,000 条记录以供显示等...
  • 你用这个表格做什么?我的意思是:它在哪个动作上闪烁?如果您制作一个 WinForm,例如,3 个面板等区域并将一些控件放入这些面板中,它根本不应该闪烁。既不是点击面板也不是拖动窗口。
  • 在您的解决方案尝试代码中:this.DoubleBuffered = true;和双缓冲=真;做同样的事情。与 SetStyle 调用相同。因为“this”显式引用了本地范围(至少在这段代码中被剪掉了)。如果你在代码的其他地方重复调用这些东西并快速设置和取消设置某些值,这也会产生闪烁。 (在 OP 添加新代码 sn-p 之后:)您仍然应该告诉我们您的 PROB 区域内发生了什么。 (这些是 userControls 吗?PictureBoxes?你是在他们的背景上绘画吗(使用 OnPaint)?)
  • 我做了一些事情(但现在已经大约 4 年了......)阻止屏幕布局更新,直到我完成我的工作......我认为它类似于“SuspendLayout( )" 和 "ResumeLayout()"...你可能想看看它。

标签: c# winforms flicker


【解决方案1】:

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

在您更新屏幕(向 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 原因发生异常,您希望您的窗口进行布局,无论您对异常执行什么操作。

【讨论】:

  • 我试过了,但它不起作用..你能告诉我这背后的原因是什么吗?
  • 是的,终于成功了。我不知道为什么它以前不起作用。
猜你喜欢
  • 2020-10-21
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 2023-03-22
  • 2013-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多