0

我正在 vb.net (winforms) 中创建一个应用程序。
在主窗体中,我创建了 4 个面板,每个面板都有大约 15 个控件。该表单还有 4 个按钮来切换面板。每个按钮设置当前面板可见 = false 和另一个面板可见 = true。

表单有一个 backgroundImage 并且面板是透明的。如果我切换面板,您会看到背景被重绘(它“闪烁”)。我不想这样,所以我想出了一个解决方案:我用相同的图像设置了 4 个面板的背景,现在“闪烁”消失了,但面板的控件绘制速度非常慢 - 特别是当第一个面板的控件与第二个面板的控件位于同一位置。

我已经尝试过“SuspendLayout”和“ResumeLayout”,所以没有
我还尝试在 firstPanel.visible = false 和 secondPanel.visible = true 之间执行“Refresh()”,但随后又出现“闪烁”。

那么,有人有一些解决方案可以让我的应用程序更快吗?



编辑:重要的是,如果我在没有 backgroundImage 的情况下尝试相同的方法,它工作正常!

4

1 回答 1

0

我编写了一个自定义控制面板来解决我的应用程序的缓慢渲染性能。这是代码:

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

在您的表单上添加以下代码:

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
于 2020-07-22T10:57:09.253 回答