【发布时间】:2011-07-13 10:32:36
【问题描述】:
我正在使用 TableLayoutPanel 进行考勤标记。我在这个 TableLayoutPanel 中添加了控件(一个面板和一个标签)并为它们创建了事件。在某些情况下,我已清除所有控件并继续在 TableLayoutPanel 的不同位置绑定相同的控件。重新绑定控件时,TableLayoutPanel 会闪烁并且初始化速度太慢。
【问题讨论】:
标签: c# winforms tablelayoutpanel
我正在使用 TableLayoutPanel 进行考勤标记。我在这个 TableLayoutPanel 中添加了控件(一个面板和一个标签)并为它们创建了事件。在某些情况下,我已清除所有控件并继续在 TableLayoutPanel 的不同位置绑定相同的控件。重新绑定控件时,TableLayoutPanel 会闪烁并且初始化速度太慢。
【问题讨论】:
标签: c# winforms tablelayoutpanel
在您添加所有控件之前暂停布局。
TableLayoutPanel panel = new TabelLayoutPanel();
panel.SuspendLayout();
// add controls
panel.ResumeLayout();
也看看使用双缓冲。您必须创建 TableLayoutPanel 的子类。查看示例here。
【讨论】:
这对我很有效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
- 为每个
TableLayoutPannel、Pannel、Splitcontainer、Datagridview和所有容器调用SetDoubleBuffered(“TableLaoutPannel_controlName”)控件。感谢 RhishikeshLathe 于 2014 年 2 月 16 日晚上 20:11 发布
【讨论】:
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 延迟方面遇到了一个大问题,这解决了它。
【讨论】:
使用此面板将属性 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; }
}
}
【讨论】:
我最终使用了另一种替代方法,因为我的很多 UI 都使用透明度作为背景颜色。我知道这会显着降低 WINFORMS 的性能。然而,这不是 WPF 应用程序的情况(通常不显示为闪烁),因此转换可能是有益的。
【讨论】:
//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);
}
//完美适用于表格布局面板的双缓冲解决方案,不会发生闪烁
【讨论】:
作为对上述内容的改进,我得到了更好的结果:
TableLayoutPanel panel = new TabelLayoutPanel();
panel.SuspendLayout();
panel.StopPaint();
// add controls
panel.ResumePaint();
panel.ResumeLayout();
【讨论】:
StopPaint 和 ResumePaint 不是 TableLayoutPanel 控件的本机方法。您显然正在使用一个扩展程序,而您的答案中没有包含该扩展程序。