8

在我的应用程序中,我确实需要以良好的顺序放置很多控件(标签、文本框、域向上)。所以我继续使用一些嵌套TableLayoutPanel的 . 现在的问题是,此表单对大多数事件(调整大小、最大化、最小化和......)的响应非常慢,表格中的控件需要 5 秒才能调整大小,重新绘制为新的表单大小。

我现在把一根手指放在我的眼睛里!如果这种形式在我的家用 PC 上这么慢(i7@4GHz 和一个好的显卡),明天它会在工作中的旧 P4 计算机上做什么?

我什至尝试使用下面的代码,但它绝对没有任何作用,如果它没有更慢的话!

private void FilterForm_ResizeBegin(object sender, EventArgs e)
{
    foreach(TableLayoutPanel tlp in panelFilters.Controls)
    {
        if(tlp != null)
        {
            tlp.SuspendLayout();
        }
    }
}

private void FilterForm_ResizeEnd(object sender, EventArgs e)
{
    foreach (TableLayoutPanel tlp in panelFilters.Controls)
    {
        if (tlp != null)
        {
            tlp.ResumeLayout();
        }
    }
}

请让我知道是否有使 tablelayoutpanel 工作得更快的技巧......或者如果您知道更好的方法来放置大约数百个控件很好地对齐。

4

2 回答 2

23

使用此代码。

public class CoTableLayoutPanel : TableLayoutPanel
{
    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.CacheText, true);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= NativeMethods.WS_EX_COMPOSITED;
            return cp;
        }
    }

    public void BeginUpdate()
    {
        NativeMethods.SendMessage(this.Handle, NativeMethods.WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
    }

    public void EndUpdate()
    {
        NativeMethods.SendMessage(this.Handle, NativeMethods.WM_SETREDRAW, new IntPtr(1), IntPtr.Zero);
        Parent.Invalidate(true);
    }
}

public static class NativeMethods
{
    public static int WM_SETREDRAW = 0x000B; //uint WM_SETREDRAW
    public static int WS_EX_COMPOSITED = 0x02000000; 


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); //UInt32 Msg
}
于 2012-04-06T03:26:03.017 回答
1

If you create a new class derived from TableLayoutPanel and set the ControlStyles such that DoubleBuffered is true, your performance will improve dramatically.

public class MyPanel :  TableLayoutPanel
{
    public MyPanel()
    {
        this.SetStyle(ControlStyles.DoubleBuffer, true);
    }

}
于 2012-01-17T19:02:05.913 回答