1
+ using System ;
2
+ using System . Runtime . InteropServices ;
3
+
4
+ namespace SoundSwitch . UI . Menu . Util ;
5
+
6
+ public static class RoundedCorner
7
+ {
8
+ private const int OS_WINDOWS_11 = 22000 ;
9
+
10
+ // The enum flag for DwmSetWindowAttribute's second parameter, which tells the function what attribute to set.
11
+ // Copied from dwmapi.h
12
+ private enum DWMWINDOWATTRIBUTE
13
+ {
14
+ DWMWA_WINDOW_CORNER_PREFERENCE = 33
15
+ }
16
+
17
+ // The DWM_WINDOW_CORNER_PREFERENCE enum for DwmSetWindowAttribute's third parameter, which tells the function
18
+ // what value of the enum to set.
19
+ // Copied from dwmapi.h
20
+ public enum DWM_WINDOW_CORNER_PREFERENCE
21
+ {
22
+ DWMWCP_DEFAULT = 0 ,
23
+ DWMWCP_DONOTROUND = 1 ,
24
+ DWMWCP_ROUND = 2 ,
25
+ DWMWCP_ROUNDSMALL = 3
26
+ }
27
+
28
+ // Import dwmapi.dll and define DwmSetWindowAttribute in C# corresponding to the native function.
29
+ [ DllImport ( "dwmapi.dll" , CharSet = CharSet . Unicode , PreserveSig = false ) ]
30
+ private static extern void DwmSetWindowAttribute ( IntPtr hwnd ,
31
+ DWMWINDOWATTRIBUTE attribute ,
32
+ ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute ,
33
+ uint cbAttribute ) ;
34
+
35
+ /// <summary>
36
+ /// Round the corner of a menu/form (only for Windows 11)
37
+ /// </summary>
38
+ /// <param name="handle"></param>
39
+ /// <param name="preference"></param>
40
+ public static void RoundCorner ( IntPtr handle , DWM_WINDOW_CORNER_PREFERENCE preference )
41
+ {
42
+ if ( Environment . OSVersion . Version . Major < 10 || Environment . OSVersion . Version . Build < OS_WINDOWS_11 )
43
+ {
44
+ return ;
45
+ }
46
+
47
+ var pref = preference ;
48
+ DwmSetWindowAttribute ( handle , DWMWINDOWATTRIBUTE . DWMWA_WINDOW_CORNER_PREFERENCE , ref pref , sizeof ( uint ) ) ;
49
+ }
50
+
51
+
52
+ [ DllImport ( "Gdi32.dll" , EntryPoint = "CreateRoundRectRgn" ) ]
53
+ public static extern IntPtr CreateRoundRectRgn
54
+ (
55
+ int nLeftRect , // x-coordinate of upper-left corner
56
+ int nTopRect , // y-coordinate of upper-left corner
57
+ int nRightRect , // x-coordinate of lower-right corner
58
+ int nBottomRect , // y-coordinate of lower-right corner
59
+ int nWidthEllipse , // width of ellipse
60
+ int nHeightEllipse // height of elli
61
+ ) ;
62
+ }
0 commit comments