Results 1 to 7 of 7

Thread: Random window position.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Devon, UK
    Posts
    311

    Random window position.

    How do i make my app startup in a random window position?
    Last edited by Chrome; Oct 30th, 2002 at 05:39 PM.
    Sam.

  2. #2
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    VB Code:
    1. Private Sub Form_Load()
    2.     RandomFormPos Me
    3. End Sub
    4.  
    5. Public Sub RandomFormPos(frm As Form)
    6.    
    7.     Dim scrW As Integer, scrH As Integer, _
    8.         newTop As Integer, newLeft As Integer
    9.    
    10.     Let scrH = Screen.Height
    11.     Let scrW = Screen.Width
    12.    
    13.     Do
    14.         Randomize
    15.         newTop = Rnd * scrH
    16.     Loop Until Not newTop + frm.Height > scrH
    17.    
    18.     Do
    19.         Randomize
    20.         newLeft = Rnd * scrW
    21.     Loop Until Not newLeft + frm.Width > scrW
    22.    
    23.     frm.Top = newTop
    24.     frm.Left = newLeft
    25.  
    26. End Sub


    Is this what you wanted?

    Jason

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Devon, UK
    Posts
    311
    well almsot, but that only seemes to move the app, to around 3 or 4 random places, i want it all over the screen.

    please help.
    Sam.

  4. #4
    Fanatic Member Alien_poo's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    668
    Uh, will this not work??? A lot less code...

    VB Code:
    1. Private Sub Form_Load()
    2.     Randomize Timer
    3.     Form1.Top = (Rnd * (Screen.Height - 1) + 1)
    4.     Form1.Left = (Rnd * (Screen.Width - 1) + 1)
    5. End Sub

    And if you want your form to move all around the place put the code in a timer...
    "A RESPECTED scientist has put forward the stunning - if unsavoury - possibility that humans are descended from sewage dumped overboard by aliens."
    "First we read that we are the creation of God, then scientists say we are descended from apes. Now they say we're some sort of alien poo. How much further can we sink?"
    - Robert Matthews, Science Correspondent

  5. #5
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    Originally posted by Alien_poo
    Uh, will this not work??? A lot less code...

    VB Code:
    1. Private Sub Form_Load()
    2.     Randomize Timer
    3.     Form1.Top = (Rnd * (Screen.Height - 1) + 1)
    4.     Form1.Left = (Rnd * (Screen.Width - 1) + 1)
    5. End Sub

    And if you want your form to move all around the place put the code in a timer...
    Your code makes the form load outside the screens view most the time BTW...

    VB Code:
    1. Private Sub Form_Load()
    2.     Randomize Timer
    3.     Form1.Top = (Rnd * (Screen.Height - 1) + 1)
    4.     Form1.Left = (Rnd * (Screen.Width - 1) + 1)
    5.    
    6.     Do While Me.Top > Screen.Height
    7.     Form1.Top = (Rnd * (Screen.Height - 1) + 1)
    8.     Loop
    9.     Do While Me.Left > Screen.Width
    10.     Form1.Left = (Rnd * (Screen.Width - 1) + 1)
    11.     Loop
    12. End Sub

    Would make it choose a new possition, until it is at least viewable all the time
    Wayne

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here is a streamlined version of the same concept.
    VB Code:
    1. Public Sub RandomPos(frm As Form)
    2.  
    3.     Dim dblLeftRange As Double
    4.     Dim dblTopRange As Double
    5.    
    6.     dblLeftRange = Screen.Width - frm.Width
    7.     dblTopRange = Screen.Height - frm.Height
    8.    
    9.     Randomize
    10.    
    11.     frm.Move Rnd * dblLeftRange, Rnd * dblTopRange
    12.     frm.Show
    13.  
    14. End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Devon, UK
    Posts
    311
    thanks all it works now, BTW i used MartinLiss's code. But thanks anywayz.
    Sam.

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