【问题标题】:Winforms slow gui (drawing controls) - vb.netWinforms 慢 gui(绘图控件) - vb.net
【发布时间】:2013-05-01 13:15:55
【问题描述】:

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

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

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

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



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

【问题讨论】:

  • 不能说这是否会使您的应用程序更快,但我会尝试设置属性Form.DoubleBuffered = True。告诉我它是否会改变你的情况
  • @Steve 它消除了闪烁,但要使其绘制得更快,您还需要消除其他图像背景。
  • @Pietu1998 当然,有了这样的改变,你需要重新开始测试性能
  • 忘了说Form.DoubleBuffered = true...我听说这可能是另一种解决方案,但不是在这里...考虑在WPF中创建应用程序,希望获得更好的性能。

标签: vb.net winforms performance user-interface


【解决方案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

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

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

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多