AHK实现定时自动、手动、随机更换壁纸

定时自动、手动更换壁纸,这样的功能是我所需要的,相信也会有朋友需要。 这个脚本是我在国外论坛中发现的,改了一下功能用来分享。 注意:随机功能已经被我注释了,因为我不喜欢短时间内重复欣赏同一张图片,偶然也不行。 相关注释行和添加的代码,在脚本中很容易能看出来,我就不去改成更方便的切换了,就这样吧。

填坑,之前说会有自动换壁纸的功能,结果一直没做,现在做好了,!R打开3秒一张图的自动切换功能,同时也不会影响手动切换,其实功能很简单,就是一直拖着没去做。

脚本启动后,选择图库,脚本会索引,然后利用随机数去做选择。当按下!w的时候,会切换壁纸。原脚本会一直气泡提醒,在win10中很烦人,于是我注释掉了。关于随机的部分,在注释中,可以根据自己的喜好更改,比如说每10分钟换一张壁纸···

比较重要的部分,是读取文件夹内的文件数量,然后生成伪数组,之后利用随机数或者变量去引用伪数组所代表的文件名。

; Initial Preferences

#SingleInstance Force
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

; Few global variables

IniFile = %A_ScriptDir%\wpchange.ini
TempWallpaper = %A_MyDocuments%\currentwallpaper.jpg

IniRead, WallPaperFolder, %IniFile%, Settings, WPFolder, NOT_SET ;  读取配置文件,获取之前保存的路径,如果有的话。

If WallPaperFolder = NOT_SET ; 如果不存在配置文件,或者需要重新更换路径。
{
	FileSelectFolder, SetFolder, , 3 ; Show a folder for the wallpapers
	if SetFolder =
	{
		MsgBox, You didn't select a folder, the program will now close.	; We can't function without a folder, if the user didn't select one, exit.
		ExitApp
	}
	else
	{
		SetFolder := RegExReplace(SetFolder, "\\$")  ; Removes the trailing backslash, if present.
		WallPaperFolder = %SetFolder%\ ; We need the trailing backslash for our purposes, but removing it first eliminates the possibility of dual slashes.
		IniWrite, %WallPaperFolder%, %IniFile%, Settings, WPFolder ; Now that we have all the settings, write them to IniFile so we don't need to ask them again at relaunch
		MsgBox Setting saved. To reset, choose the "Reset Folder" item from tray or delete file %IniFile%.`n`nYou can now change your wallpaper at will by pressing "Win+MiddleMouseButton". The program will select a random image from the folder you provided and set it as your wallpaper.
	}
}

RefreshWPs()
{
	Global
	ArrayCount=0
	Loop, %WallPaperFolder%*.jpg ; check all jpg images in the WallpaperFolder
	{
	ArrayCount+=1
	Array%ArrayCount% := A_LoopFileName ; Make an array of all the available filenames
	}
}
filenum=0 ;因为我不喜欢随机功能,所以在函数中借用了一个变量,使用了+=,这样每按一次,就意味着使用下一张图片,而不是随机。
;在脚本中先定义函数为0,这样第一次就是从1开始了。需要随机功能的朋友请删除这个。
ChangeWP()
{
	global
	
	filenum+=1 ;需要随机功能的朋友请删除这个。并把下面一行取消注释
	;~ Random, filenum, 1, %ArrayCount% ;这里设定了随机数,如果从这里入手,应该是可以改成顺序显示的。traytip的语句全部被我禁用了。之后再改。
	filecalled := Array%filenum% ;利用随机数和伪数组,搞定下一张图片
	if (filenum=ArrayCount) ;因为是顺序换图,所以循环,就这样。需要随机功能的朋友请删除这部分。
	{
		filenum=0
	}
	
	
	TrayTip, Wallpaper Changer, Setting "%filecalled%" as your wallpaper,,1
	FileCopy, %WallPaperFolder%%filecalled%, %TempWallpaper%, 1 ; copy the file to a temp location
	FileCopyError = %ErrorLevel%
	If ErrorLevel = 1
	{
		MsgBox While copying %WallPaperFolder%%filecalled% to a temporary location at %TempWallpaper%, a FileCopy error occured, please contact the developer!
	}
	DllCall("SystemParametersInfo", UInt, 0x14, UInt, 0, Str, TempWallpaper, UInt, 2)
	RegWrite, REG_SZ, HKEY_CURRENT_USER, Control Panel\Desktop, WallPaper, %WallPaperFolder%%filecalled%
	Sleep, 500 ; can't change the wallpaper too quickly in succession.
	return
}

