如何在C#.NET中避免TableLayoutPanel中的闪烁问题

13

我正在使用TableLayoutPanel来进行考勤标记。我已经在TableLayoutPanel中添加了控件(一个面板和一个标签),并为它们创建了事件。在某些情况下,我已经清除了所有控件,并绑定了相同的控件到TableLayoutPanel的不同位置。重新绑定控件时,TableLayoutPanel会闪烁,并且初始化速度非常缓慢。

7个回答

25

在添加全部控件之前,暂停布局。

TableLayoutPanel panel = new TabelLayoutPanel();
panel.SuspendLayout();

// add controls

panel.ResumeLayout();

还要考虑使用双缓冲技术。您将需要创建一个TableLayoutPanel的子类。在此处可见一个示例。


这很有帮助,现在我的抖动少多了。谢谢。 - Jmnstr

10

VB.net:

   Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H2000000
            Return cp
        End Get
    End Property

C#:

    protected override CreateParams CreateParams
    {
     get
     {
      CreateParams cp = base.CreateParams;
      cp.ExStyle = cp.ExStyle | 0x2000000;
      return cp;
     }
    }

在VB中,将其添加到受影响的类底部,我向您保证它会起作用。

在C#中,将该属性与其他属性一起添加到类顶部。

它基本上等待Winform的完全渲染,并消除了表单绘制到屏幕时的闪烁。如果您还没有测试过,请不要忽视它。我曾经遇到过Winform延迟的巨大问题,而这个方法解决了它。


2
这个回答如何解决问题? - Bill Tür stands with Ukraine
将其添加到受影响的类底部,我向您保证它会起作用。它实际上等待Winform的完全渲染。如果您还没有测试过,请不要忽略。我曾经遇到过Winform延迟的巨大问题,并且这个方法解决了它。 - user6329238
这个回答非常好,整个表单的闪烁问题得到了解决,感谢您的帖子。 - Daniel Arena

10

这对我非常有效 移除 Windows 窗体中 TableLayoutPanel 和 Panel 引起的闪烁

以下是该链接中的内容(逐字复制)

完全移除 Windows 窗体中 TableLayoutPanel 和 Panel 引起的闪烁,可按如下步骤进行:=- 1. 将 Form 的 double buffered 属性设置为 true。 2. 在 form.cs 中粘贴以下 2 个函数。

#region .. Double Buffered function ..
   public static void SetDoubleBuffered(System.Windows.Forms.Control c)
    {
        if (System.Windows.Forms.SystemInformation.TerminalServerSession)
            return;
        System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered",
        System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance);
        aProp.SetValue(c, true, null);
    }

   #endregion


   #region .. code for Flucuring ..

   protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }

    #endregion
  1. 对每个TableLayoutPannelPannelSplitcontainerDatagridview以及所有容器控件调用SetDoubleBuffered("TableLaoutPannel_controlName")

感谢RhishikeshLathe于16-Feb-14 20:11pm发布此内容。


1
在那里添加了一个引用块。但是请注意,仅添加到其他帖子的链接并复制所有内容本身并不是适当的归属。如果除了链接中的细节之外没有其他要添加的内容,则最好将它们留作评论。下次小心。 - Bhargav Rao

1
使用此面板将属性dBuffer设置为true。
public partial class MyTableLayoutPanel : TableLayoutPanel
{
        public MyTableLayoutPanel()
        {
            InitializeComponent();
        }

        public MyTableLayoutPanel(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        /// <summary>
        /// Double buffer
        /// </summary>
        [Description("Double buffer")]
        [DefaultValue(true)]
        public bool dBuffer
        {
            get { return this.DoubleBuffered; }
            set { this.DoubleBuffered = value; }
        }
}

2
你能描述一下为什么应该将dBuffer设置为true吗? - Robin Ellerkmann

1

我最终采用了另一种选择,因为我的许多UI都使用透明度作为背景颜色。我知道这会显著降低WINFORMS的性能。但是,在WPF应用程序中通常不会出现闪烁,因此进行转换可能会有益。


1
//Call this function on form load.
SetDoubleBuffered(tableLayoutPanel1);


public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;
            System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance);
            aProp.SetValue(c, true, null);
        }

//双缓冲解决方案在表格布局面板上完美运作,不会出现闪烁


-1
作为对上述方法的改进,我使用以下方法取得了更好的结果:
    TableLayoutPanel panel = new TabelLayoutPanel();
    panel.SuspendLayout();
    panel.StopPaint();

    // add controls

    panel.ResumePaint();
    panel.ResumeLayout();

8
“StopPaint”和“ResumePaint”不是TableLayoutPanel控件的本地方法。显然,您正在使用一个扩展,但您没有在答案中包含它。 - LarsTech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接