diff --git a/Preses/GlobalPres.cs b/Preses/GlobalPres.cs index 228186f..039cf71 100644 --- a/Preses/GlobalPres.cs +++ b/Preses/GlobalPres.cs @@ -1,7 +1,9 @@ -using CommunityToolkit.Mvvm.ComponentModel; +using System.Windows; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging.Messages; using MaterialDesignThemes.Wpf; +using Sheas_Cealer.Utils; namespace Sheas_Cealer.Preses; @@ -18,6 +20,8 @@ internal partial class GlobalPres : ObservableRecipient, IRecipient(T oldValue, T newValue, string? propertyName) => Messenger.Send(new PropertyChangedMessage(this, propertyName, oldValue!, newValue!)); diff --git a/Utils/BorderThemeSetter.cs b/Utils/BorderThemeSetter.cs new file mode 100644 index 0000000..80d7bdf --- /dev/null +++ b/Utils/BorderThemeSetter.cs @@ -0,0 +1,31 @@ +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; + +namespace Sheas_Cealer.Utils; + +internal static partial class BorderThemeSetter +{ + private const int DWMWA_USE_IMMERSIVE_DARK_MODE_OLD = 19; + private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 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, DWMWA_USE_IMMERSIVE_DARK_MODE, out isDarkTheme, (uint)Marshal.SizeOf(typeof(nint))); + + _ = DwmSetWindowAttribute(windowHwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_OLD, ref isDarkTheme, (uint)Marshal.SizeOf(typeof(nint))); + _ = DwmSetWindowAttribute(windowHwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, ref isDarkTheme, (uint)Marshal.SizeOf(typeof(nint))); + } +} \ No newline at end of file diff --git a/Utils/IconRemover.cs b/Utils/IconRemover.cs index 67ca68d..75588a2 100644 --- a/Utils/IconRemover.cs +++ b/Utils/IconRemover.cs @@ -1,5 +1,4 @@ -using System; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; @@ -16,28 +15,24 @@ internal static partial class IconRemover private const uint WM_SETICON = 0x0080; [LibraryImport("user32.dll", EntryPoint = "GetWindowLongW")] - private static partial int GetWindowLong(IntPtr hwnd, int index); + private static partial int GetWindowLong(nint hwnd, int index); [LibraryImport("user32.dll", EntryPoint = "SetWindowLongW")] - private static partial int SetWindowLong(IntPtr hwnd, int index, int newStyle); + private static partial int SetWindowLong(nint hwnd, int index, nint newStyle); [LibraryImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags); + private static partial bool SetWindowPos(nint hwnd, nint hwndInsertAfter, int x, int y, int width, int height, uint flags); [LibraryImport("user32.dll", EntryPoint = "SendMessageW")] - private static partial IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam); + private static partial nint SendMessage(nint hwnd, uint msg, nint wParam, nint lParam); internal static void RemoveIcon(Window window) { - // 获取该窗口句柄 - IntPtr hwnd = new WindowInteropHelper(window).Handle; + nint hwnd = new WindowInteropHelper(window).Handle; - // 将窗口更改为不显示窗口图标 _ = SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_DLGMODALFRAME); - // 更新窗口的非客户区域来显示更改 - SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + SetWindowPos(hwnd, nint.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); - // 防止自定义图标生效 - SendMessage(hwnd, WM_SETICON, new IntPtr(1), IntPtr.Zero); - SendMessage(hwnd, WM_SETICON, IntPtr.Zero, IntPtr.Zero); + SendMessage(hwnd, WM_SETICON, new nint(1), nint.Zero); + SendMessage(hwnd, WM_SETICON, nint.Zero, nint.Zero); } } \ No newline at end of file