Sheas-Cealer/Utils/BorderThemeSetter.cs
2024-12-10 00:09:19 +08:00

31 lines
1.3 KiB
C#

using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace Sheas_Cealer.Utils;
internal static partial class BorderThemeSetter
{
private const int DwmwaUseImmersiveDarkModeOld = 19;
private const int DwmwaUseImmersiveDarkMode = 20;
[LibraryImport("dwmapi.dll")]
private static partial int DwmGetWindowAttribute(nint hwnd, uint attr, out nint attrValue, uint attrSize);
[LibraryImport("dwmapi.dll")]
private static partial int DwmSetWindowAttribute(nint hwnd, uint attr, ref nint attrValue, uint attrSize);
internal static void SetBorderTheme(Window window, bool? isLightTheme)
{
nint isDarkTheme;
nint desktopHwnd = nint.Zero;
nint windowHwnd = new WindowInteropHelper(window).EnsureHandle();
if (isLightTheme.HasValue)
isDarkTheme = !isLightTheme.Value ? 1 : 0;
else
DwmGetWindowAttribute(desktopHwnd, DwmwaUseImmersiveDarkMode, out isDarkTheme, (uint)Marshal.SizeOf(typeof(nint)));
_ = DwmSetWindowAttribute(windowHwnd, DwmwaUseImmersiveDarkModeOld, ref isDarkTheme, (uint)Marshal.SizeOf(typeof(nint)));
_ = DwmSetWindowAttribute(windowHwnd, DwmwaUseImmersiveDarkMode, ref isDarkTheme, (uint)Marshal.SizeOf(typeof(nint)));
}
}