PDA

View Full Version : Dynamic desktop background changer



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.

ErosOlmi
13-11-2010, 09:12
Hi Johannes and welcome to thinBasic community. You did a great entrance :)
Hope you will enjoy to stay here exchanging ideas and programming fun.

Ciao
Eros

Michael Clease
14-11-2010, 03:56
Hello Johannes


Welcome to thinBasic and I hope you have some more interesting scripts for us in the future.

You can make executable with thinBasic just look at the script menu and select Bundle.

Regards

Mike

Johannes
14-11-2010, 22:33
Hello Mike,

It's funny that the free programs and utilities invariably have more options and are more stable than the stuff you have to pay for. All of you here are creating a big dilemma for me though: in a while there will be no real reason for me to keep programming on my ARM machine. :(

Well, there's always the fact that I find ARM assembler more elegant and logical than x86 assembler. :D

Thanks for that tip. I'll keep my setup as it is now (simpler to change the program if I want to) but it may come in handy if I want/have to write something for somebody else.