我正在運行一個PowerShell腳本來將背景更改爲某一組顏色。我想在不重新啓動的情況下執行此操作,但不幸的是,無法立即在Windows 7/8平臺上使更改立即生效。我在網上發現了很多解決方案,但我找不到適合自己的解決方案。我想這可能與設置SystemParametersInfo有關,但我不確定。我已經看到了一些解決方案併爲自己嘗試過,但是我也無法讓它們工作。註冊表項更新只是查找,但更改不會生效,直到我重新啓動後。以下是我迄今爲止,如果有人看到任何我可以做不同的事情,我會感謝幫助!Powershell - 使桌面背景更改立即生效
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'調用移動到**,因爲這可能會導致系統更新其內部狀態。 – 2014-09-10 22:13:41
@JonathanPotter嗨喬納森,謝謝你的回覆。我發現我不需要使用SystemParametersInfo。我需要弄清楚如何使用SetSysColors。現在編輯我的問題。 – wheatfairies 2014-09-10 22:22:11
如果你想以純色結束,你仍然需要'SystemParametersInfo'清除任何壁紙圖像。 – 2014-09-10 23:21:11