0

我正在尝试以模拟旋转轮的窗口形式为按钮的背景图像设置动画(比旋转鼠标指针更优雅)。我在第二个线程中执行此操作,该线程运行一个循环动画按钮背景图像旋转(第一个线程是创建表单的主线程)。

这是类,第二个线程运行 spin() 方法:

Public Class spinningWhell
    Private Delegate Sub DelRefreshButton()
    Public rotate As Boolean
    Private TargetButton As Button
    Private ButtonForm As Windows.Forms.Form
    Private myImage As Image

    Public Sub New(ByRef PassedButton As Button)
        TargetButton = PassedButton
        ButtonForm = PassedButton.Parent
        myImage = TargetButton.BackgroundImage.Clone
    End Sub

    Public Sub spin()
        Do While rotate
            myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
            Thread.Sleep(100)
            refreshButton()
        Loop
    End Sub
    Private Sub refreshButton()
        If ButtonForm.InvokeRequired Then
            Dim del As New DelRefreshButton(AddressOf refreshButton)
            ButtonForm.Invoke(del)
        End If
        TargetButton.BackgroundImage = myImage
        ButtonForm.Refresh()

        'Form1.Button1.Refresh()
        'TargetButton.Refresh()
        'Application.OpenForms.Item(TargetButton.Parent.Name).Refresh()
        'Form1.Button1.Refresh()
    End Sub

End Class

尽管使用了 Invoke 方法,但 buttonform.refresh() 给了我一个交叉的异常。我试图调用按钮或表单(ButtonForm.refresh 之后的注释行),但唯一有效的是 Form1.button1.refresh() 其中 Form1 和 Button1 是表单和按钮的名称。有什么建议吗?

4

1 回答 1

3

实际更新 GUI 的两行需要位于ELSE块中,因此它们仅在方法实际在主 UI 线程上执行时才执行:

改变:

Private Sub refreshButton()
    If ButtonForm.InvokeRequired Then
        Dim del As New DelRefreshButton(AddressOf refreshButton)
        ButtonForm.Invoke(del)
    End If
    TargetButton.BackgroundImage = myImage
    ButtonForm.Refresh()
End Sub

至:

Private Sub refreshButton()
    If ButtonForm.InvokeRequired Then
        Dim del As New DelRefreshButton(AddressOf refreshButton)
        ButtonForm.Invoke(del)
    Else
        TargetButton.BackgroundImage = myImage
        ButtonForm.Refresh()
    End If
End Sub
于 2013-05-05T15:55:08.443 回答