;****************************************************************************** ;* Script : RandomWall.au3 ;* ;* Description : Random Wallpaper Changer ;* ;* Language : Auto-It v3 - http://www.autoitscript.com/autoit3/ ;****************************************************************************** ;* Program History: ;* ;* Version : 1.0.0 ;* Author : Craig H. Rettig ;* Date : 29 Jul 2008 ;* Notes : Initial release ;****************************************************************************** ;* This program is free software. It comes without any warranty, to ;* the extent permitted by applicable law. You can redistribute it ;* and/or modify it under the terms of the Do What The F*** You Want ;* To Public License, Version 2, as published by Sam Hocevar. See ;* http://sam.zoy.org/wtfpl/COPYING for more details. ;****************************************************************************** #Include #Include Opt('MustDeclareVars', 1) Dim Const $SPI_SETDESKWALLPAPER = 20; Dim Const $SPIF_UPDATEINIFILE = 0x01; Dim Const $SPIF_SENDWININICHANGE = 0x02; Dim Const $REGISTRY_PATH = "HKEY_CURRENT_USER\Control Panel\Desktop" _Main() Func _Main() Local $FileList, $ImageDir, $RndNum, $BMPfile, $hImage, $iX, $iY, $hClone, $err, $ImgFilter If $CmdLine[0] > 0 Then $ImageDir = $CmdLine[1] Else $ImageDir = @ScriptDir EndIf $ImgFilter = "(?-i)\.(jpg|png|bmp)" $FileList = RecursiveFileSearch($ImageDir, $ImgFilter, ".", 1) If IsArray($FileList) And UBound($FileList) > 1 Then $RndNum = Random(1, $FileList[0], 1) ; Convert image to .BMP $BMPfile = @SystemDir & "\" & StringStripWS(@UserName, 8) & "_RandomWall.bmp" _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($FileList[$RndNum]) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) $hClone = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iX, $iY, $GDIP_PXF24RGB) _GDIPlus_ImageSaveToFile($hClone, $BMPfile) _GDIPlus_ImageDispose($hClone) _GDIPlus_ImageDispose($hImage) _GDIPlus_ShutDown() If FileExists($BMPfile) Then If ($iX = @DesktopWidth) Or ($iY = @DesktopHeight) Then ; Update the wallpaper style in the Registry (Centered) $err = RegWrite($REGISTRY_PATH, "WallpaperStyle", "REG_SZ", "1") $err += RegWrite($REGISTRY_PATH, "TileWallpaper", "REG_SZ", "0") ElseIf ($iX * $iY) <= ((@DesktopWidth * @DesktopHeight) / 2) Then ; Update the wallpaper style in the Registry (Tiled) $err = RegWrite($REGISTRY_PATH, "WallpaperStyle", "REG_SZ", "1") $err += RegWrite($REGISTRY_PATH, "TileWallpaper", "REG_SZ", "1") Else ; Update the wallpaper style in the Registry (Stretched) $err = RegWrite($REGISTRY_PATH, "WallpaperStyle", "REG_SZ", "2") $err += RegWrite($REGISTRY_PATH, "TileWallpaper", "REG_SZ", "0") EndIf If $err <> 2 Then MsgBox(4096, "Registery Error", "There was an error writing to the Registry.") Else ; Make the .DLL calls to change the image $err = DllCall("User32.dll", "int", "SystemParametersInfo", "int", $SPI_SETDESKWALLPAPER, "int", 0, "string", $BMPfile, "int", $SPIF_UPDATEINIFILE) If @Error <> 0 Then MsgBox(4096, ".DLL Error", "There was an error making the .DLL call: " & $SPIF_UPDATEINIFILE & @CR & "Error Code: " & @Error) EndIf $err = DllCall("User32.dll", "int", "SystemParametersInfo", "int", $SPI_SETDESKWALLPAPER, "int", 0, "string", $BMPfile, "int", $SPIF_SENDWININICHANGE) If @Error <> 0 Then MsgBox(4096, ".DLL Error", "There was an error making the .DLL call: " & $SPIF_SENDWININICHANGE & @CR & "Error Code: " & @Error) EndIf EndIf Else MsgBox(4096, "File Missing", $BMPfile & " was not found.") EndIf Else MsgBox(4096, "No image files found", "There were no .JPG, .PNG, or .BMP files found in the " & Chr(34) & $ImageDir & Chr(34) & " directory.") EndIf EndFunc ; #cs ---------------------------------------------------------------------------- ; AutoIt Version: 3.2.10.0 ; Author: WeaponX ; Updated: 2/21/08 ; Script Function: Recursive file search ; ; 2/21/08 - Added pattern for folder matching, flag for return type ; 1/24/08 - Recursion is now optional ; ; Parameters: ; ; RFSstartdir: Path to starting folder ; ; RFSFilepattern: RegEx pattern to match ; "\.(mp3)" - Find all mp3 files - case sensitive (by default) ; "(?i)\.(mp3)" - Find all mp3 files - case insensitive ; "(?-i)\.(mp3|txt)" - Find all mp3 and txt files - case sensitive ; ; RFSFolderpattern: ; "(Music|Movies)" - Only match folders named Music or Movies - case sensitive (by default) ; "(?i)(Music|Movies)" - Only match folders named Music or Movies - case insensitive ; "(?!(Music|Movies)\b)\b.+" - Match folders NOT named Music or Movies - case sensitive (by default) ; ; RFSFlag: Specifies what is returned in the array ; 0 - Files and folders ; 1 - Files only ; 2 - Folders only ; ; RFSrecurse: TRUE = Recursive, FALSE = Non-recursive ; ; RFSdepth: Internal use only ; ; #ce ---------------------------------------------------------------------------- Func RecursiveFileSearch($RFSstartDir, $RFSFilepattern = ".", $RFSFolderpattern = ".", $RFSFlag = 0, $RFSrecurse = true, $RFSdepth = 0) Local $RFSfilecount, $RFSsearch, $RFSnext ;Ensure starting folder has a trailing slash If StringRight($RFSstartDir, 1) <> "\" Then $RFSstartDir &= "\" If $RFSdepth = 0 Then ;Get count of all files in subfolders for initial array definition $RFSfilecount = DirGetSize($RFSstartDir, 1) ;File count + folder count (will be resized when the function returns) Global $RFSarray[$RFSfilecount[1] + $RFSfilecount[2] + 1] EndIf $RFSsearch = FileFindFirstFile($RFSstartDir & "*.*") If @Error Then Return ;Search through all files and folders in directory While 1 $RFSnext = FileFindNextFile($RFSsearch) If @Error Then ExitLoop ;If folder and recurse flag is set and regex matches If StringInStr(FileGetAttrib($RFSstartDir & $RFSnext), "D") Then If $RFSrecurse AND StringRegExp($RFSnext, $RFSFolderpattern, 0) Then RecursiveFileSearch($RFSstartDir & $RFSnext, $RFSFilepattern, $RFSFolderpattern, $RFSFlag, $RFSrecurse, $RFSdepth + 1) If $RFSFlag <> 1 Then ;Append folder name to array $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext $RFSarray[0] += 1 EndIf EndIf ElseIf StringRegExp($RFSnext, $RFSFilepattern, 0) AND $RFSFlag <> 2 Then ;Append file name to array $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext $RFSarray[0] += 1 EndIf WEnd FileClose($RFSsearch) If $RFSdepth = 0 Then Redim $RFSarray[$RFSarray[0] + 1] Return $RFSarray EndIf EndFunc ;==>RecursiveFileSearch