你好,我正在尝试制作一个小脚本来每次给定时间更改我的壁纸 我有一个文件夹,其中的图片名称为 1.bmp 、 2.bmp 等
我制作了这个脚本,但它根本不起作用
PS D:\Téléchargements\images\Wallpapers> for($i=1; $i -le 6; $i++){
>> reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ
/d D:\Téléchargements\images\Wallpapers\$i.bmp /f
>> Start-Sleep -s 10
>> rundll32.exe user32.dll, UpdatePerUserSystemParameters
>> Start-Sleep -s 2
>> }
有人可以解释一下为什么吗:(
PS:开始睡眠值是完全随机的,此处用于测试
这应该可以解决问题(在 win 10 中检查):
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d h:\Quotefancy-1542-3840x2160.jpg /f
Start-Sleep -s 10
rundll32.exe user32.dll, UpdatePerUserSystemParameters, 0, $false
或者你可以像这样使用 win32 api:
$setwallpapersrc = @"
using System.Runtime.InteropServices;
public class wallpaper
{
public const int SetDesktopWallpaper = 20;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper ( string path )
{
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
}
}"@
Add-Type -TypeDefinition $setwallpapersrc
[wallpaper]::SetWallpaper("h:\Quotefancy-1542-3840x2160.jpg")
这里有一个类似的问题:Powershell script from快捷方式更改桌面虽然不完全重复,但我的答案会回答你的问题,所以基本上我发现,首先运行'rundll32.exe user32.dll, UpdatePerUserSystemParameters' 单独不会刷新桌面壁纸,除非设置应用程序或打开然后运行
RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters 1, True
RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters
因此,解决方法可以是打开设置应用程序运行命令并将其关闭。
这是一个用于执行此操作的 PowerShell 脚本:
$settings = Get-Process -Name SystemSettings -ErrorAction SilentlyContinue
if (-not $settings) {
Start-Process "ms-settings:"
Start-Sleep -Seconds 1
}
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name 'WallPaper' -Value $MyImage
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name 'WallpaperStyle' -Value 2
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name 'TileWallpaper' -Value 0
RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters 1, True
RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters
if (-not $settings) {
Start-Sleep -Seconds 1
Stop-Process -Name "SystemSettings"
}