我正在运行一个PowerShell脚本,将背景更改为一组特定的颜色。我希望在不重启计算机的情况下完成此操作,但是很遗憾,在Windows 7/8平台上无法立即生效更改。我在网上找到了许多解决方案,但是没有找到适合我的一种。我认为这可能与设置SystemParametersInfo有关,但我不确定。我已经看过一些解决方案并尝试了它们,但我也无法让它们起作用。注册表键更新正常,但更改直到重启后才会生效。以下是我目前拥有的内容,如果有人看到我可以做出不同的建议,我将不胜感激!
backgroundtest.ps1
backgroundtest.ps1
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Background
{
public class Setter {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
public const int SetDesktopBackground = 20; <# following examples online to set parameters #>
public static void SetBackground() {
SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
key.SetValue(@"WallPaper", 0); <#remove wallpaper#>
RegistryKey key2 = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
key2.SetValue(@"Background", "0 118 163"); <#set background to new color>
key.Close();
key2.Close();
}
}
}
"@
[Background.Setter]::SetBackground()
SystemParametersInfo
调用移动到更新注册表键之后,而不是之前,因为这可能会导致系统更新其内部状态。 - Jonathan PotterSystemParametersInfo
来清除任何壁纸图像。 - Jonathan Potter