From d9afb798dbdc84c1221086d62f04f8a5a100895b Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 9 Oct 2023 20:30:53 +0800 Subject: [PATCH] style: makes the window border color is the same on both Windows 10 and 11 --- src/Views/Controls/Window.cs | 52 +++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Views/Controls/Window.cs b/src/Views/Controls/Window.cs index cd4282a5..c379f13c 100644 --- a/src/Views/Controls/Window.cs +++ b/src/Views/Controls/Window.cs @@ -1,7 +1,8 @@ using System; using System.Windows; -using System.Windows.Documents; -using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Windows.Interop; +using System.Windows.Media; namespace SourceGit.Views.Controls { /// @@ -9,6 +10,31 @@ namespace SourceGit.Views.Controls { /// public class Window : System.Windows.Window { + [StructLayout(LayoutKind.Sequential)] + private struct OSVERSIONINFOEX { + public int Size; + public int Major; + public int Minor; + public int Build; + public int Platform; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string CSDVersion; + public ushort ServicePackMajor; + public ushort ServicePackMinor; + public short SuiteMask; + public byte ProductType; + public byte Reserved; + } + + [DllImport("ntdll.dll", CharSet = CharSet.Unicode, SetLastError = true)] + private static extern int RtlGetVersion(ref OSVERSIONINFOEX version); + + [DllImport("dwmapi.dll", CharSet = CharSet.Unicode, SetLastError = true)] + private static extern long DwmSetWindowAttribute(IntPtr hwnd, + uint attribute, + ref uint pvAttribute, + uint cbAttribute); + public static readonly DependencyProperty IsMaximizedProperty = DependencyProperty.Register( "IsMaximized", typeof(bool), @@ -22,7 +48,27 @@ namespace SourceGit.Views.Controls { public Window() { Style = FindResource("Style.Window") as Style; - Loaded += (_, __) => OnStateChanged(null); + Loaded += OnWindowLoaded; + } + + private void OnWindowLoaded(object sender, RoutedEventArgs e) { + OnStateChanged(null); + + // Windows 11 需要特殊处理一下边框,使得其与Window 10下表现一致 + OSVERSIONINFOEX version = new OSVERSIONINFOEX() { Size = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; + if (RtlGetVersion(ref version) == 0 && version.Major >= 10 && version.Build >= 22000) { + Models.Theme.Changed += UpdateBorderColor; + Unloaded += (_, __) => Models.Theme.Changed -= UpdateBorderColor; + + UpdateBorderColor(); + } + } + + private void UpdateBorderColor() { + IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle(); + Color color = (BorderBrush as SolidColorBrush).Color; + uint preference = ((uint)color.B << 16) | ((uint)color.G << 8) | (uint)color.R; + DwmSetWindowAttribute(hWnd, 34, ref preference, sizeof(uint)); } protected override void OnStateChanged(EventArgs e) {