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

WinUI3中为窗口背景设置亚克力画笔的最简方法是什么?

在WinUI3中实现无第三方依赖的窗口毛玻璃效果

需求说明

无需使用WinEx或Win2D NuGet包,实现窗口毛玻璃效果(可透过窗口看到桌面),替代WinUI2中已废弃的ElementCompositionPreview.SetIsHostBackdrop和AcrylicBackgroundSource.HostBackdrop方案。


方案一:纯WinUI3 Composition API实现

在窗口代码后台(如MainWindow.xaml.cs)中添加以下逻辑:

using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Hosting;
using Windows.UI;

public MainWindow()
{
    InitializeComponent();
    SetAcrylicBackdrop();
}

private void SetAcrylicBackdrop()
{
    // 创建并配置Acrylic画刷
    var acrylicBrush = new AcrylicBrush
    {
        BackgroundSource = AcrylicBackgroundSource.Backdrop,
        TintColor = Colors.Transparent,
        FallbackColor = Colors.Transparent,
        TintOpacity = 0.2,
        Opacity = 0.9
    };

    // 设置窗口背景
    Background = acrylicBrush;

    // 获取根视觉元素与 compositor
    var rootElement = Content as FrameworkElement;
    var rootVisual = ElementCompositionPreview.GetElementVisual(rootElement);
    var compositor = rootVisual.Compositor;

    // 创建HostBackdrop画刷并绑定到视觉元素
    var backdropBrush = compositor.CreateHostBackdropBrush();
    var backdropVisual = compositor.CreateSpriteVisual();
    backdropVisual.Brush = backdropBrush;
    backdropVisual.Size = new Vector2((float)ActualWidth, (float)ActualHeight);

    ElementCompositionPreview.SetElementChildVisual(rootElement, backdropVisual);

    // 监听窗口尺寸变化,更新背景视觉大小
    SizeChanged += (s, e) =>
    {
        backdropVisual.Size = new Vector2((float)e.NewSize.Width, (float)e.NewSize.Height);
    };
}

同时需要在窗口XAML中设置背景透明:

<Window
    x:Class="YourApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:YourApp"
    mc:Ignorable="d"
    Background="Transparent">
    <!-- 窗口内容 -->
</Window>

方案二:结合Win32 DWM API实现

通过P/Invoke调用系统DWM接口,直接启用窗口的HostBackdrop效果:

using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using Windows.UI;

public partial class MainWindow : Window
{
    [DllImport("dwmapi.dll")]
    private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

    private const int DWMWA_USE_HOSTBACKDROPBRUSH = 35;

    public MainWindow()
    {
        InitializeComponent();
        Activated += MainWindow_Activated;
    }

    private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
    {
        // 获取窗口句柄
        var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
        
        // 启用HostBackdropBrush
        int enable = 1;
        DwmSetWindowAttribute(hwnd, DWMWA_USE_HOSTBACKDROPBRUSH, ref enable, sizeof(int));

        // 设置半透明Acrylic背景
        var acrylicBrush = new AcrylicBrush
        {
            BackgroundSource = AcrylicBackgroundSource.Backdrop,
            TintColor = Colors.White,
            TintOpacity = 0.1,
            FallbackColor = Colors.LightGray
        };
        Background = acrylicBrush;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:22:33