How do i make my app startup in a random window position?
Printable View
How do i make my app startup in a random window position?
VB Code:
Private Sub Form_Load() RandomFormPos Me End Sub Public Sub RandomFormPos(frm As Form) Dim scrW As Integer, scrH As Integer, _ newTop As Integer, newLeft As Integer Let scrH = Screen.Height Let scrW = Screen.Width Do Randomize newTop = Rnd * scrH Loop Until Not newTop + frm.Height > scrH Do Randomize newLeft = Rnd * scrW Loop Until Not newLeft + frm.Width > scrW frm.Top = newTop frm.Left = newLeft End Sub
Is this what you wanted?
Jason
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.
Uh, will this not work??? A lot less code...
VB Code:
Private Sub Form_Load() Randomize Timer Form1.Top = (Rnd * (Screen.Height - 1) + 1) Form1.Left = (Rnd * (Screen.Width - 1) + 1) 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...Quote:
Originally posted by Alien_poo
Uh, will this not work??? A lot less code...
VB Code:
Private Sub Form_Load() Randomize Timer Form1.Top = (Rnd * (Screen.Height - 1) + 1) Form1.Left = (Rnd * (Screen.Width - 1) + 1) End Sub
And if you want your form to move all around the place put the code in a timer...
VB Code:
Private Sub Form_Load() Randomize Timer Form1.Top = (Rnd * (Screen.Height - 1) + 1) Form1.Left = (Rnd * (Screen.Width - 1) + 1) Do While Me.Top > Screen.Height Form1.Top = (Rnd * (Screen.Height - 1) + 1) Loop Do While Me.Left > Screen.Width Form1.Left = (Rnd * (Screen.Width - 1) + 1) Loop End Sub
Would make it choose a new possition, until it is at least viewable all the time :)
Here is a streamlined version of the same concept.
VB Code:
Public Sub RandomPos(frm As Form) Dim dblLeftRange As Double Dim dblTopRange As Double dblLeftRange = Screen.Width - frm.Width dblTopRange = Screen.Height - frm.Height Randomize frm.Move Rnd * dblLeftRange, Rnd * dblTopRange frm.Show End Sub
thanks all it works now, BTW i used MartinLiss's code. But thanks anywayz.