Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions src/Wpf.Ui/Controls/FluentWindow/FluentWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// All Rights Reserved.

using System.Windows.Shell;
using Wpf.Ui.Appearance;
using Wpf.Ui.Interop;
using Wpf.Ui.Win32;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
Expand Down Expand Up @@ -106,6 +108,11 @@ protected override void OnSourceInitialized(EventArgs e)
OnExtendsContentIntoTitleBarChanged(default, ExtendsContentIntoTitleBar);
OnBackdropTypeChanged(default, WindowBackdropType);

if (Utilities.IsOSWindows11OrNewer)
{
UnsafeNativeMethods.ApplyBorderColor(this, ApplicationAccentColorManager.PrimaryAccent);
}

base.OnSourceInitialized(e);
}

Expand Down
61 changes: 39 additions & 22 deletions src/Wpf.Ui/Interop/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,44 @@ public static bool ApplyWindowCornerPreference(IntPtr handle, WindowCornerPrefer

int pvAttribute = (int)UnsafeReflection.Cast(cornerPreference);

// TODO: Validate HRESULT
_ = Dwmapi.DwmSetWindowAttribute(
return Dwmapi.DwmSetWindowAttribute(
handle,
Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE,
ref pvAttribute,
Marshal.SizeOf(typeof(int))
);
) == 0;
}

return true;
/// <summary>
/// Tries to apply the color of the border.
/// </summary>
/// <param name="window">The window.</param>
/// <param name="color">The color.</param>
/// <returns><see langword="true" /> if invocation of native Windows function succeeds.</returns>
public static bool ApplyBorderColor(Window window, Color color) =>
GetHandle(window, out IntPtr windowHandle)
&& ApplyBorderColor(windowHandle, color);

/// <summary>
/// Tries to apply the color of the border.
/// </summary>
/// <param name="handle">The handle.</param>
/// <param name="color">The color.</param>
/// <returns><see langword="true"/> if invocation of native Windows function succeeds.</returns>
public static bool ApplyBorderColor(IntPtr handle, Color color)
{
if (handle == IntPtr.Zero)
{
return false;
}

if (!User32.IsWindow(handle))
{
return false;
}

int colorNum = (color.B << 16) | (color.G << 8) | color.R;
return Dwmapi.DwmSetWindowAttribute(handle, Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_BORDER_COLOR, ref colorNum, sizeof(int)) == 0;
}

/// <summary>
Expand Down Expand Up @@ -93,10 +122,7 @@ public static bool RemoveWindowDarkMode(IntPtr handle)
dwAttribute = Dwmapi.DWMWINDOWATTRIBUTE.DMWA_USE_IMMERSIVE_DARK_MODE_OLD;
}

// TODO: Validate HRESULT
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also corrected this, it's only returning 0 on success.

_ = Dwmapi.DwmSetWindowAttribute(handle, dwAttribute, ref pvAttribute, Marshal.SizeOf(typeof(int)));

return true;
return Dwmapi.DwmSetWindowAttribute(handle, dwAttribute, ref pvAttribute, Marshal.SizeOf(typeof(int))) == 0;
}

/// <summary>
Expand Down Expand Up @@ -132,10 +158,7 @@ public static bool ApplyWindowDarkMode(IntPtr handle)
dwAttribute = Dwmapi.DWMWINDOWATTRIBUTE.DMWA_USE_IMMERSIVE_DARK_MODE_OLD;
}

// TODO: Validate HRESULT
_ = Dwmapi.DwmSetWindowAttribute(handle, dwAttribute, ref pvAttribute, Marshal.SizeOf(typeof(int)));

return true;
return Dwmapi.DwmSetWindowAttribute(handle, dwAttribute, ref pvAttribute, Marshal.SizeOf(typeof(int))) == 0;
}

/// <summary>
Expand Down Expand Up @@ -214,15 +237,12 @@ public static bool ApplyWindowBackdrop(IntPtr handle, WindowBackdropType backgro
return false;
}

// TODO: Validate HRESULT
_ = Dwmapi.DwmSetWindowAttribute(
return Dwmapi.DwmSetWindowAttribute(
handle,
Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE,
ref backdropPvAttribute,
Marshal.SizeOf(typeof(int))
);

return true;
) == 0;
}

/// <summary>
Expand Down Expand Up @@ -296,15 +316,12 @@ public static bool ApplyWindowLegacyMicaEffect(IntPtr handle)
{
var backdropPvAttribute = 0x1; // Enable

// TODO: Validate HRESULT
_ = Dwmapi.DwmSetWindowAttribute(
return Dwmapi.DwmSetWindowAttribute(
handle,
Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_MICA_EFFECT,
ref backdropPvAttribute,
Marshal.SizeOf(typeof(int))
);

return true;
) == 0;
}

/// <summary>
Expand Down