PS C:\>

Apply the Windows theme to your WPF and PowerShell application

Share:

In this post i will explain you how to apply the default Windows theme (Dark or Light) to your PowerShell and WPF GUI using MahApps.

In my previous post, from my own blog, I explained how to change the theme and accent of a WPF GUI, as below:
  
Now let's get further with this.

As you may now, by on new Windows 10 release, two themes are avaible:
- Dark mode
- Light mode

The goal of the script I will show you is to check the current Windows 10 theme and apply it to your GUI.
- If Windows 10 is configured with Dark mode, GUI will be opened in Dark
- If Windows 10 is configured with Light mode, GUI will be opened in Light

For that I will check a specific registry value called AppsUseLightTheme and apply the theme with the method mentioned in my post here.

See below the script to detect the Windows theme and apply it to your GUI.

$Windows_Theme_Key = (get-itemproperty -path registry::"HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" -erroraction 'silentlycontinue').AppsUseLightTheme
If($Windows_Theme_Key -ne $null)
{
If($Windows_Theme_Key -eq 1)
{
$OS_Theme = "BaseLight"
}
Else
{
$OS_Theme = "BaseDark"
}
}
Else
{
$OS_Theme = "BaseLight"
}

$Theme = [MahApps.Metro.ThemeManager]::DetectAppStyle($form)
[MahApps.Metro.ThemeManager]::ChangeAppStyle($form, $Theme.Item2, [MahApps.Metro.ThemeManager]::GetAppTheme("$OS_Theme"));

See below an overview:

Damien Van Robaeys 😁

Aucun commentaire