簡體   English   中英

c#.net中如何避免TableLayoutPanel閃爍

[英]How to avoid flickering in TableLayoutPanel in c#.net

我正在使用 TableLayoutPanel 進行考勤標記。 我在這個 TableLayoutPanel 中添加了控件(一個面板和一個標簽)並為它們創建了事件。 在某些情況下,我已清除所有控件並繼續在 TableLayoutPanel 的不同 position 中綁定相同的控件。 重新綁定控件時,TableLayoutPanel 會閃爍並且初始化速度太慢。

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

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

// add controls

panel.ResumeLayout();

還要看看使用雙緩沖。 您必須創建 TableLayoutPanel 的子類。 請參閱此處的示例。

這對我很有用消除由於 windows 形式的 TableLayoutPanel 和面板而導致的閃爍

這是該鏈接中的內容(逐字復制)

完全消除由於 windows 表格 go 中的 TableLayoutPanel 和面板導致的閃爍,如下所示:=- 1. 設置表格的雙緩沖屬性 =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 於 2014 年 2 月 16 日下午 20:11 發布

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

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 中將其添加到受影響的 class 的底部,我向您保證它會起作用。

在 C# 中,將該屬性與其他屬性一起添加到 class 的頂部。

它基本上等待 Winform 的完整呈現,並消除正在繪制到屏幕上的表單的閃爍。 如果您還沒有測試它,請不要忽視。 我在winform延遲方面遇到了一個大問題,這解決了它。

我最終使用了另一種選擇,因為我的很多 UI 都使用透明度作為背景 colors。 我知道這會顯着降低 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM