Created
September 10, 2025 22:08
-
-
Save emoacht/a117985263bdb049bee0b5c1f2891143 to your computer and use it in GitHub Desktop.
Attached property to round corners of window on Windows 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Windows; | |
using System.Windows.Interop; | |
internal static class WindowCorner | |
{ | |
public static bool GetIsRounded(DependencyObject obj) | |
{ | |
return (bool)obj.GetValue(IsRoundedProperty); | |
} | |
public static void SetIsRounded(DependencyObject obj, bool value) | |
{ | |
obj.SetValue(IsRoundedProperty, value); | |
} | |
public static readonly DependencyProperty IsRoundedProperty = | |
DependencyProperty.RegisterAttached("IsRounded", typeof(bool), typeof(WindowCorner), new PropertyMetadata(false, OnIsRoundedChanged)); | |
private static void OnIsRoundedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
if ((e.NewValue is not true) || (d is not Window window)) | |
return; | |
// Apply rounded corners in desktop apps for Windows 11 | |
// https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/ui/apply-rounded-corners | |
var handle = new WindowInteropHelper(window).EnsureHandle(); | |
var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND; | |
DwmSetWindowAttribute( | |
handle, | |
DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE, | |
ref preference, | |
sizeof(uint)); | |
} | |
[DllImport("dwmapi.dll")] | |
private static extern void DwmSetWindowAttribute( | |
IntPtr hwnd, | |
DWMWINDOWATTRIBUTE attribute, | |
ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute, | |
uint cbAttribute); | |
private enum DWMWINDOWATTRIBUTE : uint | |
{ | |
DWMWA_WINDOW_CORNER_PREFERENCE = 33 | |
} | |
private enum DWM_WINDOW_CORNER_PREFERENCE | |
{ | |
DWMWCP_DEFAULT = 0, | |
DWMWCP_DONOTROUND = 1, | |
DWMWCP_ROUND = 2, | |
DWMWCP_ROUNDSMALL = 3 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment