连接新的Picturebox Paint事件

时间:2016-08-06 19:22:27

标签: vb.net class events picturebox

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

基本上我已经在一个类中创建了picturebox,我想挂接paint事件,并在主窗体类中新建类时触发paint事件。也许我做错了,也许这不是最佳做法,也许有人可以提出更好的方法。目前,当我将类调用为主窗体时,类变量是通过的,但是我从来没有看到我声明“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

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

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

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

1 个答案:

答案 0 :(得分:0)

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

如果有断点,显然VS只会进入事件处理程序。

相关问题