正如标题所说 - 壁纸只是变黑了。这是我当前正在使用的代码(在一个网站上找到的(我认为是 stackoverflow),但我一生都找不到):
function Set-Wallpaper {
param (
[string]$Path = ".\wallpaper.png",
[ValidateSet('Tile', 'Center', 'Stretch', 'Fill', 'Fit', 'Span')]
[string]$Style = 'Fill'
)
begin {
try {
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Tile, Center, Stretch, Fill, Fit, Span, NoChange
}
public class Setter
{
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 fuWinIni );
public static void SetWallpaper ( string path, Wallpaper.Style style )
{
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
RegistryKey key = Registry.CurrentUser.OpenSubKey( "Control Panel\\Desktop", true );
switch( style )
{
case Style.Tile :
key.SetValue( @"WallpaperStyle", "0" ) ;
key.SetValue( @"TileWallpaper", "1" ) ;
break;
case Style.Center :
key.SetValue( @"WallpaperStyle", "0" ) ;
key.SetValue( @"TileWallpaper", "0" ) ;
break;
case Style.Stretch :
key.SetValue( @"WallpaperStyle", "2" ) ;
key.SetValue( @"TileWallpaper", "0" ) ;
break;
case Style.Fill :
key.SetValue( @"WallpaperStyle", "10" ) ;
key.SetValue( @"TileWallpaper", "0" ) ;
break;
case Style.Fit :
key.SetValue( @"WallpaperStyle", "6" ) ;
key.SetValue( @"TileWallpaper", "0" ) ;
break;
case Style.Span :
key.SetValue( @"WallpaperStyle", "22" ) ;
key.SetValue( @"TileWallpaper", "0" ) ;
break;
case Style.NoChange :
break;
}
key.Close();
}
}
}
"@
} catch {}
$StyleNum = @{
Tile = 0
Center = 1
Stretch = 2
Fill = 3
Fit = 4
Span = 5
}
}
process {
[Wallpaper.Setter]::SetWallpaper($Path, $StyleNum[$Style])
}
}
Set-WallPaper -Path 'c:\path\to\wallpaper.jpg' -Style Fill
我尝试将其从 png 图像更改为 bmp 图像,但没有成功 - 返回到 png 以防万一这只是侥幸,事实并非如此。壁纸还是黑色的。 VSCode 没有给我任何错误 - 它相信代码应该运行得很好。
谢谢!
功能设置-壁纸{ 参数( [字符串]$Path = ".\wallpaper.png", [ValidateSet('平铺'、'居中'、'拉伸'、'填充'、'适合'、'跨度')] [字符串]$Style = '填充' )
begin {
try {
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Tile, Center, Stretch, Fill, Fit, Span, NoChange
}
public class Setter
{
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 fuWinIni);
public static void SetWallpaper (string path, Style style)
{
SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange);
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
switch(style)
{
case Style.Tile:
key.SetValue(@"WallpaperStyle", "0");
key.SetValue(@"TileWallpaper", "1");
break;
case Style.Center:
key.SetValue(@"WallpaperStyle", "0");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Stretch:
key.SetValue(@"WallpaperStyle", "2");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Fill:
key.SetValue(@"WallpaperStyle", "10");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Fit:
key.SetValue(@"WallpaperStyle", "6");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Span:
key.SetValue(@"WallpaperStyle", "22");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.NoChange:
// No change to wallpaper style
break;
}
key.Close();
}
}
}
“@ } 抓住 {}
$StyleNum = @{
Tile = [Wallpaper.Style]::Tile
Center = [Wallpaper.Style]::Center
Stretch = [Wallpaper.Style]::Stretch
Fill = [Wallpaper.Style]::Fill
Fit = [Wallpaper.Style]::Fit
Span = [Wallpaper.Style]::Span
}
}
process {
[Wallpaper.Setter]::SetWallpaper($Path, $StyleNum[$Style])
}
}
设置-WallPaper-路径'PUTYOURDESIREDIMAGEFILEPATHHERE'-样式填充