如何解决将窗口背景从纯色更改为墙纸的脚本
我正在尝试使用 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'
不幸的是,它仅在设置 -> 个性化 -> 背景已设置为“图片”时才有效。它不会从“纯色”切换。有没有办法扩展脚本,让它也从“纯色”切换到“图片”?
解决方法
好的,所以解决方案是使用 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,$Image,$fWinIni)
}
Set-WallPaper -Image 'c:\Temp\wallpaper.png'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。