SetRandomWallpaper

  • Dieses Mini Script Sucht alle Bilder im Scriptverzeichnis eigestellten Verzeichnis und setzt zufällig Eines als neuen Desktophintergrund (Ja ich weiß, dass das nichts besonderes ist). :D
    Unterstützte Dateiformate: jpg; png; bmp; gif

    Spoiler anzeigen
    [autoit]

    #include <File.au3>
    #include <Array.au3>
    #include <GDIPlus.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("TrayMenuMode", 1)
    Opt("TrayOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    Global $sPicFolder = IniRead("Settings.ini", "Paths", "PicPath", @ScriptDir)

    [/autoit] [autoit][/autoit] [autoit]

    Global $aExtPics[6]
    $aExtPics[0] = 5

    [/autoit] [autoit][/autoit] [autoit]

    $aExtPics[1] = 'jpeg'
    $aExtPics[2] = 'jpg'
    $aExtPics[3] = 'png'
    $aExtPics[4] = 'bmp'
    $aExtPics[5] = 'gif'

    [/autoit] [autoit][/autoit] [autoit]

    $sTempFilePath = @TempDir & "\+~RandomWallPaper~+.bmp"
    $sBackUpFilePath = @TempDir & "\+~RandomWallPaperBackUp~+.bmp"

    [/autoit] [autoit][/autoit] [autoit]

    $cRandomWallpaper = TrayCreateItem("Zufälliges WallPaper")
    $cUndo = TrayCreateItem("Letztes WallPaper wiederherstellen")
    $cPicPath = TrayCreateItem("Bilderpfad auswählen")
    $cExit = TrayCreateItem("Beenden")

    [/autoit] [autoit][/autoit] [autoit]

    TrayItemSetOnEvent($cRandomWallpaper, "_SetRandomWallPaper")
    TrayItemSetOnEvent($cPicPath, "_PicPath")
    TrayItemSetOnEvent($cUndo, "_Undo")
    TrayItemSetOnEvent($cExit, "_Exit")

    [/autoit] [autoit][/autoit] [autoit]

    $aPics = _FileListToArrayEx($sPicFolder, $aExtPics)
    If $aPics = 0 Then _PathInValid()

    [/autoit] [autoit][/autoit] [autoit]

    While Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _SetRandomWallPaper()
    TrayItemSetState($cRandomWallpaper, 4)
    If FileExists($sTempFilePath) Then FileCopy($sTempFilePath, $sBackUpFilePath, 1)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    $hBitmapWallPaper = _GDIPlus_BitmapCreateFromFile($sPicFolder & "\" & $aPics[Random(1, $aPics[0], 1)])
    _GDIPlus_ImageSaveToFile($hBitmapWallPaper, $sTempFilePath)
    _GDIPlus_BitmapDispose($hBitmapWallPaper)
    _GDIPlus_Shutdown()

    [/autoit] [autoit][/autoit] [autoit]

    _SetWallPaper($sTempFilePath)
    EndFunc ;==>_SetRandomWallPaper

    [/autoit] [autoit][/autoit] [autoit]

    Func _PicPath()
    TrayItemSetState($cPicPath, 4)
    $sPicFolderTmp = FileSelectFolder("Dateipfad auswählen", "", 7, $sPicFolder)
    If Not @error And IsArray(_FileListToArrayEx($sPicFolderTmp, $aExtPics)) Then
    If StringRight($sPicFolderTmp, 1) = "\" Then $sPicFolderTmp = StringTrimRight($sPicFolderTmp, 1)
    $sPicFolder = $sPicFolderTmp
    $aPics = _FileListToArrayEx($sPicFolder, $aExtPics)
    IniWrite("Settings.ini", "Paths", "PicPath", $sPicFolder)
    Return 1
    EndIf
    Return 0
    EndFunc ;==>_PicPath

    [/autoit] [autoit][/autoit] [autoit]

    Func _PathInValid()
    Switch MsgBox(52, "Fehler", "Im angegebenen Pfad wurden keine kompatiblen Bilddateien gefunden oder der Pfad ist ungültig. Möchten sie den Pfad jetzt neu bestimmen?")
    Case 6
    If Not _PicPath() Then _PathInValid()
    Case 7
    MsgBox(64, "Info", "Da keine Bilder gefunden wurden sind einige Funktionen des Programms deaktiviert, wenn sie einen neuen Pfad zu ihren Bildern auswählen möchten klicken sie auf 'Bilderpfad auswählen'.")
    TrayItemSetState($cRandomWallpaper, 128)
    EndSwitch
    EndFunc ;==>_PathInValid

    [/autoit] [autoit][/autoit] [autoit]

    Func _FileListToArrayEx($sPath, $aExt)
    If Not IsArray($aExt) Then Return SetError(1, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $aFilesRaw = _FileListToArray($sPath, '*', 1)
    Local $aFilesReturn[1]

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To $aFilesRaw[0]
    $sExtTmp = StringRight($aFilesRaw[$i], StringLen($aFilesRaw[$i]) - StringInStr($aFilesRaw[$i], ".", 0, -1))
    $bValid = False
    For $j = 1 To $aExt[0]
    If $sExtTmp = $aExt[$j] Then $bValid = True
    Next
    If $bValid = True Then _ArrayAdd($aFilesReturn, $aFilesRaw[$i])
    Next
    If UBound($aFilesReturn) <= 1 Then Return SetError(2, 0, 0)
    $aFilesReturn[0] = UBound($aFilesReturn) - 1
    Return $aFilesReturn
    EndFunc ;==>_FileListToArrayEx

    [/autoit] [autoit][/autoit] [autoit]

    Func _Undo()
    TrayItemSetState($cUndo, 4)
    If FileExists($sBackUpFilePath) Then
    FileCopy($sTempFilePath, $sTempFilePath & '.copybuffer', 1)
    FileCopy($sBackUpFilePath, $sTempFilePath, 1)
    FileCopy($sTempFilePath & '.copybuffer', $sBackUpFilePath, 1)
    FileDelete($sTempFilePath & '.copybuffer')
    _SetWallPaper($sTempFilePath)
    EndIf
    EndFunc ;==>_Undo

    [/autoit] [autoit][/autoit] [autoit]

    Func _SetWallPaper($sImagePath)
    RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "TileWallPaper", "REG_SZ", 0)
    RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "WallpaperStyle", "REG_SZ", -1)
    RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", "REG_SZ", $sImagePath)
    DllCall("user32.dll", "int", "SystemParametersInfo", "int", 20, "int", 0, "str", $sImagePath, "int", BitOR(1, 2))
    EndFunc ;==>_SetWallPaper

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    Exit
    EndFunc ;==>_Exit

    [/autoit]


    Edit: Großes Update :thumbup: !

    Änderungen:
    - TrayMenu hinzugefügt
    - Einstellungen hinzugefügt
    - Bessere Überprüfung des Bilderpfads
    - Undo Funktion
    - Kleinere Bugs behoben

  • Es wäre wirklich schön wenn das wallpaper am anfang ausgelesen wird und beim beenden wieder das alte wieder kommt