【问题标题】:Script to change windows background from solid color to wallpaper将Windows背景从纯色更改为墙纸的脚本
【发布时间】:2021-05-07 00:27:26
【问题描述】:

我正在尝试使用 PowerShell 脚本来设置 Windows 壁纸。我是从Script to change wallpaper in windows 10? 那里学到的:

Function Set-WallPaper($Value)
{
    Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
    rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
 
Set-WallPaper -value 'c:\Temp\wallpaper.png'

不幸的是,它仅在设置 -> 个性化 -> 背景已设置为“图片”时才有效。它不会从“纯色”切换。有没有办法扩展脚本,让它也从“纯色”切换到“图片”?

【问题讨论】:

  • 即使您注销并重新登录也不行吗?
  • 好问题。如果我注销并登录,则根据需要设置壁纸。所以注册表值很好,但我需要以某种方式传播更改。嗯。
  • 从我读到的一些已经成功地改变了图像,只需运行脚本两次。我看到的另一个有趣的方法是改变你的屏幕分辨率。至于编写脚本 - 正如您所承认的那样,脚本确实更改了密钥,因此这似乎是一个错误。

标签: powershell registry


【解决方案1】:

好的,所以解决方案是使用SystemParametersInfo 方法,而不是旧的和不可靠的UpdatePerUserSystemParameters。例如。这个表格来自Set-Wallpaper by Jose Espitia

Function Set-WallPaper($Image) {
  
Add-Type -TypeDefinition @" 
using System; 
using System.Runtime.InteropServices;
  
public class Params
{ 
    [DllImport("User32.dll",CharSet=CharSet.Unicode)] 
    public static extern int SystemParametersInfo (Int32 uAction, 
                                                   Int32 uParam, 
                                                   String lpvParam, 
                                                   Int32 fuWinIni);
}
"@ 
  
    $SPI_SETDESKWALLPAPER = 0x0014
    $UpdateIniFile = 0x01
    $SendChangeEvent = 0x02
  
    $fWinIni = $UpdateIniFile -bor $SendChangeEvent
  
    $ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
 
}
 
 Set-WallPaper -Image 'c:\Temp\wallpaper.png'

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多