Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 70 additions & 14 deletions Sample Applications/WPFGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.Windows.Navigation;
using System.Windows.Shell;
using Microsoft.Win32;
Expand Down Expand Up @@ -41,15 +43,15 @@ public MainWindow(MainWindowViewModel viewModel, IServiceProvider serviceProvide
GlassFrameThickness = new Thickness(-1),
ResizeBorderThickness = ResizeMode == ResizeMode.NoResize ? default : new Thickness(4),
UseAeroCaptionButtons = true,
NonClientFrameEdges = SystemParameters.HighContrast ? NonClientFrameEdges.None :
NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left
NonClientFrameEdges = GetPreferredNonClientFrameEdges()
}
);

SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
this.StateChanged += (s, e) => UpdateMainWindowVisuals();
this.Activated += (s, e) => UpdateMainWindowVisuals();
this.Deactivated += (s, e) => UpdateMainWindowVisuals();
this.Loaded += (_, _) => ApplyWindowDarkMode();
}

private void UpdateWindowBackground()
Expand Down Expand Up @@ -99,29 +101,26 @@ private void UpdateMainWindowVisuals()
}

UpdateTitleBarButtonsVisibility();
bool shouldForceEdgeNone = ShouldForceNonClientFrameEdgesNone();

if(SystemParameters.HighContrast == true)
{
HighContrastBorder.SetResourceReference(BorderBrushProperty, IsActive ? SystemColors.ActiveCaptionBrushKey :
SystemColors.InactiveCaptionBrushKey);
HighContrastBorder.BorderThickness = new Thickness(8, 1, 8, 8);

WindowChrome wc = WindowChrome.GetWindowChrome(this);
if(wc is not null)
{
wc.NonClientFrameEdges = NonClientFrameEdges.None;
}
}
else
{
HighContrastBorder.BorderBrush = Brushes.Transparent;
HighContrastBorder.BorderThickness = new Thickness(0);
}

var wc = WindowChrome.GetWindowChrome(this);
if(wc is not null)
{
wc.NonClientFrameEdges = NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left;
}
var wc = WindowChrome.GetWindowChrome(this);
if (wc is not null)
{
wc.NonClientFrameEdges = shouldForceEdgeNone
? NonClientFrameEdges.None
: NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left;
}
}

Expand Down Expand Up @@ -254,4 +253,61 @@ private void SettingsButton_Click(object sender, RoutedEventArgs e)
"ButtonClickedActivity"
);
}

private static NonClientFrameEdges GetPreferredNonClientFrameEdges()
{
return ShouldForceNonClientFrameEdgesNone()
? NonClientFrameEdges.None
: NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left;
}

private static bool ShouldForceNonClientFrameEdgesNone()
{
return SystemParameters.HighContrast == true || IsWindows10WithLegacyChromeBehavior();
}

private static bool IsWindows10WithLegacyChromeBehavior()
{
return OperatingSystem.IsWindows() &&
OperatingSystem.IsWindowsVersionAtLeast(10) &&
!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000);
}

private void ApplyWindowDarkMode()
{
if (!IsImmersiveDarkModeSupported())
{
return;
}

var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero)
{
return;
}

int darkModeEnabled = 1;
try
{
_ = DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkModeEnabled, sizeof(int));
}
catch (DllNotFoundException)
{
// dwmapi.dll is unavailable on this system.
}
catch (EntryPointNotFoundException)
{
// The immersive dark mode attribute is not supported.
}
}

private static bool IsImmersiveDarkModeSupported()
{
return OperatingSystem.IsWindows() && OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17763);
}

[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
}
Loading