Skip to content

Commit

Permalink
Set solid color wallpaper
Browse files Browse the repository at this point in the history
  • Loading branch information
cschneegans committed Sep 14, 2024
1 parent bd50734 commit d59a6fb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ public record class Configuration(
ExpressSettingsMode ExpressSettings,
ScriptSettings ScriptSettings,
IKeySettings KeySettings,
IWallpaperSettings WallpaperSettings,
bool BypassRequirementsCheck,
bool BypassNetworkCheck,
bool EnableLongPaths,
Expand Down Expand Up @@ -323,6 +324,7 @@ bool HideEdgeFre
ExpressSettings: ExpressSettingsMode.DisableAll,
ScriptSettings: new ScriptSettings([]),
KeySettings: new SkipKeySettings(),
WallpaperSettings: new DefaultWallpaperSettings(),
BypassRequirementsCheck: false,
BypassNetworkCheck: false,
EnableLongPaths: false,
Expand Down
2 changes: 2 additions & 0 deletions UnattendGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<None Remove="resource\RemoveCapabilities.ps1" />
<None Remove="resource\RemoveFeatures.ps1" />
<None Remove="resource\RemovePackages.ps1" />
<None Remove="resource\SetWallpaper.ps1" />
<None Remove="resource\ShowAllTrayIcons.ps1" />
</ItemGroup>

Expand All @@ -30,6 +31,7 @@
<EmbeddedResource Include="resource\Bloatware.json" />
<EmbeddedResource Include="resource\known-writeable-folders.txt" />
<EmbeddedResource Include="resource\DeleteTaskbarIcons.ps1" />
<EmbeddedResource Include="resource\SetWallpaper.ps1" />
<EmbeddedResource Include="resource\ShowAllTrayIcons.ps1" />
<EmbeddedResource Include="resource\RemoveBloatware.ps1" />
<EmbeddedResource Include="resource\TimeOffset.json" />
Expand Down
28 changes: 28 additions & 0 deletions modifier/Optimizations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -43,6 +44,14 @@ public record class ConfigureKeySettings(
KeySetting ScrollLock
) : IKeySettings;

public interface IWallpaperSettings;

public class DefaultWallpaperSettings : IWallpaperSettings;

public record class SolidWallpaperSettings(
Color Color
) : IWallpaperSettings;

class OptimizationsModifier(ModifierContext context) : Modifier(context)
{
public override void Process()
Expand Down Expand Up @@ -416,5 +425,24 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
}
}
}
{
if (Configuration.WallpaperSettings is SolidWallpaperSettings settings)
{
string ps1File = @"C:\Windows\Setup\Scripts\SetWallpaper.ps1";
string script = Util.StringFromResource("SetWallpaper.ps1");
StringWriter writer = new();
writer.WriteLine($"$htmlColor = '{ColorTranslator.ToHtml(settings.Color)}';");
writer.WriteLine(script);
AddTextFile(writer.ToString(), ps1File);
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [
CommandBuilder.UserRunOnceCommand(rootKey, subKey, "SetWallpaper", CommandBuilder.InvokePowerShellScript(ps1File)),
];
})
);
}
}
}
}
32 changes: 32 additions & 0 deletions resource/SetWallpaper.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Add-Type -TypeDefinition '
using System.Drawing;
using System.Runtime.InteropServices;
public static class ColorSetter {
[DllImport("user32.dll")]
private static extern bool SetSysColors(
int cElements,
int[] lpaElements,
int[] lpaRgbValues
);
[DllImport("user32.dll")]
private static extern bool SystemParametersInfo(
uint uiAction,
uint uiParam,
string pvParam,
uint fWinIni
);
public static void SetDesktopBackground(Color color) {
SystemParametersInfo(20, 0, "", 0);
SetSysColors(1, new int[] { 1 }, new int[] { ColorTranslator.ToWin32(color) });
}
}
' -ReferencedAssemblies 'System.Drawing';

$color = [System.Drawing.ColorTranslator]::FromHtml( $htmlColor );
[ColorSetter]::SetDesktopBackground( $color );
Set-ItemProperty -Path 'Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers' -Name 'BackgroundType' -Type 'DWord' -Value 1 -Force;
Set-ItemProperty -Path 'Registry::HKCU\Control Panel\Desktop' -Name 'WallPaper' -Type 'String' -Value '' -Force;
Set-ItemProperty -Path 'Registry::HKCU\Control Panel\Colors' -Name 'Background' -Type 'String' -Value "$($color.R) $($color.G) $($color.B)" -Force;

0 comments on commit d59a6fb

Please sign in to comment.