如何避免c#.net中TableLayoutPanel中的闪烁

时间:2011-07-13 10:32:37

标签: c# winforms tablelayoutpanel

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

7 个答案:

答案 0 :(得分:18)

暂停布局,直到您添加了所有控件。

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

// add controls

panel.ResumeLayout();

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

答案 1 :(得分:5)

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

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

  

完全消除因TableLayoutPanel& amp;而导致的闪烁小组   windows窗体如下:= -   1.设置Form = 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. SetDoubleBuffered(“TableLaoutPannel_controlName”)TableLayoutPannelPannelSplitcontainer&所有容器   控件。
  2.         

    感谢RhishikeshLathe发表于16-Feb-14 20:11 pm

答案 2 :(得分: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; }
        }
}

答案 3 :(得分:0)

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延迟方面遇到了一个大问题,并且此问题得以解决。

答案 4 :(得分:0)

还有另一种选择,我最终在UI中使用了很多透明色作为背景色。我知道这会大大降低WINFORMS的性能。但是,对于WPF应用程序不是这种情况(通常不显示为闪烁),因此进行转换可能会有所帮助。

答案 5 :(得分:0)

import tkinter as tk

# --- function ---

def on_selection(event):
    # here you can get selected element
    print('previous:', listbox.get('active'))
    print(' current:', listbox.get(listbox.curselection()))

    # or using `event`

    #print('event:', event)
    #print('widget:', event.widget)
    print('(event) previous:', event.widget.get('active'))
    print('(event)  current:', event.widget.get(event.widget.curselection()))

    lbl['text'] = "Seleced: " + listbox.get(listbox.curselection())
    print('---')

# --- main ---

root = tk.Tk()

listbox = tk.Listbox(root)
listbox.pack()

listbox.insert(1, 'Hello 1')
listbox.insert(2, 'Hello 2')
listbox.insert(3, 'Hello 3')
listbox.insert(4, 'Hello 4')
listbox.bind('<<ListboxSelect>>', on_selection)

lbl = tk.Label(root, text='?')
lbl.pack()

root.mainloop()

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

答案 6 :(得分:-1)

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

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

    // add controls

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