使用计时器推进帧的 VB.NET 动画图像类 - 这是一个好方法吗?

发布于 2024-10-05 16:46:50 字数 2168 浏览 0 评论 0原文

VB.NET、.NET 4

您好,

我正在编写一个工业控制前端,它有一些“奇特”的图形来指示某些机器的状态。例如,我通过显示从加热器图片中发出的红色波浪箭头来指示某些加热器已打开。我通过创建一个继承自 PictureBox 的类并使用计时器来推进图像来实现此目的:

Public Class AnimatedPictureBox
    Inherits PictureBox

    Private WithEvents Timer As New Timers.Timer
    Public Property Interval As Double
        Get
            Return Timer.Interval
        End Get
        Set(ByVal value As Double)
            Timer.Interval = value
        End Set
    End Property    

    Public ImageList As New List(Of Image)
    Private NextImageIndex As Integer = 0

    Public Sub New(ByVal Interval As Integer)
        MyBase.New()
        Timer = New Timers.Timer
        Me.Interval = Interval
        Me.SizeMode = PictureBoxSizeMode.StretchImage
        Me.Visible = True
    End Sub
    Public Sub New()
        Me.New(100)
    End Sub

    Public Sub BeginAnimation(ByVal [Loop] As Boolean)
        Timer.Start()
    End Sub
    Public Sub BeginAnimation()
        BeginAnimation([Loop]:=True)
    End Sub
    Public Sub StopAnimation()
        Timer.Stop()
    End Sub

    Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
        InvokeControl(Me, _
            Sub(x)
                x.Image = x.ImageList(NextImageIndex)
                x.NextImageIndex += 1
                If x.NextImageIndex > x.ImageList.Count - 1 Then x.NextImageIndex = 0
            End Sub)
    End Sub
End Class

其中 InvokeControl 是处理控件的跨线程编组的模块中的子例程:

Private Sub InvokeControl(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T))
    If Control.InvokeRequired Then
        Try
            Control.Invoke(New Action(Of T, Action(Of T))(AddressOf InvokeControl), New Object() {Control, Action})
        Catch ex As Exception
            '..Handle the error..
        End Try
    Else
        Action(Control)
    End If
End Sub

我的问题是“这是解决此问题的好方法吗?”还是有一些明显更好的方法?”我不太擅长编程,并且担心多个带有嵌入式定时器的对象会对 CPU 造成负担。任何建议将不胜感激!

提前致谢, 布莱恩·

P.S.我使用动画 GIF 来处理其他一些动画内容,但在这里,我希望能够处理比 GIF 更大的调色板的图像格式(如果这有意义的话)。换句话说,当我尝试将一些动画保存为 GIF 时,图像质量受到了无法接受的打击。

VB.NET, .NET 4

Hello,

I am writing an industrial control front-end that has some "fancy" graphics to indicate the states of some machinery. For example, I indicate that some heaters are on by showing red wavy arrows emanating out from a picture of a heater. I accomplished this by creating a class that inherits from PictureBox and using a timer to advance the images:

Public Class AnimatedPictureBox
    Inherits PictureBox

    Private WithEvents Timer As New Timers.Timer
    Public Property Interval As Double
        Get
            Return Timer.Interval
        End Get
        Set(ByVal value As Double)
            Timer.Interval = value
        End Set
    End Property    

    Public ImageList As New List(Of Image)
    Private NextImageIndex As Integer = 0

    Public Sub New(ByVal Interval As Integer)
        MyBase.New()
        Timer = New Timers.Timer
        Me.Interval = Interval
        Me.SizeMode = PictureBoxSizeMode.StretchImage
        Me.Visible = True
    End Sub
    Public Sub New()
        Me.New(100)
    End Sub

    Public Sub BeginAnimation(ByVal [Loop] As Boolean)
        Timer.Start()
    End Sub
    Public Sub BeginAnimation()
        BeginAnimation([Loop]:=True)
    End Sub
    Public Sub StopAnimation()
        Timer.Stop()
    End Sub

    Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
        InvokeControl(Me, _
            Sub(x)
                x.Image = x.ImageList(NextImageIndex)
                x.NextImageIndex += 1
                If x.NextImageIndex > x.ImageList.Count - 1 Then x.NextImageIndex = 0
            End Sub)
    End Sub
End Class

Where InvokeControl is a subroutine in a module that handles cross-thread marshalling of controls:

Private Sub InvokeControl(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T))
    If Control.InvokeRequired Then
        Try
            Control.Invoke(New Action(Of T, Action(Of T))(AddressOf InvokeControl), New Object() {Control, Action})
        Catch ex As Exception
            '..Handle the error..
        End Try
    Else
        Action(Control)
    End If
End Sub

My question is "Is this an alright way to go about this or is there some obvious better way?" I'm not too good at programming and am worried that having several of these objects with their embedded timers taxing the CPU. Any suggestions would be greatly appreciated!

Thanks in advance,
Brian

P.S. I went with animated GIFs for some other animation stuff, but here, I wanted to be able to handle image formats that can handle a larger palette than a GIF (if that makes sense). In other words, when I tried to save some of my animations as GIFs, the image quality took an unacceptable hit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-10-12 16:46:50

我认为这是一个很好的做法。计时器为您提供了很大的灵活性,并且正如 Mike 所说,它们不会占用任何 CPU 时间。

I think this is a good approach. The timers give you a lot of flexibility, and like Mike said, they won't take any CPU time to speak of.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文