Results 1 to 16 of 16

Thread: best way to float and move randomly a picture on top of a vb6 form

  1. #1
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 07
    Posts
    3,000

    best way to float and move randomly a picture on top of a vb6 form

    i am trying to determine how to float move randomly a picture on top of a form. should i use a picturebox or another borderless form or maybe even a child form. Once i determine the best way I will post another question on how to randomly move it and keep it within the boundaries of the form it is over
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,756

    Re: best way to float and move randomly a picture on top of a vb6 form

    Use a Picturebox or an Image Control

    Code:
    Dim nX As Single
    Dim nY As Single
    
    Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     nX = X / Screen.TwipsPerPixelX
     nY = Y / Screen.TwipsPerPixelY
    End Sub
    
    Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       Image1.Move Image1.Left + ((X / Screen.TwipsPerPixelX)) - nX, Image1.Top + ((Y / Screen.TwipsPerPixelY)) - nY
     End If
    End Sub
    
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     nX = X / Screen.TwipsPerPixelX
     nY = Y / Screen.TwipsPerPixelY
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       Picture1.Move Picture1.Left + ((X / Screen.TwipsPerPixelX)) - nX, Picture1.Top + ((Y / Screen.TwipsPerPixelY)) - nY
     End If
    End Sub
    NOTE: If your Picturebox has it's ScaleMode = vbPixels then don't use the Screen.TwipsPerPixelX and Screen.TwipsPerPixelY
    Last edited by jmsrickland; Aug 6th, 2012 at 09:46 AM.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 07
    Posts
    3,000

    Re: best way to float and move randomly a picture on top of a vb6 form

    Quote Originally Posted by jmsrickland View Post
    Use a Picturebox or an Image Control

    Code:
    Dim nX As Single
    Dim nY As Single
    
    Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     nX = X / Screen.TwipsPerPixelX
     nY = Y / Screen.TwipsPerPixelY
    End Sub
    
    Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       Image1.Move Image1.Left + ((X / Screen.TwipsPerPixelX)) - nX, Image1.Top + ((Y / Screen.TwipsPerPixelY)) - nY
     End If
    End Sub
    
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     nX = X / Screen.TwipsPerPixelX
     nY = Y / Screen.TwipsPerPixelY
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = 1 Then
       Picture1.Move Picture1.Left + ((X / Screen.TwipsPerPixelX)) - nX, Picture1.Top + ((Y / Screen.TwipsPerPixelY)) - nY
     End If
    End Sub
    NOTE: If your Picturebox has it's ScaleMode = vbPixels then don't use the Screen.TwipsPerPixelX and Screen.TwipsPerPixelY
    Thanks for adding that, but this has nothing to do with the mouse and i don't think an image control will float on top of other controls, only a picturebox
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,756

    Re: best way to float and move randomly a picture on top of a vb6 form

    I'm sorry, I thought you wanted to move a picture on top of a VB6 Form using a mouse.

    So, you want to move the picture using code to move picture here and there over other objects on a Form. Without going into code then you need to do a random number selection for X and Y of the picture and make sure that these values keep the picture within the borders of the Form. Is this correct?

    Question:

    Do you want the picture to show without showing the container box it's in, like moving a ball around the Form?
    Last edited by jmsrickland; Aug 6th, 2012 at 10:50 AM.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  5. #5
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,756

    Re: best way to float and move randomly a picture on top of a vb6 form

    Quote Originally Posted by isnoend07 View Post
    Thanks for adding that, but this has nothing to do with the mouse and i don't think an image control will float on top of other controls, only a picturebox
    Correct, but you can simulate an Image control if you use a User Control that has a transparent image on it.
    Last edited by jmsrickland; Aug 6th, 2012 at 11:01 AM.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  6. #6
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 07
    Posts
    3,000

    Red face Re: best way to float and move randomly a picture on top of a vb6 form

    Quote Originally Posted by jmsrickland View Post
    I'm sorry, I thought you wanted to move a picture on top of a VB6 Form using a mouse.

    So, you want to move the picture using code to move picture here and there over other objects on a Form. Without going into code then you need to do a random number selection for X and Y of the picture and make sure that these values keep the picture within the borders of the Form. Is this correct?
    Yes that is correct

    Question:

    Do you want the picture to show without showing the container box it's in, like moving a ball around the Form?
    yes this is a Keno game jackpot, trying to duplicate how the ones at the casino works. have not made the image yet. Wanted to see if and how it works
    first
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  7. #7
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,756

    Re: best way to float and move randomly a picture on top of a vb6 form

    Maybe you could look up on the net about how to make a sprite program.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  8. #8
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 07
    Posts
    3,000

    Re: best way to float and move randomly a picture on top of a vb6 form

    Quote Originally Posted by jmsrickland View Post
    Maybe you could look up on the net about how to make a sprite program.
    Thanks I will try
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 03
    Location
    Argentina
    Posts
    4,321

    Re: best way to float and move randomly a picture on top of a vb6 form

    You need to use a PictureBox, it has it's own window and you can also control it's Zorder (Front/Back). About how to move he picturebox with the mouse, it's as easy as:
    Code:
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static lx  As Single, ly As Single
        If Button = 0 Then
            lx = X
            ly = Y
        ElseIf Button = vbLeftButton Then
            Picture1.Left = Picture1.Left + X - lx
            Picture1.Top = Picture1.Top + Y - ly
        End If
    End Sub
    Explanation on the code: If we would set Left and Top values to be X and Y parameters then we would always be dragging the PictureBox by holding its Top-Left corner, so this code first gets the distance between Left/Top PictureBox borders and the X/Y coordinates where the mouse moves (before dragging start), so having this offsets, when dragging starts by holding left mouse button down we can substract them from Left and Top to be able to drag the control by taking it from the real spot where the user clicked inside the PictureBox. Static variables are perfect for doing this since they always retain the value they had the last time the Sub/Function executed, keeping this value even when the Sub/Function where they are declared lose scope. Using modular declared variables for doing this would be exactly the same thing.

    EDIT: I see you'll also need transparency, then a PictureBox may not be enough for that, you could use other sort of control like the controls Lavolpe's submitted to the CodeBank.
    Last edited by jcis; Aug 11th, 2012 at 02:04 AM.

  10. #10
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 07
    Posts
    3,000

    Re: best way to float and move randomly a picture on top of a vb6 form

    Quote Originally Posted by jcis View Post
    You need to use a PictureBox, it has it's own window and you can also control it's Zorder (Front/Back). About how to move he picturebox with the mouse, it's as easy as:
    Code:
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static lx  As Single, ly As Single
        If Button = 0 Then
            lx = X
            ly = Y
        ElseIf Button = vbLeftButton Then
            Picture1.Left = Picture1.Left + X - lx
            Picture1.Top = Picture1.Top + Y - ly
        End If
    End Sub
    Explanation on the code: If we would set Left and Top values to be X and Y parameters then we would always be dragging the PictureBox by holding its Top-Left corner, so this code first gets the distance between Left/Top PictureBox borders and the X/Y coordinates where the mouse moves (before dragging start), so having this offsets, when dragging starts by holding left mouse button down we can substract them from Left and Top to be able to drag the control by taking it from the real spot where the user clicked inside the PictureBox. Static variables are perfect for doing this since they always retain the value they had the last time the Sub/Function executed, keeping this value even when the Sub/Function where they are declared lose scope. Using modular declared variables for doing this would be exactly the same thing.

    EDIT: I see you'll also need transparency, then a PictureBox may not be enough for that, you could use other sort of control like the controls Lavolpe's submitted to the CodeBank.
    Thanks for adding that, but this has nothing to do with the mouse as i told jmsrickland. Don't need transparency
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  11. #11
    PowerPoster jcis's Avatar
    Join Date
    Jan 03
    Location
    Argentina
    Posts
    4,321

    Re: best way to float and move randomly a picture on top of a vb6 form

    What is the movement you need the PBox to perform? like a ball bouncing inside the Form's borders or what?

  12. #12
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 07
    Posts
    3,000

    Re: best way to float and move randomly a picture on top of a vb6 form

    Quote Originally Posted by jcis View Post
    What is the movement you need the PBox to perform? like a ball bouncing inside the Form's borders or what?
    was thinking kind of like as screen saver where the pic just floats around without disappearing past the form.
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  13. #13
    PowerPoster jcis's Avatar
    Join Date
    Jan 03
    Location
    Argentina
    Posts
    4,321

    Re: best way to float and move randomly a picture on top of a vb6 form

    See this example, add a Timer and a PictureBox (Timer1 and Picture1)
    Code:
    Private Const mSpeed As Single = 5
    
    Private Sub Form_Load()
        With Picture1
            .AutoSize = True
            .BorderStyle = 0
            .BackColor = vbYellow
            .Picture = Me.Icon
        End With
        Timer1.Interval = 1
        Timer1.Enabled = True
        Me.ScaleMode = vbPixels
        Picture1.ScaleMode = vbPixels
    End Sub
    
    Private Sub Timer1_Timer()
    Static GoingLeft As Boolean, GoingUp As Boolean
    Dim NewX As Single, NewY As Single
    
        With Picture1
            If GoingLeft Then
                If .Left - mSpeed <= 0 Then
                    NewX = .Left
                    GoingLeft = False
                Else
                    NewX = .Left - mSpeed
                End If
            Else
                If .Left + .ScaleWidth + mSpeed >= Me.ScaleWidth Then
                    NewX = Me.ScaleWidth - .ScaleWidth
                    GoingLeft = True
                Else
                    NewX = .Left + mSpeed
                End If
            End If
            If GoingUp Then
                If .Top - mSpeed < 0 Then
                    NewY = .Top
                    GoingUp = False
                Else
                    NewY = .Top - mSpeed
                End If
            Else
                If .Top + .ScaleHeight + mSpeed > Me.ScaleHeight Then
                    NewY = Me.ScaleHeight - .ScaleHeight
                    GoingUp = True
                Else
                    NewY = .Top + mSpeed
                End If
            End If
            .Move NewX, NewY
        End With
    End Sub
    You can resize your Form even at runtime to test that the PictureBox is confined inside the Form. You can also change speed by using different values for constant mSpeed.
    Last edited by jcis; Aug 11th, 2012 at 03:03 PM.

  14. #14
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,756

    Re: best way to float and move randomly a picture on top of a vb6 form

    Hi jcis,

    That's pretty cool app. I did one like that in Java using many balls balls bouncing off the walls of the Form and if any two collided they blew up. I'm wondering, however, if this will resolve the OP's request as I was under the impression that he wanted a random like movement without any reason to direction of the object instead of straight-line. I guess he will say so.Anyway, like I said, it is a really cool app.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  15. #15
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,756

    Re: best way to float and move randomly a picture on top of a vb6 form

    Hi jcis,

    That's pretty cool app. I did one like that in Java using many balls balls bouncing off the walls of the Form and if any two collided they blew up. I'm wondering, however, if this will resolve the OP's request as I was under the impression that he wanted a random like movement without any rhyme or reason to direction of the object, kind of like an aquarium of fish freely swimming around in all directions, instead of straight-line. I guess he will say so. Anyway, like I said, it is a really cool app.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  16. #16
    PowerPoster jcis's Avatar
    Join Date
    Jan 03
    Location
    Argentina
    Posts
    4,321

    Re: best way to float and move randomly a picture on top of a vb6 form

    Well, this is how Windows screensavers do it, the image bouncing on the screen borders in 90 degree angles. The movement will look less repetitive If the Form is not a perfect square. Anyway many other effects can be made, for example, by suddenly using different speeds for x and y or making not linear movement, curves, spiral, whatever..

Posting Permissions

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