;~ Traytip, Wallpaper Changer, Refreshing Wallpapers...,,1
RefreshWPs()
;~ TrayTip, Wallpaper Changer, Wallpaper list refreshed! (%ArrayCount% Found),,1
Menu, TRAY, Add
Menu, TRAY, Add, Refresh Wallpapers, RefreshWallpapers ; Manual refresh
Menu, TRAY, Add, Change Wallpaper, ChangeWallpaper ; Add a menu for it too
Menu, TRAY, Add
Menu, TRAY, Add, Reset Folder, RstFolder ; Add menu for resetting folder
Return ; Ends the auto-execute section

RefreshWallpapers: ; does the same job as the auto-execute
;~    Traytip, Wallpaper Changer, Refreshing Wallpapers...,,1
   RefreshWPs()
;~    TrayTip, Wallpaper Changer, Wallpaper list refreshed! (%ArrayCount% Found),,1
   Return

ChangeWallpaper:
	ChangeWP()
	Return

RstFolder:
	MsgBox, 52, Wallpaper Changer, Resetting folder settings requires program restart. You can choose new folder on next launch. Do you want to reset the settings and close the application now?
	IfMsgBox Yes
	{
		IniWrite, NOT_SET, %IniFile%, Settings, WPFolder ; Set the WPFolder as "NOT_SET" so next launch the user can choose a new folder.
		ExitApp
	}
	Else
	{
;~ 		TrayTip, Wallpaper Changer, Reset cancelled. Nothing done!,,1 ; Inform the user that we did nothing when "No" was clicked.
	}
	Return

!w::ChangeWP() ;热键手动切换壁纸
go:	;利用标签引用函数
	ChangeWP()
	return
!r::SetTimer,go,3000 ;定时,每3秒触发一次go标签,也就是每3秒换一张壁纸。这个随意了。
; Debug Stuff

!q::MsgBox IsAdmin: %A_IsAdmin% - My Documents: %A_MyDocuments%
!a::ListVars
!z::MsgBox Last Copy: "%WallPaperFolder%%filecalled%" to "%A_MyDocuments%\currentwallpaper.jpg"
f5::Reload


给TA捐赠
共{{data.count}}人
人已捐赠
其他案例

[办公]屏幕任意位置显示当前时间

2016-11-26 21:45:53

其他案例

[办公]一键调节音量的脚本

2016-11-30 23:43:49

8 条回复 A文章作者 M管理员
  1. ss

    老大,您的脚本我运行成功了一会,很喜欢,然后就出问题了:while copying d:\wallpapers\5.jpg to a temporary location at c:\users\xxx\documents\currentwallpaper.jpg, a filecopy error occured,
    please contact the developer.

  2. ss

    自己研究了一下,问题解决了,这个脚本太罗嗦了,本来几行就能解决的东西。。。

    • 悠然生活

      既然解决了问题,就顺便分享一下,也给后来人学习一下嘛。

  3. ahkjoo

    在win10、win11下面我遇到的问题。
    表面上看可以切换,
    但win11下,因为我已经对每个虚拟桌面单独设置了背景图片。
    如果我开始按照ahk切换照片,看起来可以,
    但是当我切换虚拟桌面后,虚拟桌面下的图片又回到了原始的图片。
    这说明,AHK没有替换掉设置的虚拟桌面下的背景图片
    请问这应该如何优化呢?

  4. ahkjoo

    看了代码,的确写入到了注册表,但是win来回切换虚拟桌面的时候也会重新写注册表,这样导致之前的切换失效。

  5. ahkjoo

    下载了微软官方的 必应壁纸应用来测试,发现官方的应用成功的适配了虚拟桌面切换背景图片的切换问题。所有,我想AHK应该还是有办法的。
    方向可能就是找到虚拟桌面配置背景图片的参数方法什么的。。作者可不可以研究一下呢?

    • 悠然生活

      我年纪大了,不cool了,对桌面背景没有任何需求了···

  6. 悠然生活

    我年纪大了,不cool了,对桌面背景没有任何需求了···

个人中心
购物车
优惠劵
有新私信 私信列表
搜索