Jump to content
Sign in to follow this  
ezzetabi

Randomize Wallpaper.

Recommended Posts

Larry said that my ChangeWallpaper func can't work with Active destop actived.

I can't test, but Larry words are true 99% of times. So you are warned.

AutoItSetOption("MustDeclareVars", 1)
If WinExists('Wallpaper randomizer') Then WinClose('Wallpaper randomizer')
AutoItWinSetTitle('Wallpaper randomizer')


Dim $sFolder, $iMin, $iRtime, $aFiles, $bRandom, $c

If FileExists(@UserName & '.ini') Then
   $sFolder = IniRead(@UserName & '.ini', 'Settings', 'Folder', '')
   $iMin = IniRead(@UserName & '.ini', 'Settings', 'Min', '')
   $iRtime = IniRead(@UserName & '.ini', 'Settings', 'RTime', '')
   $bRandom = IniRead(@UserName & '.ini', 'Settings', 'Random', '')
   
Else
   $sFolder = FileSelectFolder('Select the folder where seek the .bmp files', @HomeDrive)
   If @error Then Exit
   
   If StringRight($sFolder, 1) <> '\' Then $sFolder = $sFolder & '\'
   $iMin = InputBox('Question', 'Set minimal time, in minutes, you want to wait to change wallpaper', 3)
   If @error Then Exit
   
   Do
      $iRtime = InputBox('Question', 'Set max time, in minutes, you want to wait to change wallpaper', 3) - $iMin
      If @error Then Exit
      If $iRtime >= 0 Then ExitLoop
      MsgBox(4096 + 16, 'Error', 'Min time must be less than max time')
   Until 0
   
   $bRandom = MsgBox(4096 + 4 + 32, 'Question', 'Do you want to change wallpapers in a random order?')
  ;$bRandom is for 6 yes and for 7 no
   
   IniWrite(@UserName & '.ini', 'Settings', 'Folder', $sFolder)
   IniWrite(@UserName & '.ini', 'Settings', 'Min', $iMin)
   IniWrite(@UserName & '.ini', 'Settings', 'RTime', $iRtime)
   IniWrite(@UserName & '.ini', 'Settings', 'Random', $bRandom)
EndIf

$aFiles = _FileSearch($sFolder & '*.bmp', 1)
If $aFiles[0] <= 1 Then
   MsgBox(4096 + 16, 'Error', 'Not enough .bmp files found' & @LF & 'To change wallpaper you need at least two pictures.')
   Exit
EndIf

$c = 0
While 1
   If $bRandom = 7 Then
      $c = $c + 1
      If $c > $aFiles[0] Then $c = 1
   Else
      $c = Int(Random(1, $aFiles[0] + 1))
   EndIf
   
   _ChangeWallpaper($aFiles[$c], 2)
   Sleep($iMin * 60000 + Int(Random(0, $iRtime + 1)) * 60000)
   $aFiles = _FileSearch($sFolder & '*.bmp', 1)
   If $aFiles[0] <= 1 Then Exit
Wend

Exit
Func _ChangeWallpaper($sFile, $iType)
   If Not FileExists($sFile) Then
      SetError(1)
      Return -1
   EndIf
   If StringTrimLeft($sFile, StringInStr($sFile, '.', 0, -1)) <> 'bmp' Then
      SetError(2)
      Return -2
   EndIf
   Select
      Case $iType = 1
         RegWrite('HKCU\Control Panel\Desktop', 'TileWallpaper', 'reg_sz', '1')
         RegWrite('HKCU\Control Panel\Desktop', 'WallpaperStyle', 'reg_sz', '0')
      Case $iType = 2
         RegWrite('HKCU\Control Panel\Desktop', 'TileWallpaper', 'reg_sz', '0')
         RegWrite('HKCU\Control Panel\Desktop', 'WallpaperStyle', 'reg_sz', '0')
      Case $iType = 3
         RegWrite('HKCU\Control Panel\Desktop', 'TileWallpaper', 'reg_sz', '0')
         RegWrite('HKCU\Control Panel\Desktop', 'WallpaperStyle', 'reg_sz', '2')
      Case Else
        ;
   EndSelect
   RegWrite('HKCU\Control Panel\Desktop', 'Wallpaper', 'reg_sz', $sFile)
   DllCall("user32", "int", "SystemParametersInfo", "int", 20, "int", 0, "str", $sFile, "int", 0)
   Return 0
EndFunc  ;==>_ChangeWallpaper

Func _FileSearch($sIstr, $bSF)
   Local $sInst, $bSF, $sCriteria, $sPath, $c, $sBuffer, $aFolders = '', $iH, $sCS, $sCF, $sCP, $sOutPut = '', $aNull[1]
   $sPath = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sPath = '' Then $sPath = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   $sBuffer = $sPath & '*.*' & @LF
   If $bSF = 1 Then
      Do 
         $sCS = StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1)
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1))
         $iH = FileFindFirstFile($sCS)
         While $iH <> - 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $aFolders = $aFolders & $sCP & $sCF & '\' & @LF 
               $sBuffer = $sBuffer & $sCP & $sCF & '\*.*' & @LF 
            EndIf
         Wend
         $sBuffer = StringReplace($sBuffer, $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   $aFolders = StringSplit($sPath & @LF & $aFolders, @LF)
  ;Now we have all folders where look for.
   For $c = 1 To $aFolders[0]
      If $aFolders[$c] = '' Then ContinueLoop
      $iH = FileFindFirstFile($aFolders[$c] & $sCriteria)
      While $iH <> - 1
         $sCF = FileFindNextFile($iH)
         If @error Then
            FileClose($iH)
            ExitLoop
         EndIf
         If $sCF = '.' Or $sCF = '..' Then ContinueLoop
         $sOutPut = $sOutPut & $aFolders[$c] & $sCF & @LF
      Wend
   Next
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull[0]
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch
Edited by ezzetabi

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...