Animating a Picture

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • I Hate My Computer
    New Member
    • Mar 2007
    • 44

    Animating a Picture

    How can I make a picture slide across the form in VBA. It can be pre-programmed or random what ever is easier. I need to be able to see it move at any given speed.

    Thanks for your support.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by I Hate My Computer
    How can I make a picture slide across the form in VBA. It can be pre-programmed or random what ever is easier. I need to be able to see it move at any given speed.
    Moving it is easy - you can either set the .Top and/or .Left properties, or use the .Move method.

    Comment

    • I Hate My Computer
      New Member
      • Mar 2007
      • 44

      #3
      Originally posted by Killer42
      Moving it is easy - you can either set the .Top and/or .Left properties, or use the .Move method.
      I understand but this moves it so fast that I can't see it happen.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by I Hate My Computer
        I understand but this moves it so fast that I can't see it happen.
        Have you tried putting a DoEvents in the loop? This might make a slight difference.

        You could also invoke the Sleep function, which is somewhere in the API. Try a search for terms such as "VB SLEEP" on TheScripts. I think it allows you to put your program to sleep (basically, tell the CPU to ignore it) for a specified number of milliseconds.

        Comment

        • tristanlbailey
          New Member
          • Apr 2007
          • 30

          #5
          How are you using the picture's .Left and .Top properties or Move method? Are you using them in a loop of some kind?

          One way of doing this at a fairly consistent pace, independent of the computer's overall speed, is by using a Timer control.

          Example:
          (to move from left to right)


          Code:
          Private Sub Timer1_Timer()
          
              Picture1.Move Picture1.Left + 15
          
          End Sub
          Set the Interval property of the Timer control to "50" (50 milliseconds). You may adjust this value after testing the code, to meet your needs.

          Somewhere else in your program's code, you will switch the timer on by changing its Enabled property value to "True" (causing the picture to begin moving), and then switch the timer off by changing its Enabled property value to "False" (stopping the movement of the picture).

          The direction in which the picture moves can be adjusted by changing the "+" (plus) to a "-" (minus), causing it to move right; and/or by giving the Move method a Top value to move the picture up or down.

          Example
          (to move diagonally down-right)

          Code:
          Private Sub Timer1_Timer()
          
              Picture1.Move Picture1.Left + 15, Picture1.Top - 15
          
          End Sub
          If the form's ScaleMode property is set to "Twips", the number added or subtracted is equal to the number of twips that the picture is moved, when the Timer's Timer event is executed each interval. One pixel is equal to 15 twips.

          Get the picture?

          Comment

          • I Hate My Computer
            New Member
            • Mar 2007
            • 44

            #6
            Originally posted by tristanlbailey
            How are you using the picture's .Left and .Top properties or Move method? Are you using them in a loop of some kind?

            One way of doing this at a fairly consistent pace, independent of the computer's overall speed, is by using a Timer control.

            Example:
            (to move from left to right)


            Code:
            Private Sub Timer1_Timer()
            
                Picture1.Move Picture1.Left + 15
            
            End Sub
            Set the Interval property of the Timer control to "50" (50 milliseconds). You may adjust this value after testing the code, to meet your needs.

            Somewhere else in your program's code, you will switch the timer on by changing its Enabled property value to "True" (causing the picture to begin moving), and then switch the timer off by changing its Enabled property value to "False" (stopping the movement of the picture).

            The direction in which the picture moves can be adjusted by changing the "+" (plus) to a "-" (minus), causing it to move right; and/or by giving the Move method a Top value to move the picture up or down.

            Example
            (to move diagonally down-right)

            Code:
            Private Sub Timer1_Timer()
            
                Picture1.Move Picture1.Left + 15, Picture1.Top - 15
            
            End Sub
            If the form's ScaleMode property is set to "Twips", the number added or subtracted is equal to the number of twips that the picture is moved, when the Timer's Timer event is executed each interval. One pixel is equal to 15 twips.

            Get the picture?
            I get it but VBA has no Private Sub Timer1_Timer() type control. It has OnTime.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Unfortunately, VBA doesn't have a timer control. Unbelievable, but that's MS for you.

              There are ways to simulate it, and third-party controls you can use. For example, see this thread.

              (Sorry, I typed this a couple of hours ago but forgot to hit Submit.)

              Comment

              • I Hate My Computer
                New Member
                • Mar 2007
                • 44

                #8
                Originally posted by Killer42
                Unfortunately, VBA doesn't have a timer control. Unbelievable, but that's MS for you.

                There are ways to simulate it, and third-party controls you can use. For example, see this thread.

                (Sorry, I typed this a couple of hours ago but forgot to hit Submit.)
                I know that I have this code so far:

                Code:
                Sub ShapeMover()
                    Image1.Left = Image1.Left + 6
                    Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="ShapeMover"
                End Sub
                When I run this it says sub or function not defined and this is all the code I have. How can I fix this?

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by I Hate My Computer
                  I know that I have this code so far:

                  Code:
                  Sub ShapeMover()
                      Image1.Left = Image1.Left + 6
                      Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="ShapeMover"
                  End Sub
                  When I run this it says sub or function not defined and this is all the code I have. How can I fix this?
                  At what point does it say this? When it runs, or when you try to compile it?

                  Maybe you need to qualify the name you pass, like "Module1.ShapeM over" or something. Or is it complaining about the TimeValue( ) function?

                  Comment

                  • I Hate My Computer
                    New Member
                    • Mar 2007
                    • 44

                    #10
                    Originally posted by Killer42
                    At what point does it say this? When it runs, or when you try ti compile it?

                    Maybe you need to qualify the name you pass, like "Module1.ShapeM over" or something. Or is it complaining about the TimeValue( ) function?
                    It says this when I close the form. I run it and it moves the shape then I wait for the timer for about a minute but it never kicks in so I close it. At that point after I close it it shows the error message. The problem is that It is not a module I want to rerun the "ShapeMover " thing.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by I Hate My Computer
                      It says this when I close the form. I run it and it moves the shape then I wait for the timer for about a minute but it never kicks in so I close it. At that point after I close it it shows the error message. The problem is that It is not a module I want to rerun the "ShapeMover " thing.
                      Well yes, but the ShapeMover routine will still reside in something. It might be a worksheet in Excel, or whatever.

                      Hm... after checking the doco, I think the parameter name When is wrong. Try LatestTime.

                      Correction: I think EarliestTime is required, but you should also set LatestTime (to the same value, in this case).

                      Comment

                      • I Hate My Computer
                        New Member
                        • Mar 2007
                        • 44

                        #12
                        Originally posted by Killer42
                        Well yes, but the ShapeMover routine will still reside in something. It might be a worksheet in Excel, or whatever.

                        Hm... after checking the doco, I think the parameter name When is wrong. Try LatestTime.

                        Correction: I think EarliestTime is required, but you should also set LatestTime (to the same value, in this case).
                        This gave me a compile error saying that the named argument was not found.

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by I Hate My Computer
                          This gave me a compile error saying that the named argument was not found.
                          Oh. That's interesting.

                          According to the online help here, which is for VBA in Excel 2003, the syntax is...
                          expression.OnTi me(EarliestTime , Procedure, LatestTime, Schedule)

                          Presumably you're using a different version. Can you check the parameters?

                          It looks as though I had the meaning of LatestTime wrong, anyway. I thought that if the procedure had not been run by this time, VBA would force it to run immediately. But in fact, the doco says...
                          If Microsoft Excel is not in Ready mode within 30 seconds, the procedure won’t be run.
                          (Note, the 30 seconds relates to their example.)

                          Perhaps you should look into obtaining a 3rd party ActiveX timer control, or something.

                          Comment

                          • tristanlbailey
                            New Member
                            • Apr 2007
                            • 30

                            #14
                            I appologise for my earlier post. I didn't realise that VBA didn't have the Timer control. Oh well...

                            Another alternative is to create "Do" loops. Although not as smooth or independent of computer processing speed, this method should still be OK.

                            Example:

                            Declarations
                            Code:
                            Private I As Integer
                            Private Mode As Integer
                            The example makes the use of a command button on the form, to activate and deactivate the movement of the picture.

                            General Code
                            Code:
                            Private Sub Command1_Click()
                            
                                Mode = Not Mode
                                If Mode = -1 Then Call PicMove
                            
                            End Sub
                            
                            Private Sub PicMove()
                            
                                If Mode = -1 Then
                                    Do
                                        Do
                                            I = I + 1
                                        Loop Until I = 10000
                                        I = 0
                                        Picture1.Move Picture1.Left + 1
                                        DoEvents
                                    Loop Until Mode = 0
                                End If
                            
                            End Sub

                            Again, the form's ScaleMode property should be set to "Twips". Note that the value added on the the Picture's Left property has changed from "15" to "1". You will also want to adjust the maximum loop value of "I", depending on the speed of the computer you want to use your program on.

                            Comment

                            • I Hate My Computer
                              New Member
                              • Mar 2007
                              • 44

                              #15
                              Originally posted by tristanlbailey
                              I appologise for my earlier post. I didn't realise that VBA didn't have the Timer control. Oh well...

                              Another alternative is to create "Do" loops. Although not as smooth or independent of computer processing speed, this method should still be OK.

                              Example:

                              Declarations
                              Code:
                              Private I As Integer
                              Private Mode As Integer
                              The example makes the use of a command button on the form, to activate and deactivate the movement of the picture.

                              General Code
                              Code:
                              Private Sub Command1_Click()
                              
                                  Mode = Not Mode
                                  If Mode = -1 Then Call PicMove
                              
                              End Sub
                              
                              Private Sub PicMove()
                              
                                  If Mode = -1 Then
                                      Do
                                          Do
                                              I = I + 1
                                          Loop Until I = 10000
                                          I = 0
                                          Picture1.Move Picture1.Left + 1
                                          DoEvents
                                      Loop Until Mode = 0
                                  End If
                              
                              End Sub

                              Again, the form's ScaleMode property should be set to "Twips". Note that the value added on the the Picture's Left property has changed from "15" to "1". You will also want to adjust the maximum loop value of "I", depending on the speed of the computer you want to use your program on.
                              This didn't do anything. No error, it just didn't do anything.

                              Comment

                              Working...