【问题标题】:How to avoid flickering in TableLayoutPanel in c#.netc#.net中如何避免TableLayoutPanel闪烁
【发布时间】:2011-07-13 10:32:36
【问题描述】:

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

【问题讨论】:

    标签: c# winforms tablelayoutpanel


    【解决方案1】:

    在您添加所有控件之前暂停布局。

    TableLayoutPanel panel = new TabelLayoutPanel();
    panel.SuspendLayout();
    
    // add controls
    
    panel.ResumeLayout();
    

    也看看使用双缓冲。您必须创建 TableLayoutPanel 的子类。查看示例here

    【讨论】:

    • 这很有帮助,我现在的抖动少了很多。谢谢。
    【解决方案2】:

    这对我很有效Remove flickering due to TableLayoutPanel & Panel in windows form

    链接中的内容(逐字复制)

    完全消除由于 TableLayoutPanel & Panel in windows窗体如下:=- 1.设置Form的双缓冲属性=true。 2.将以下2个函数粘贴到form.cs中

    #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 于 2014 年 2 月 16 日晚上 20:11 发布

    【讨论】:

    • 在那里添加了一个引用块。但是请注意,仅添加指向其他帖子的链接并复制所有内容本身并不是正确的归属。如果您除了链接中的详细信息之外没有其他要添加的内容,那么最好将它们保留为 cmets。下次小心点。
    【解决方案3】:

    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 延迟方面遇到了一个大问题,这解决了它。

    【讨论】:

    • 这如何回答这个问题?
    • 将它添加到受影响类的底部,我向你保证它会起作用。它基本上等待 Winform 的完整呈现。如果您还没有测试它,请不要忽视。我在 winform 延迟方面遇到了一个大问题,这解决了它。
    • 这很好地回答了这个问题,整个表单都解决了闪烁问题,感谢这篇文章。
    【解决方案4】:

    使用此面板将属性 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; }
            }
    }
    

    【讨论】:

    • 您能描述一下为什么应该将 dBuffer 设置为 true 吗?
    【解决方案5】:

    我最终使用了另一种替代方法,因为我的很多 UI 都使用透明度作为背景颜色。我知道这会显着降低 WINFORMS 的性能。然而,这不是 WPF 应用程序的情况(通常不显示为闪烁),因此转换可能是有益的。

    【讨论】:

      【解决方案6】:
      //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);
              }
      

      //完美适用于表格布局面板的双缓冲解决方案,不会发生闪烁

      【讨论】:

        【解决方案7】:

        作为对上述内容的改进,我得到了更好的结果:

            TableLayoutPanel panel = new TabelLayoutPanel();
            panel.SuspendLayout();
            panel.StopPaint();
        
            // add controls
        
            panel.ResumePaint();
            panel.ResumeLayout();
        

        【讨论】:

        • StopPaintResumePaint 不是 TableLayoutPanel 控件的本机方法。您显然正在使用一个扩展程序,而您的答案中没有包含该扩展程序。
        猜你喜欢
        • 2023-03-22
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        • 2016-06-30
        • 2013-11-16
        相关资源
        最近更新 更多