关于获取当前文件夹图片的问题
本帖最后由 a000000 于 2009-8-18 16:49 编辑今天得到一更换壁纸的源码,如何修改成自动获取当前文件夹的图片?请大侠赐教:
源码如下:
AutoIt Version: 3.2.12.1
Author: Zhangzq
Script Function:
Set Desktop Wallpaper Randomly.
#ce
; Script Start - Add your code below here
AutoItSetOption("MustDeclareVars", 1)
#include<GDIPlus.au3>
#include<File.au3>
#include<Misc.au3>
Const $AppTitle = "Wallpaper Randomly"
_Singleton($AppTitle)
Dim $strPathToLoad, $strPathToSave
Dim $strOption, $i
Dim $strName, $strWallpaper
If $CmdLine = 0 Then
$strPathToLoad = @MyDocumentsDir & "\My Pictures\"
Else
$strPathToLoad = $CmdLine
$strOption = StringLower($strPathToLoad)
If $strOption = "-h" Or $strOption = "/h" Then
$strName = @ScriptName
If StringRight(StringLower($strName), 4) = ".au3" Then $strName = "autoit3.exe " & $strName
MsgBox(64, $AppTitle, _
"Usage: " & $strName & " ... " & @CRLF & @CRLF & _
"Set Desktop Wallpaper randomly with Pictures in Folder(s)" & @CRLF & _
"Or My Pictures if no Folder Specified")
Exit
Else
For $i = 2 to $CmdLine
$strPathToLoad = $strPathToLoad & "|" & $CmdLine[$i]
Next
EndIf
EndIf
$strPathToSave = @UserProfileDir & "\Local Settings\Application Data\Microsoft\"
If _GetRandomPicture($strPathToLoad, $strName) Then Exit
If StringLower(StringRight($strName, 4)) = ".jpg" Then
$strWallpaper = _TempFile(@TempDir, "~wpr_", ".bmp")
_JpgToBmp($strName, $strWallpaper)
FileMove($strWallpaper, $strPathToSave & "Wallpaper1.bmp", 1)
$strWallpaper = $strPathToSave & "Wallpaper1.bmp"
Else
$strWallpaper = $strPathToSave & "Wallpaper1.bmp"
FileCopy($strName, $strWallpaper, 1)
EndIf
DllCall("user32","int","SystemParametersInfo","int",20,"int",0,"str",$strWallpaper,"int",0)
RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "ConvertedWallpaper", "REG_SZ", $strName)
RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", "REG_SZ", $strWallpaper)
Func _GetRandomPicture($strWhere, ByRef $strGot)
Local $arrayWhere, $search, $arrayName, $strPath, $strFind, $strTemp, $i
$strGot = ""
$strTemp = ""
$arrayWhere = StringSplit($strWhere, "|")
For $i = 1 To $arrayWhere
$strPath = $arrayWhere[$i]
If StringRight($strPath, 1) <> "\" Then $strPath = $strPath & "\"
$search = FileFindFirstFile($strPath & "*.*")
If $search <> -1 Then
While 1
$strFind = FileFindNextFile($search)
If @error Then ExitLoop
If StringInStr($strFind, ".jpg", 2) Or StringInStr($strFind, ".bmp", 2) Then
$strTemp = $strTemp & "|" & $strPath & $strFind
EndIf
WEnd
EndIf
Next
If $strTemp = "" Then Return -1
$strTemp = StringTrimLeft($strTemp, 1)
$arrayName = StringSplit($strTemp, "|")
If $arrayName > 1 Then
$i = Random(1, $arrayName, 1)
$strTemp = $arrayName[$i]
EndIf
$strGot = $strTemp
Return 0
EndFunc
Func _JpgToBmp($Jpg,$Bmp)
Local $hImage, $CLSID
_GDIPlus_StartUp()
$hImage = _GDIPlus_ImageLoadFromFile($Jpg)
$CLSID = _GDIPlus_EncodersGetCLSID("BMP")
_GDIPlus_ImageSaveToFileEx($hImage,$Bmp, $CLSID)
_GDIPlus_ShutDown()
EndFunc 他这个是命令行操作的,你可以改成gui操作的可能直观点 请楼上大侠帮忙改下 这个。。。我是小虾,太耗体力值啊 :face (27): 本帖最后由 afan 于 2009-8-18 12:54 编辑
其实也不用Gui,你可以试试配置个ini:
_FileListToArray 将ini里面的某文件夹里的文件读入数组;
While 1
读取ini设定的间隔时间,时间一定就 随机获取一个数组里的文件;
将文件[可选:判断是否为jpg,是就 _JpgToBmp 转换为bmp]复制到 @UserProfileDir & "\Local Settings\Application Data\Microsoft\";
之后dllcall+RegWrite搞定;
Wend 不希望加配置文件,希望单文件完成 那你是想固定文件夹路径及间隔时间?那不更容易 最终如下:
AutoIt Version: 3.2.12.1
Author: Zhangzq
Script Function:
Set Desktop Wallpaper Randomly.
#ce
; Script Start - Add your code below here
AutoItSetOption("MustDeclareVars", 1)
#include<GDIPlus.au3>
#include<File.au3>
#include<Misc.au3>
Const $AppTitle = "Wallpaper Randomly"
_Singleton($AppTitle)
Dim $strPathToLoad, $strPathToSave
Dim $strOption, $i
Dim $strName, $strWallpaper
If $CmdLine = 0 Then
$strPathToLoad = ".\"
Else
$strPathToLoad = $CmdLine
$strOption = StringLower($strPathToLoad)
If $strOption = "-h" Or $strOption = "/h" Then
$strName = @ScriptName
If StringRight(StringLower($strName), 4) = ".au3" Then $strName = "autoit3.exe " & $strName
MsgBox(64, $AppTitle, _
"Usage: " & $strName & " ... " & @CRLF & @CRLF & _
"Set Desktop Wallpaper randomly with Pictures in Folder(s)" & @CRLF & _
"Or My Pictures if no Folder Specified")
Exit
Else
For $i = 2 to $CmdLine
$strPathToLoad = $strPathToLoad & "|" & $CmdLine[$i]
Next
EndIf
EndIf
$strPathToSave = @UserProfileDir & "\Local Settings\Application Data\Microsoft\"
If _GetRandomPicture($strPathToLoad, $strName) Then Exit
If StringLower(StringRight($strName, 4)) = ".jpg" Then
$strWallpaper = _TempFile(@TempDir, "~wpr_", ".bmp")
_JpgToBmp($strName, $strWallpaper)
FileMove($strWallpaper, $strPathToSave & "Wallpaper1.bmp", 1)
$strWallpaper = $strPathToSave & "Wallpaper1.bmp"
Else
$strWallpaper = $strPathToSave & "Wallpaper1.bmp"
FileCopy($strName, $strWallpaper, 1)
EndIf
DllCall("user32","int","SystemParametersInfo","int",20,"int",0,"str",$strWallpaper,"int",0)
RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "ConvertedWallpaper", "REG_SZ", $strName)
RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", "REG_SZ", $strWallpaper)
Func _GetRandomPicture($strWhere, ByRef $strGot)
Local $arrayWhere, $search, $arrayName, $strPath, $strFind, $strTemp, $i
$strGot = ""
$strTemp = ""
$arrayWhere = StringSplit($strWhere, "|")
For $i = 1 To $arrayWhere
$strPath = $arrayWhere[$i]
If StringRight($strPath, 1) <> "\" Then $strPath = $strPath & "\"
$search = FileFindFirstFile($strPath & "*.*")
If $search <> -1 Then
While 1
$strFind = FileFindNextFile($search)
If @error Then ExitLoop
If StringInStr($strFind, ".jpg", 2) Or StringInStr($strFind, ".bmp", 2) Then
$strTemp = $strTemp & "|" & $strPath & $strFind
EndIf
WEnd
EndIf
Next
If $strTemp = "" Then Return -1
$strTemp = StringTrimLeft($strTemp, 1)
$arrayName = StringSplit($strTemp, "|")
If $arrayName > 1 Then
$i = Random(1, $arrayName, 1)
$strTemp = $arrayName[$i]
EndIf
$strGot = $strTemp
Return 0
EndFunc
Func _JpgToBmp($Jpg,$Bmp)
Local $hImage, $CLSID
_GDIPlus_StartUp()
$hImage = _GDIPlus_ImageLoadFromFile($Jpg)
$CLSID = _GDIPlus_EncodersGetCLSID("BMP")
_GDIPlus_ImageSaveToFileEx($hImage,$Bmp, $CLSID)
_GDIPlus_ShutDown()
EndFunc 本帖最后由 afan 于 2009-8-18 17:54 编辑
原来LZ只是想更改为当前目录,没看清标题,晕。。。
做了个10秒随机更换一次桌面的,图片文件夹及间隔时间修改前3、4行就行了
#include <File.au3>
#include <GDIPlus.au3>
$wjj = @MyDocumentsDir & "\My Pictures" ;需要调用图片的文件夹
$jgsj = 10 ; 间隔时间
$bmp = @UserProfileDir & "\Local Settings\zmbz.bmp"
$lb = _FileListToArray($wjj, '*.jpg')
While 1
$sjwj = $wjj & '\' & $lb, 1)]
ToBmp($sjwj, $bmp)
ghbz()
Sleep($jgsj * 1000)
WEnd
Func ToBmp($pic, $bmp) ; 转换为 bmp 作者 Sanhen
Local $hImage, $CLSID
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($pic)
$CLSID = _GDIPlus_EncodersGetCLSID("BMP")
_GDIPlus_ImageSaveToFileEx($hImage, $bmp, $CLSID)
_GDIPlus_Shutdown()
EndFunc ;==>ToBmp
Func ghbz()
DllCall("user32", "int", "SystemParametersInfo", "int", 20, "int", 0, "str", $bmp, "int", 0)
RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", "REG_SZ", $bmp)
EndFunc ;==>ghbzLZ 若是要当前文件夹的话 修改第3行为 $wjj = @ScriptDir 即可 10楼代码不错,收藏学习!
页:
[1]