将UserControl添加到Panel时渲染速度慢

时间:2010-08-23 09:30:03

标签: .net vb.net winforms render

好吧,我有一个应用程序可以在中间使用动态面板,当我从工具栏中选择一些内容时,面板会随之改变;

Public Sub loadContent(ByVal o As UserControl)
    frmMain.panelMid.Visible = False
    frmMain.panelMid.Controls.Clear()
    frmMain.panelMid.Controls.Add(o)
    frmMain.panelMid.Visible = True
End Sub)

loadContent(New UserControl1(someParameters))

但是渲染速度比我预期的要慢(即使是配置良​​好的全新PC)。所有透明度键设置为默认值,透明度为关闭。我也在usercontrol的加载上做一些数据库操作(但是当我禁用它时它仍然很慢)。我想我需要类似的东西;

点击工具栏按钮 - >可见性:off - >禁用渲染 - >将usercontrol应用于面板 - >绘制所有对象并进行数据库工作 - >启用渲染 - >可见性:

我正在搜索这样的内容,请帮忙。 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

我已经编写了一个自定义控制面板来改善这种渲染效果:

Public Class PanelDoubleBuffer
    Inherits Panel

    'MAIN LAYOUT design scheme
    Public Property PANEL_CLOSED_STATE_DIM As Integer = 40
    Public Property PANEL_OPEN_STATE_DIM As Integer = 400
    Public Property ShowVerticalScrolBar As Boolean = False
    Public Property ShowHorizontalScrolBar As Boolean = False

    Public Sub New()
        SuspendLayout()

        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.UserPaint, True)

        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.ResizeRedraw, True)
        Me.UpdateStyles()
        ResumeLayout()
    End Sub

    <DllImport("user32.dll")>
    Private Shared Function ShowScrollBar(ByVal hWnd As IntPtr, ByVal wBar As Integer, ByVal bShow As Boolean) As Boolean
    End Function

    Public Property SB_HORZ As Integer = ShowHorizontalScrolBar
    Public Property SB_VERT As Integer = ShowVerticalScrolBar
    Public Property SB_CTL As Integer = 2
    Public Property SB_BOTH As Integer = 3

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = &H85 Then
            ShowScrollBar(Me.Handle, CInt(SB_BOTH), False)
        End If

        MyBase.WndProc(m)
    End Sub


    <DllImport("user32.dll")>
    Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function
    Private Const WM_SETREDRAW As Integer = &HB

    Private Sub PanelView_Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs)
        Dim control As Control = TryCast(sender, Control)

        If control IsNot Nothing Then

            If e.Type = ScrollEventType.ThumbTrack Then
                SendMessage(control.Handle, WM_SETREDRAW, 1, 0)
                control.Refresh()
                SendMessage(control.Handle, WM_SETREDRAW, 0, 0)
            Else
                SendMessage(control.Handle, WM_SETREDRAW, 1, 0)
                control.Invalidate()
            End If
        End If
    End Sub
End Class