Microsoft Excel

Excel und VBA: Formeln, Programmierung, Lösungen

Die Excel/VBA-Beispiele

Thema

UserForm-Images animieren

Gruppe

Image

Problem

In einer UserForm werden Grafiken beim Überfahren mit der Maus animiert dargestellt.

Lösung
Geben Sie den Ereigniscode in das Klassenmodul der UserForm ein.

ClassModule: frmAnimation

Dim bln As Boolean

Private Sub cmdCancel_Click()
   Unload Me
End Sub

Private Sub Image1_Click()
   bln = False
   Image1.Picture = LoadPicture(ThisWorkbook.Path & "\prev_button_on.gif")
   Label1.Caption = CLng(Label1.Caption) + 1
   Me.Repaint
End Sub

Private Sub Image2_Click()
   bln = False
   Image2.Picture = LoadPicture(ThisWorkbook.Path & "\prev_button_on.gif")
   Label1.Caption = CLng(Label1.Caption) - 1
   Me.Repaint
End Sub

Private Sub Image2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   Call ChangeImage(Image2, X, Y)
End Sub

Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   Call ChangeImage(Image1, X, Y)
End Sub

Private Sub ChangeImage(img As Control, X As Single, Y As Single)
   Dim sPath As String, sPctA As String, sPctB As String
   sPath = ThisWorkbook.Path & "\"
   If img.Name = "Image2" Then
      sPctA = sPath & "prev_button.gif"
      sPctB = sPath & "prev_button_on.gif"
   Else
      sPctA = sPath & "next_button.gif"
      sPctB = sPath & "next_button_on.gif"
   End If
   If bln = False Then
      If X > 2 And Y > 2 And X < 16 And Y < 16 Then
         img.Picture = LoadPicture(sPctA)
      Else
         img.Picture = LoadPicture(sPctB)
         bln = False
      End If
      Me.Repaint
   End If
End Sub

StandardModule: Modul1

Sub CallForm()
   frmAnimation.Show
End Sub