EvanBlack
EvanBlack

Reputation: 759

Slow rendering when adding UserControl to Panel

Well, I have an application which works with a dynamic panel at mid and when I select something from toolbar, panel changes with;

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))

But rendering is too slow than I expect (even on a good configured brand new PC). All transparency keys set default and transparency is off. I am doing some database actions on usercontrol's load also (but its still slow when I disable them). I guess I need something like;

Click Toolbar Button -> Visibility: off -> Disable rendering -> Apply usercontrol to panel -> Paint all objects and do database work - > Enable rendering -> Visibility: on

I am searching something like this, please help. Thanks in advance :)

Upvotes: 1

Views: 1463

Answers (1)

Miguel Tomás
Miguel Tomás

Reputation: 1911

I've written a custom control panel for improving rendering like this:

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

Upvotes: 2

Related Questions