CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Moving Images

  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Thumbs up Moving Images [solved]

    Hi guys!
    Wizbang was so kind as to suppluy me with code, to move an image control along the edges of the form. it starts from the top left corner, moves right, then down, then left, lastly it moves up again and so the image continues.

    What I want to achieve is to:
    1) image start top right and moves opposite direction
    2) image start at bottom right and moves left, then up etc.
    3) image start bottom left then moves right etc.
    I'm busy with (3), this is what I have:
    Code:
    Private Sub Timer1_Timer()
    If Abs(D) > 60 Then
      T = T + D / 2 'move up or down
    Else
      L = L + D 'move left or right
    End If
    
    If L <= 0 Then
      L = 30
      D = 120 'start moving down
    ElseIf L + Image1.Width > Screen.Width - 60 Then
      L = Screen.Width - Image1.Width - 60
      D = -120 'start moving up
    ElseIf T < 30 Then
      T = 30
      D = -60 'start moving left
    ElseIf T + Image1.Height >= Screen.Height - Image1.Height Then
      T = Screen.Height - Image1.Height - 60
      D = 60 'start moving right
     ' L = L + Image1.Width + 2
    
      If L + Image1.Width >= Screen.Width - 70 Then
      L = Screen.Width - Image1.Width - 60
      D = 120 'start moving up
      T = T - D
      Image1.Top = T - 100
      End If
    
    End If
    Image1.Left = L
    Image1.Top = T
    End Sub
    what happens here is that the image gets stuck in the bottom right corner.

    How could I fix this¿
    Last edited by HanneSThEGreaT; July 31st, 2004 at 05:08 AM.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    You can do it in many ways...

    This is one that might be easy to follow...
    Code:
    'You should have an array of picctureboxes or imageboxes...
    'This code suppose you have an array of 0-3 =4 pictureboxes
    Option Explicit
    
    Private Function MoveLeftToRight(ByVal PicImage As PictureBox) As Boolean
        With PicImage
            If .Left + .Width < Me.ScaleWidth Then
                .Left = .Left + .Width
                MoveLeftToRight = True
            Else
               .Left = Me.ScaleWidth - .Width
               'return false by default
            End If
        End With
    End Function
    
    Private Function MoveRightToLeft(ByVal PicImage As PictureBox) As Boolean
        With PicImage
            If .Left - .Width > 0 Then
                .Left = .Left - .Width
                MoveRightToLeft = True
            Else
               .Left = 0
               'return false by default
            End If
        End With
    End Function
    
    Private Function MoveTopToBottom(ByVal PicImage As PictureBox) As Boolean
        With PicImage
            If .Top + .Height < Me.ScaleHeight Then
                .Top = .Top + .Height
                MoveTopToBottom = True
            Else
               .Top = Me.ScaleHeight - .Height
               'return false by default
            End If
        End With
    End Function
    
    Private Function MoveBottomToTop(ByVal PicImage As PictureBox) As Boolean
        With PicImage
            If .Top - .Height > 0 Then
                .Top = .Top - .Height
                MoveBottomToTop = True
            Else
               .Top = 0
               'return false by default
            End If
        End With
    End Function
    
    Private Sub Timer1_Timer()
        Dim PicImage As PictureBox
        For Each PicImage In PicRun
            Select Case PicImage.Index
                Case 0 'RightToLeft,UpToDown,LeftToRight,downToUp
                    Select Case PicImage.Tag
                        Case 1, ""
                            If Not MoveRightToLeft(PicImage) Then
                                PicImage.Tag = 2
                            End If
                        Case 2
                            If Not MoveTopToBottom(PicImage) Then
                                PicImage.Tag = 3
                            End If
    
                        Case 3
                            If Not MoveLeftToRight(PicImage) Then
                                PicImage.Tag = 4
                            End If
                        Case 4
                            If Not MoveBottomToTop(PicImage) Then
                                PicImage.Tag = 1
                            End If
                    End Select
    
                Case 1 'rightToleft,bottomToUp,leftToRight,upToDown
                    Select Case PicImage.Tag
                        Case 1, ""
                            If Not MoveRightToLeft(PicImage) Then
                                PicImage.Tag = 2
                            End If
                        Case 2
                            If Not MoveBottomToTop(PicImage) Then
                                PicImage.Tag = 3
                            End If
    
                        Case 3
                            If Not MoveLeftToRight(PicImage) Then
                                PicImage.Tag = 4
                            End If
                        Case 4
                            If Not MoveTopToBottom(PicImage) Then
                                PicImage.Tag = 1
                            End If
                    End Select
                Case 2 'bottomToTop,LeftToRight,UpToDown,RightToLeft
                    Select Case PicImage.Tag
                        Case 1, ""
                            If Not MoveBottomToTop(PicImage) Then
                                PicImage.Tag = 2
                            End If
                        Case 2
                            If Not MoveLeftToRight(PicImage) Then
                                PicImage.Tag = 3
                            End If
                        
                        Case 3
                            If Not MoveTopToBottom(PicImage) Then
                                PicImage.Tag = 4
                            End If
                        Case 4
                            If Not MoveRightToLeft(PicImage) Then
                                PicImage.Tag = 1
                            End If
                    End Select
                Case 3 'topDown,leftRight,DownUp,RightLeft
                    Select Case PicImage.Tag
                        Case 1, ""
                            If Not MoveTopToBottom(PicImage) Then
                                PicImage.Tag = 2
                            End If
                        Case 2
                            If Not MoveLeftToRight(PicImage) Then
                                PicImage.Tag = 3
                            End If
                        
                        Case 3
                            If Not MoveBottomToTop(PicImage) Then
                                PicImage.Tag = 4
                            End If
                        Case 4
                            If Not MoveRightToLeft(PicImage) Then
                                PicImage.Tag = 1
                            End If
                    End Select
            End Select
        Next
    End Sub
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Dec 2001
    Posts
    6,332
    The example I gave you was to move the image clockwise around the form, no matter where it starts, so you only need to put the image in the corner of your choice. Now, all that is needed is to enable counter-clockwise movement.

    Here is the modification to do that (see attached).

    EDIT: Just a tiny update...(new attachement)
    Attached Files Attached Files
    Last edited by WizBang; July 29th, 2004 at 10:21 AM.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283
    Thanx for all your help guys!

    HannesTheVeryBad....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured