【问题标题】:Hookup new Picturebox Paint Event连接新的 Picturebox Paint 事件
【发布时间】:2016-12-12 23:23:09
【问题描述】:

我是新手,所以要温柔。基本上,我正在尝试将 .gif 绘制到图片框并将其添加到 flowlayout 面板。这样做的原因是,如果我不计算帧数并绘制它,gif 就会一直循环播放。

基本上,我已经在一个类中创建了图片框,并且我想连接绘制事件,并在主窗体类中新建类时触发绘制事件。也许我做错了,也许这不是最佳做法,或者也许有人可以提出更好的方法。目前,当我将类表单称为主表单时,会单步执行类变量,但我从未看到我已声明“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 但没有动画。也许我错过了什么。

【问题讨论】:

    标签: vb.net class events picturebox


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 2015-04-23
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2014-07-29
      相关资源
      最近更新 更多