#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Version=Beta #AutoIt3Wrapper_Icon=LEMON.ICO #AutoIt3Wrapper_UseUpx=y #AutoIt3Wrapper_Res_Description=FetchWallpaperFromBing #AutoIt3Wrapper_Res_Fileversion=0.0.0.9 #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y #AutoIt3Wrapper_Res_LegalCopyright=Glory be to Bing #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include #include #include #include #include #include Global $autoChange, $acTimer Opt("TrayMenuMode", 3) main() Func main() ;Tray Settings AutoItSetOption("TrayAutoPause" , 0) ; Create a tray item with the radio item parameter selected. Local $idToday = TrayCreateItem("Today Wallpaper", -1, -1) Local $idLatest = TrayCreateItem("Latest 5", -1, -1) Local $idRandom = TrayCreateItem("Random Wallpaper", -1, -1) Local $ac = TrayCreateMenu("Auto Change", -1, -1) Local $acNone = TrayCreateItem("None", $ac, -1, 1) Local $ac1Min = TrayCreateItem("Every 1 MIN", $ac, -1, 1) Local $ac5Min = TrayCreateItem("Every 5 MINS", $ac, -1, 1) Local $ac15Min = TrayCreateItem("Every 15 MINS", $ac, -1, 1) Local $idBrowser = TrayCreateItem("Browser", -1, -1) TrayCreateItem("") ; Create a separator line. Local $idExit = TrayCreateItem("Exit") TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu. While 1 Switch TrayGetMsg() Case $idToday _getTodayWallpaper() Case $idLatest _getLatestWallpaper() Case $idRandom _rndWallpaper() Case $acNone $autochange = 0 Case $ac1Min _setChange(1) Case $ac5Min _setChange(5) Case $ac15Min _setChange(15) Case $idBrowser _opnFolder() Case $idExit ExitLoop Case Else _autoChange() EndSwitch WEnd EndFunc ;set interval Func _setChange($interval) If $autoChange = 0 Then $acTimer = TimerInit() EndIf $autoChange = $interval EndFunc Func _autoChange() If $autoChange > 0 Then $passTime = TimerDiff($acTimer) If $passTime >= $autoChange*60*1000 Then _rndWallpaper() $acTimer = TimerInit() EndIf EndIf EndFunc Func _opnFolder() ShellExecute(@ScriptDir) EndFunc Func _rndWallpaper() Local $flist = _FileListToArray(@ScriptDir, "*.jpg") If @error == 4 Or $flist[0] < 2 Then _getLatestWallpaper() Else Local $rnd = Random(1, $flist[0], 1) _ChangeWallpaper(_PathFull($flist[$rnd]), 3) EndIf EndFunc Func _getTodayWallpaper() ; Read the file without downloading to a folder. The option of 'get the file from the local cache' has been selected. Local $dData = InetRead("http://cn.bing.com") ; The number of bytes read is returned using the @extended macro. Local $iBytesRead = @extended ; Convert the ANSI compatible binary string back into a string. Local $sData = BinaryToString($dData) ; Find wallpaper url Local $url = StringRegExp($sData, "url:\s*'(.*?\.jpg)'", 1) If @error == 0 Then ;ConsoleWrite($url[0]) Local $fname = StringRegExpReplace($url[0], ".*/", "") If Not FileExists($fname) Then Local $pre = StringLeft($url[0], 4) If $pre == "http" Then InetGet($url[0], $fname) Else InetGet("http://www.bing.com" & $url[0], $fname) EndIf EndIf If FileGetSize($fname) > 10240 Then ;Set as wallpaper _ChangeWallpaper(_PathFull($fname), 3) EndIf Return 0 Else Return -1 EndIf ; Display the results. ; MsgBox($MB_SYSTEMMODAL, "", "The number of bytes read: " & $iBytesRead & @CRLF & @CRLF & $sData) EndFunc Func _getLatestWallpaper() ; get 5 latest wallpapers include today Local $dData = InetRead("http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=5") ; The number of bytes read is returned using the @extended macro. Local $iBytesRead = @extended ; Convert the ANSI compatible binary string back into a string. Local $sData = BinaryToString($dData) ; ConsoleWrite($sData) ; Find wallpaper url Local $url = StringRegExp($sData, """url"":\s*""(.*?\.jpg)""", 3) If @error == 0 Then Local $i, $fname, $pre For $i = 0 To UBound($url) - 1 ConsoleWrite($url[$i] & @LF) $fname = StringRegExpReplace($url[$i], ".*/", "") If Not FileExists($fname) Then $pre = StringLeft($url[$i], 4) If $pre == "http" Then InetGet($url[$i], $fname) Else InetGet("http://www.bing.com" & $url[$i], $fname) EndIf EndIf Next If FileGetSize($fname) > 10240 Then ;Set as wallpaper _ChangeWallpaper(_PathFull($fname), 3) EndIf Return 0 Else Return -1 EndIf ; Display the results. ; MsgBox($MB_SYSTEMMODAL, "", "The number of bytes read: " & $iBytesRead & @CRLF & @CRLF & $sData) EndFunc Func _ChangeWallpaper($sFile, $iType) If Not FileExists($sFile) Then SetError(1) Return -1 EndIf If StringTrimLeft($sFile, StringInStr($sFile, '.', 0, -1)) <> 'jpg' 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 ; Convert jpg to bmp If StringLower(StringRight($sFile,3)) = "jpg" Then _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($sFile) $sCLSID = _GDIPlus_EncodersGetCLSID("BMP") _GDIPlus_ImageSaveToFileEx($hImage, @ScriptDir & "\" & "bing" & ".bmp", $sCLSID) _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() $sFile = @ScriptDir & "\bing.bmp" EndIf RegWrite('HKCU\Control Panel\Desktop', 'Wallpaper', 'reg_sz', $sFile) DllCall("User32.dll", "int", "SystemParametersInfo", "int", 20, "int", 0, "str", $sFile, "int", 0) Return 0 EndFunc ;==>_ChangeWallpaper