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

发布于 2024-11-19 14:07:07 字数 174 浏览 5 评论 0 原文

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

I am using a TableLayoutPanel for attendance marking purposes. I have added controls (a Panel and a Label) inside of this TableLayoutPanel and created events for them. In some conditions I have cleared all of the controls and proceeded to bind the same controls in different position of TableLayoutPanel. While re-binding the controls, the TableLayoutPanel flickers and is far too slow in initializing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟花肆意 2024-11-26 14:07:07

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

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

// add controls

panel.ResumeLayout();

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

Suspend the layout until you've added all your controls on.

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

// add controls

panel.ResumeLayout();

Also look at using Double Buffering. You'll have to create a sub-class of the TableLayoutPanel. See an example here.

離人涙 2024-11-26 14:07:07

VB.net:C#

   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

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

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

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

它本质上等待 Winform 的完整渲染,并消除绘制到屏幕上的窗体的闪烁。如果您还没有测试过,请不要忽视。我遇到了 winform 延迟的大问题,这解决了它。

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;
     }
    }

In VB add it to the bottom of affected class and I assure you it will work.

In C# add the property to the top of the class along with your other properties.

It essentially awaits the full render of the Winform, and removes the flickering of the form being painted to the screen. If you havent tested it please dont disregard. I had a huge issue with winform latency and this fixed it.

爱你是孤单的心事 2024-11-26 14:07:07

这对我来说非常有用 删除由于 TableLayoutPanel & 导致的闪烁; Windows 表单中的面板

这里是该链接中的内容(逐字复制)

完全消除由于TableLayoutPanel & 造成的闪烁。面板输入
Windows 窗体如下:=-
1. 设置Form的双缓冲属性=true。
2. 在form.cs中粘贴以下2个函数

#region .. 双缓冲函数 ..
   公共静态无效SetDoubleBuffered(System.Windows.Forms.Control c)
    {
        if (System.Windows.Forms.SystemInformation.TerminalServerSession)
            返回;
        System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered",
        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance);
        aProp.SetValue(c, true, null);
    }

   #endregion


   #region .. Flucuring 代码 ..

   受保护覆盖​​ CreateParams CreateParams
    {
        得到
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            返回cp;
        }
    }

    #endregion
  • 为每个 TableLayoutPannelPannelSplitcontainerDatagridview 调用 SetDoubleBuffered(“TableLaoutPannel_controlName”) &所有集装箱
    控制。
  • 感谢 RhishikeshLathe 于 2014 年 2 月 16 日晚上 20:11 发布

    This worked great for me Remove flickering due to TableLayoutPanel & Panel in windows form

    Here what's in that link (copied verbatim)

    Completely Remove flickering due to TableLayoutPanel & Panel in
    windows form go as follows:=-
    1. Set double buffered property of Form =true.
    2. Paste Following 2 functions in 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. Call SetDoubleBuffered(“TableLaoutPannel_controlName”) for each TableLayoutPannel,Pannel, Splitcontainer, Datagridview & all container
      controls.

    Thanks to RhishikeshLathe Posted 16-Feb-14 20:11pm

    失眠症患者 2024-11-26 14:07:07

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

    Use this panel to set the property 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; }
            }
    }
    
    雨轻弹 2024-11-26 14:07:07

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

    There is another alternative that I ended up using as quite alot of my UI was using Transparency for background colors. I understand that this significantly degrades performance in WINFORMS. However this isnt the case with WPF applications (not usually visible as a flicker), so a conversion could be beneficial.

    回梦 2024-11-26 14:07:07
    //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);
            }
    

    //与表格布局面板的双缓冲解决方案完美配合,并且不会发生闪烁

    //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);
            }
    

    //Works perfectly the double buffered solution for table layout panel and no flickering happens

    不再让梦枯萎 2024-11-26 14:07:07

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

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

    As an improvement of the above, I had better results with:

        TableLayoutPanel panel = new TabelLayoutPanel();
        panel.SuspendLayout();
        panel.StopPaint();
    
        // add controls
    
        panel.ResumePaint();
        panel.ResumeLayout();
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文