如何在 c# windows 10 UWP 中圆角 App main window?
How to round corner of App main window in c# windows 10 UWP?
问题:圆角 C# windows 10 UWP 应用程序主 window。
限制条件:我使用的是windows sdk版本windows version 2004 10.0 Build 19041.
听说切换到windows11 sdk版本
会自动圆角
我在 App.xaml.cs 中尝试了以下方法。Ref
逻辑如下
获取mainwindow的window句柄,传给windows api圆角
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;
}
但 window 句柄在构造函数代码中为空。当我在 onlaunched 事件中尝试此代码时,window 处理 returns 值并且代码成功执行。但是圆角效果不是available.Let我知道有没有解决办法
How to round corner of App main window in c# windows 10 UWP?
恐怕没有api可以为uwp设置应用程序的主要window圆角,它是由系统管理的,没有提供api设置手动。
从您提到的代码中派生,您可以获得 window 的 hwnd,但是没有采用 HWND 的受支持 API。对于 DwmSetWindowAttribute
,请参阅其文档最低支持的客户端 Windows Vista [仅限桌面应用],但不包括 UWP 平台。
问题:圆角 C# windows 10 UWP 应用程序主 window。
限制条件:我使用的是windows sdk版本windows version 2004 10.0 Build 19041.
听说切换到windows11 sdk版本
会自动圆角我在 App.xaml.cs 中尝试了以下方法。Ref
逻辑如下
获取mainwindow的window句柄,传给windows api圆角
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;
}
但 window 句柄在构造函数代码中为空。当我在 onlaunched 事件中尝试此代码时,window 处理 returns 值并且代码成功执行。但是圆角效果不是available.Let我知道有没有解决办法
How to round corner of App main window in c# windows 10 UWP?
恐怕没有api可以为uwp设置应用程序的主要window圆角,它是由系统管理的,没有提供api设置手动。
从您提到的代码中派生,您可以获得 window 的 hwnd,但是没有采用 HWND 的受支持 API。对于 DwmSetWindowAttribute
,请参阅其文档最低支持的客户端 Windows Vista [仅限桌面应用],但不包括 UWP 平台。