加速TableLayoutPanel

时间:2017-04-26 19:55:26

标签: .net vb.net

我在winform中有几个嵌套的TableLayoutPanels。它花了很长时间来绘制(?)它们等等,所以我使用这段代码使它几乎是瞬间完成的(它是一个双缓冲的TableLayoutPanel):

Imports System.Runtime.InteropServices

Public Class CoTableLayoutPanel
    Inherits TableLayoutPanel
    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.CacheText, True)
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or NativeMethods.WS_EX_COMPOSITED
            Return cp
        End Get
    End Property

    Public Sub BeginUpdate()
        NativeMethods.SendMessage(Me.Handle, NativeMethods.WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero)
    End Sub

    Public Sub EndUpdate()
        NativeMethods.SendMessage(Me.Handle, NativeMethods.WM_SETREDRAW, New IntPtr(1), IntPtr.Zero)
        Parent.Invalidate(True)
    End Sub
End Class

Public NotInheritable Class NativeMethods
    Private Sub New()
    End Sub
    Public Shared WM_SETREDRAW As Integer = &HB
    'uint WM_SETREDRAW
    Public Shared WS_EX_COMPOSITED As Integer = &H2000000


    <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
    End Function
    'UInt32 Msg
End Class

当调整表单大小时以及当我使用下一个代码来调整行的大小时,它的效果非常好,它的工作速度非常快:

TableLayoutPanel1.RowStyles(0) = New RowStyle(SizeType.Percent, 100)
TableLayoutPanel1.RowStyles(1) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel1.RowStyles(2) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel1.RowStyles(3) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel1.RowStyles(4) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel1.RowStyles(5) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel1.RowStyles(6) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel1.RowStyles(7) = New RowStyle(SizeType.Percent, 0)
TableLayoutPanel2.RowStyles(0) = New RowStyle(SizeType.AutoSize)

问题在于我将它们恢复到原来的大小:

TableLayoutPanel1.RowStyles(0) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(1) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(2) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(3) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(4) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(5) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(6) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel1.RowStyles(7) = New RowStyle(SizeType.Percent, 12.5)
TableLayoutPanel2.RowStyles(0) = New RowStyle(SizeType.Percent, 28.15)

使用这段代码,似乎他们应该以相同的速度调整大小,但它没有,并且明显更加轻松。有没有人有任何关于我能做什么的建议,以便尽快调整大小?

0 个答案:

没有答案