You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# Windows 10 UWP应用中实现主窗口圆角效果(SDK 19041)

根因说明

你调用的DWMWA_WINDOW_CORNER_PREFERENCE是Windows 11 内部版本22000及以上才新增的DWM窗口属性,Windows 10 19041版本系统不支持该属性,因此即使API调用返回成功,也不会产生圆角效果。


可行解决方案

方案1:Windows 10兼容模拟圆角(适配19041版本)

Win10本身无系统级窗口圆角能力,需在应用层模拟实现:

  1. 扩展应用内容到标题栏区域,隐藏系统默认边框
    在OnLaunched方法中添加如下代码:
using Windows.ApplicationModel.Core;
using Windows.UI.ViewManagement;
using Windows.UI;

// 扩展内容覆盖标题栏区域
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;

// 透明化系统标题栏按钮背景
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ButtonBackgroundColor = Colors.Transparent;
titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
titleBar.BackgroundColor = Colors.Transparent;
titleBar.InactiveBackgroundColor = Colors.Transparent;
  1. 修改主页面布局实现圆角
    将主页面的根背景设为透明,外层包裹带圆角的Border控件:
<Page Background="Transparent">
    <Border x:Name="RootBorder" CornerRadius="12" Background="White" Margin="0">
        <!-- 原有页面布局内容 -->
        <Grid>
            ...
        </Grid>
    </Border>
</Page>
  1. 适配最大化场景
    监听窗口大小变化事件,最大化状态下隐藏圆角避免边缘空隙:
Window.Current.SizeChanged += (s, e) =>
{
    var view = ApplicationView.GetForCurrentView();
    if (view.IsFullScreenMode || view.VisibleBounds.Width == Window.Current.Bounds.Width)
    {
        RootBorder.CornerRadius = new CornerRadius(0);
    }
    else
    {
        RootBorder.CornerRadius = new CornerRadius(12);
    }
};

方案2:多系统版本兼容适配

如果应用需要同时支持Win10和Win11,可添加版本判断逻辑:

// 先获取窗口句柄
dynamic corewin = Windows.UI.Core.CoreWindow.GetForCurrentThread();
var interop = (ICoreWindowInterop)corewin;
var handle = interop.WindowHandle;

// 判断系统版本
if (Environment.OSVersion.Version.Build >= 22000)
{
    // Win11及以上直接调用系统API设置圆角
    var attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
    var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;
    DwmSetWindowAttribute(handle, attribute, ref preference, sizeof(uint));
}
else
{
    // Win10走模拟圆角逻辑
}

注意事项

窗口句柄必须在OnLaunched方法中、主窗口初始化完成后获取,构造函数阶段CoreWindow还未初始化,必然返回空值。

内容的提问来源于stack exchange,提问作者Sujith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:36:03