连接新的 Picturebox Paint 事件

Hookup new Picturebox Paint Event

我是新手,所以要温柔。基本上我正在尝试将 .gif 绘制到图片框上并将其添加到流程布局面板中。原因是如果我不计算帧数并绘制它,gif 会一直循环。

基本上我已经在单个 class 中创建了图片框,我想连接绘画事件,并在 class 在主视图中更新时触发绘画事件表格 class。也许我做错了,也许这不是最佳做法,或者有人可以建议更好的做法。当前,当我将 class 表单称为主表单时,class 变量会逐步执行,但我从未看到我已声明 "withevents" 的图片框被逐步执行。此外,Paint 事件也永远不会介入。请在下面查看我的代码

    Public Class CompleteMark
    Implements IDisposable

    Dim WithEvents _picBox As PictureBox

#Region "VARIABLES"

    'Disposable variables
    Private managedResource As System.ComponentModel.Component
    Private unmanagedResource As IntPtr
    Protected disposed As Boolean = False

    'Class Variables
    Dim _cm As New Bitmap(My.Resources.cmt)
    Dim _currentlyAnimating As Boolean = False
    Dim _frameCounter As Integer = 0
    Dim _frameCount As Integer
    Dim _frameDim As Imaging.FrameDimension


    '_picBox  = New PictureBox()




#End Region

    Sub New(ByVal flp As FlowLayoutPanel)
        _picBox = New PictureBox()
        _picBox.Size = New Size(14, 13)
        _picBox.Margin = New Padding(0, 0, 0, 0)
        _picBox.Name = "AnimCheckMark"
        _picBox.Image = _cm
        flp.Controls.Add(_picBox)
        _picBox.Invalidate()

        'AddHandler _picBox.Paint, AddressOf AnimCheckMark_Paint

    End Sub

    Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
        _picBox.Invalidate()
    End Sub

    Private Sub AnimateImage()
        If Not _currentlyAnimating Then
            ImageAnimator.Animate(_cm, New EventHandler(AddressOf OnFrameChanged))
            _currentlyAnimating = True
        End If
    End Sub

    Private Sub AnimCheckMark_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles _picBox.Paint
        _frameDim = New Imaging.FrameDimension(_cm.FrameDimensionsList(0))
        _frameCount = _cm.GetFrameCount(_frameDim)

        If _frameCounter < _frameCount Then
            AnimateImage()
            ImageAnimator.UpdateFrames()
            e.Graphics.DrawImage(_cm, New Point(0, 0))
            _frameCounter += 1
        End If
    End Sub

#Region " IDisposable Support "

    Protected Overridable Overloads Sub Dispose(
            ByVal disposing As Boolean)
        If Not Me.disposed Then
            If disposing Then
                managedResource.Dispose()
            End If
            ' Add code here to release the unmanaged resource.
            unmanagedResource = IntPtr.Zero
            ' Note that this is not thread safe. 
        End If
        Me.disposed = True
    End Sub

    Public Sub AnyOtherMethods()
        If Me.disposed Then
            Throw New ObjectDisposedException(Me.GetType().ToString,
                    "This object has been disposed.")
        End If
    End Sub


    ' Do not change or add Overridable to these methods. 
    ' Put cleanup code in Dispose(ByVal disposing As Boolean). 
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
    Protected Overrides Sub Finalize()
        Dispose(False)
        MyBase.Finalize()
    End Sub
#End Region

End Class

然后我在主窗体上用测试按钮新建了这个class:

Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
    Dim chkMark As CompleteMark
    chkMark = New CompleteMark(flpAdobeLbl)
End Sub

在表单上创建了图片框,并且显示了 .gif 但没有动画。也许我错过了什么。

糟糕!解决了它,上面的代码可以很好地连接到一个事件。我只是愚蠢地没有在事件处理程序中设置断点,所以 VS 没有进入它。

显然 VS 只会在存在断点时进入事件处理程序。