Problem:Round the corners of C# windows 10 UWP application main window.
Constraints:I am using windows sdk version windows version 2004 10.0 Build 19041.
I heard that corners are rounded automatically if I switch to windows 11 sdk version
I have tried the following in App.xaml.cs.Ref
The logic is as follows
get the window handle of main window and pass it to windows api to round the corners.
sealed partial class App : Application
{
[ComImport, Guid("45D64A29-A63E-4CB6-B498-5781D298CB4F")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ICoreWindowInterop
{
IntPtr WindowHandle { get; }
bool MessageHandled { set; }
}
public enum DWMWINDOWATTRIBUTE
{
DWMWA_WINDOW_CORNER_PREFERENCE = 33
}
// The DWM_WINDOW_CORNER_PREFERENCE enum for DwmSetWindowAttribute's third parameter, which tells the function
// what value of the enum to set.
public enum DWM_WINDOW_CORNER_PREFERENCE
{
DWMWCP_DEFAULT = 0,
DWMWCP_DONOTROUND = 1,
DWMWCP_ROUND = 2,
DWMWCP_ROUNDSMALL = 3
}
// Import dwmapi.dll and define DwmSetWindowAttribute in C# corresponding to the native function.
[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern long DwmSetWindowAttribute(IntPtr hwnd,
DWMWINDOWATTRIBUTE attribute,
ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,
uint cbAttribute);
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
dynamic corewin = Windows.UI.Core.CoreWindow.GetForCurrentThread();
var interop = (ICoreWindowInterop)corewin;
var handle = interop.WindowHandle;
var attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;
DwmSetWindowAttribute(handle, attribute, ref preference, sizeof(uint));
this.Suspending += OnSuspending;
}
but the window handle is null in constructor code. When I tried this code in onlaunched event,the window handle returns value and the code executes successfully. But the rounded effect is not available.Let me know if any solutions available