0

I'm creating an application in vb.net (winforms).
In the mainform, I create 4 panels and every panel has around 15 controls. The form also has 4 buttons to switch the panels. Every button sets the current panel visible = false and another panel visible = true.

The form has a backgroundImage and the panels are transparent. If I switch a panel, you see background being redrawn (it 'flickers'). I don't want that, so I came up with a solution: I set the background of the 4 panels with the same image and now the 'flickering' is gone, but the controls of the panels are drawn very slow - especially when a control of the first panel is on the same location as a control of the second panel.

I already tried doing 'SuspendLayout' and 'ResumeLayout', so that doesn't work for me.
I also tried doing a 'Refresh()' between the firstPanel.visible = false and the secondPanel.visible = true but then I get the 'flickering' again.

So, anybody has some solutions to make my application faster?



EDIT: Important to say that if I try the same without the backgroundImage, it works fine!

Vella
  • 71
  • 1
  • 3
  • 11
  • 3
    Can't say if this will make your application faster, but I will try to set the property `Form.DoubleBuffered = True`. Tell me if it changes your situation – Steve May 07 '13 at 14:28
  • 1
    @Steve It removes the flickering, but to make it draw faster you also need to remove the other image backgrounds. – PurkkaKoodari May 07 '13 at 14:29
  • @Pietu1998 of course, with a change like this you need to start over again in testing the performances – Steve May 07 '13 at 14:30
  • Forgot to mention that the Form.DoubleBuffered = true... I heard that could be another solution, but not here... Thinking of creating the application in WPF instead, hoping to get better performance. – Vella May 07 '13 at 14:31
  • The answer is right here: http://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls – Vella May 07 '13 at 15:01

1 Answers1

0

I've written a custom control panel to address the slow rendering performance on my Apps. here's the code:

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

And on your Forms add this code bellow:

Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or 33554432
        Return cp
    End Get
End Property
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23