找回密码
 加入
搜索
查看: 1327|回复: 10

关于获取当前文件夹图片的问题

[复制链接]
发表于 2009-8-18 11:57:17 | 显示全部楼层 |阅读模式
本帖最后由 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] = 0 Then
        $strPathToLoad = @MyDocumentsDir & "\My Pictures\"
Else
        $strPathToLoad = $CmdLine[1]
        $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 & " [Folder1] [Folder2] ... [FolderN]" & @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[0]
                        $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[0]
                $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[0] > 1 Then
                $i = Random(1, $arrayName[0], 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
发表于 2009-8-18 12:12:08 | 显示全部楼层
他这个是命令行操作的,你可以改成gui操作的可能直观点
 楼主| 发表于 2009-8-18 12:12:43 | 显示全部楼层
请楼上大侠帮忙改下
发表于 2009-8-18 12:21:25 | 显示全部楼层
这个。。。我是小虾,太耗体力值啊
 楼主| 发表于 2009-8-18 12:35:55 | 显示全部楼层
发表于 2009-8-18 12:51:43 | 显示全部楼层
本帖最后由 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
 楼主| 发表于 2009-8-18 16:12:49 | 显示全部楼层
不希望加配置文件,希望单文件完成
发表于 2009-8-18 16:17:21 | 显示全部楼层
那你是想固定文件夹路径及间隔时间?那不更容易
 楼主| 发表于 2009-8-18 16:49:31 | 显示全部楼层
最终如下:
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] = 0 Then
        $strPathToLoad = ".\"
Else
        $strPathToLoad = $CmdLine[1]
        $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 & " [Folder1] [Folder2] ... [FolderN]" & @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[0]
                        $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[0]
                $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[0] > 1 Then
                $i = Random(1, $arrayName[0], 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
发表于 2009-8-18 17:50:03 | 显示全部楼层
本帖最后由 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[Random(1, $lb[0], 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   ;==>ghbz
LZ 若是要当前文件夹的话 修改第3行为 $wjj = @ScriptDir 即可
发表于 2009-8-19 21:59:41 | 显示全部楼层
10楼代码不错,收藏学习!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-17 18:34 , Processed in 0.075053 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表