Johannes
13-11-2010, 03:03
This was the reason I downloaded thinBasic. I have a large number of desktop backgrounds and I wanted to change the background dynamically, without restarting Windows, logging off/logging on, or using the desktop properties dialog. Every single "solution" I found using Google involved restarting Windows or logging off and logging on again. Windows can do it so I knew I should be able to do it myself. The first thinBasic program I wrote! With comments! ;)
'----------------------------------------------------------------------------
' !Background
'
' Choose a random background for the desktop.
'----------------------------------------------------------------------------
Uses "FILE"
Const d As String = APP_SourcePath+"!Backgrounds.txt"
Const e As String = CRLF
Dim a,b,i,n,q As Long
Dim f(),l,m As String
' Define API function.
Declare Function x Lib "User32.dll" Alias "SystemParametersInfoA" (ByVal A As Integer, ByVal B As Integer, ByVal C As String, ByVal D As Integer) As Integer
' Check if name file is present.
If FILE_Exists(d) Then
' File is already present. Load file and remove.
l = FILE_Load(d)
q = FILE_Kill(d)
Else
' File is not yet present. Create file.
' Read names of available backgrounds.
n = DIR_ListArray(f,APP_SourcePath,"*.jpg")
' Initialise random generator.
For i=1 To n
q += Len(f(i))
Next n
a = Rnd(-Timer-q)
' Swap names.
' A single swap takes approximately 1.3 microseconds.
For i=1 To 250000
a = Rndf(1,n,0)
b = Rndf(1,n,0)
Swap f(a),f(b)
Next i
' Create name file.
For i=1 To n
l += f(i)+e
Next i
EndIf
' Pick first name in list file.
a = InStr(l,e)
m = APP_SourcePath+LEFT$(l,a-1)
l = Mid$(l,a+Len(e))
If l<>"" Then
q = FILE_Save(d,l)
EndIf
' Call Windows API to dynamically change the desktop background.
q = x(20, 0, m, 3)
And of course Windows Scheduler didn't want to run a thinBasic script, so I had to make this Visual Basic Script file as well and schedule that.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\...<where the thinBasic program is>...\!Background.tbasic" & Chr(34), 0
Set WshShell = Nothing
I named the thinBasic program !Background (a holdover from my Acorn days, which aren't quite over yet) and if you don't want to, change the name in the .VBS accordingly.
If you place your background JPGs in the same directory as the thinBasic program and schedule the .VBS in Windows Scheduler (11am and 11pm for me) everything should work fine. Double clicking either the .VBS or the thinBasic program will also change the desktop background immediately.
'----------------------------------------------------------------------------
' !Background
'
' Choose a random background for the desktop.
'----------------------------------------------------------------------------
Uses "FILE"
Const d As String = APP_SourcePath+"!Backgrounds.txt"
Const e As String = CRLF
Dim a,b,i,n,q As Long
Dim f(),l,m As String
' Define API function.
Declare Function x Lib "User32.dll" Alias "SystemParametersInfoA" (ByVal A As Integer, ByVal B As Integer, ByVal C As String, ByVal D As Integer) As Integer
' Check if name file is present.
If FILE_Exists(d) Then
' File is already present. Load file and remove.
l = FILE_Load(d)
q = FILE_Kill(d)
Else
' File is not yet present. Create file.
' Read names of available backgrounds.
n = DIR_ListArray(f,APP_SourcePath,"*.jpg")
' Initialise random generator.
For i=1 To n
q += Len(f(i))
Next n
a = Rnd(-Timer-q)
' Swap names.
' A single swap takes approximately 1.3 microseconds.
For i=1 To 250000
a = Rndf(1,n,0)
b = Rndf(1,n,0)
Swap f(a),f(b)
Next i
' Create name file.
For i=1 To n
l += f(i)+e
Next i
EndIf
' Pick first name in list file.
a = InStr(l,e)
m = APP_SourcePath+LEFT$(l,a-1)
l = Mid$(l,a+Len(e))
If l<>"" Then
q = FILE_Save(d,l)
EndIf
' Call Windows API to dynamically change the desktop background.
q = x(20, 0, m, 3)
And of course Windows Scheduler didn't want to run a thinBasic script, so I had to make this Visual Basic Script file as well and schedule that.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\...<where the thinBasic program is>...\!Background.tbasic" & Chr(34), 0
Set WshShell = Nothing
I named the thinBasic program !Background (a holdover from my Acorn days, which aren't quite over yet) and if you don't want to, change the name in the .VBS accordingly.
If you place your background JPGs in the same directory as the thinBasic program and schedule the .VBS in Windows Scheduler (11am and 11pm for me) everything should work fine. Double clicking either the .VBS or the thinBasic program will also change the desktop background immediately.