如何使用 PowerShell 和注册表将桌面背景图像替换为纯色?

如何使用 PowerShell 和注册表将桌面背景图像替换为纯色?

我想用较暗的纯色替换默认的桌面背景图像,未注册Windows 10 专业版 (内部预览) 版。我正在测试各种终端配色方案,默认图像太亮而且不稳固,令人烦恼。我尝试了各种注册表设置,还重命名和替换了图像,但结果总是一样。

我尝试了一些方法:

# Take (user) owbership of file:
takeown /F C:\Windows\Web\Wallpaper\Windows\img0.jpg
# C:\Windows\Web\Wallpaper\Windows\img0.jpg

$key = 'HKCU:\Control Panel\Colors'         # 
$key = 'HKCU:\Control Panel\Desktop'        # 
$key = 'HKCU:\Control Panel\Desktop\Colors' # 

# Doesn't seem to effect the Desktop...only console
Set-ItemProperty -Path $key -Name 'Window' -Value '1 36 86'
# reset to default
Set-ItemProperty -Path $key -Name 'Window' -Value '255 255 255'

如何使用 PowerShell 设置正确的注册表项来删除背景图像并获得纯色?

答案1

我确实找到了两种方法。一种是CMD-R运行一些 Windows 魔法,另一种是使用 ( ) 将 PATH 值设置为空''

然而,显然还有另一个注册表项包含十六进制编码形式的路径,HKCU:\Control Panel\Desktop\TranscodedImageCache您可以通过基本的十六进制转换看到它。

# Set the wallpaper PATH to ''
$key = 'HKCU:\Control Panel\Desktop'
Set-ItemProperty -Path $key -Name 'WallPaper' -Value ''

# Re-start windows Explorer:
Stop-Process -ProcessName explorer

# Using `CMD+R` and run : 
shell:::{ED834ED6-4B5A-4bfe-8F11-A626DCB6A921} -Microsoft.Personalization\pageWallpaper

# Getting the "Transcoded" PATH:
$TIC=(Get-ItemProperty 'HKCU:\Control Panel\Desktop' TranscodedImageCache -ErrorAction Stop).TranscodedImageCache
[System.Text.Encoding]::Unicode.GetString($TIC) -replace '(.+)([A-Z]:[0-9a-zA-Z\\])+','$2'

#C:\Windows\Web\Wallpaper\Windows\_img0.jpg

也涉及这个答案

  • 您需要重新启动 Windows explorer.exe(使用Sysinternal 的进程浏览器或 PS Stop-Process -ProcessName explorer:)以使注册表更改生效。

更新:2020-01-09

  • 您无需重新启动资源管理器,也无需通过 PoweShell 编译任何东西,......几乎。来自博客和SESU 回答说,我发现了一句很棒的俏皮话:
add-type -typedefinition "using System;`n using System.Runtime.InteropServices;`n public class PInvoke { [DllImport(`"user32.dll`")] public static extern bool SetSysColors(int cElements, int[] lpaElements, int[] lpaRgbValues); }"

# Now to get your desktop to instantly turn purple, run it with:
[PInvoke]::SetSysColors(1, @(1), @(0xAA40C0))
# Or tack it on the end of above for a true one-line experience.

作者注:“这不会影响注册表,所以如果您希望更改保留,您还需要自己将新数据写入注册表。”

答案2

not2qubit通过调用 C# 完成了解决方案。我清除了当前壁纸,并将新的颜色值保存到注册表中

变量$color存储要设置的新颜色。

变量$CSharpScript存储要调用的 C# 脚本。

C# 的基本代码从这里获取:https://stackoverflow.com/questions/7309943/c-set-desktop-wallpaper-to-a-solid-color

我们[SolidWallpaper]::SetWallpaper($color)可以调用创建的 C# 函数,其中SolidWallpaper创建了类并SetWallpaper创建了函数

$color = 0xf0f0f0

$CSharpScript = @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

public class SolidWallpaper
{
  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 fWinIni);

  [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  private static extern bool SetSysColors(int cElements, int[] lpaElements, uint[] lpaRgbValues);

  public static void SetWallpaper(uint color) {
    // clear current wallpaper and update ini
    SystemParametersInfo(SetDesktopWallpaper, 0, "", UpdateIniFile | SendWinIniChange);

    // set solid color
    int[] lpaElements = { 1 };
    uint[] lpaRgbValues = { color };
    SetSysColors(1, lpaElements, lpaRgbValues);

    // save to registry
    int rgb = (int)color;
    RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
    key.SetValue(@"Background", string.Format("{0} {1} {2}", (rgb >> 16) & 0xff, (rgb >> 8) & 0xff, (rgb >> 0) & 0xff));
  }
}
"@

add-type -typedefinition $CSharpScript
[SolidWallpaper]::SetWallpaper($color)

相关内容