style<Window>: makes the window border color is the same on both Windows 10 and 11

This commit is contained in:
leo 2023-10-09 20:30:53 +08:00
parent 80aa468b08
commit d9afb798db

View file

@ -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 {
/// <summary>
@ -9,6 +10,31 @@ namespace SourceGit.Views.Controls {
/// </summary>
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) {