Windows 10 应用创建模糊背景窗口的三种方法

简介: 原文 Windows 10 应用创建模糊背景窗口的三种方法 现代的操作系统中创建一张图片的高斯模糊效果非常容易,不过如果要在窗口中获得模糊支持就需要操作系统的原生支持了。iOS/Mac 和 Windows 系统都对此有支持。

原文 Windows 10 应用创建模糊背景窗口的三种方法

现代的操作系统中创建一张图片的高斯模糊效果非常容易,不过如果要在窗口中获得模糊支持就需要操作系统的原生支持了。iOS/Mac 和 Windows 系统都对此有支持。

本文将介绍三种创建模糊背景窗口的方法。有人可能喜欢称之为毛玻璃窗口、亚克力窗口。


This post is written in multiple languages. Please select yours:

 

最早我是在 StackOverflow 上回答一位网友的提问时写了一份非常长的答案,后来小伙伴建议我将答案改写成博客,于是我就改了。StackOverflow 上的答案在这里:colors - WPF: How to Style my form with Transparency levels - Stack Overflow

三种创建模糊背景窗口的方法

Windows 10 上创建带模糊背景的窗口有三种不同的方法,不过每一种都是既有好处又有坏处的:

  1. 调用 Win32 API —— SetWindowCompositionAttribute,使用这种方式能够获得一个背景轻微透明的窗口。当然,如果需要模拟亚克力效果或者是 iOS/Mac 上的模糊效果就 gg 了。
    The image from my post

  2. 为窗口中的背景图片添加 WPF 自带的模糊效果 BlurEffect。这种方式你想获得多大的模糊半径就能获得多大的模糊半径,不过带来的就是更高的性能损耗。同时,还得考虑在移动窗口的时候动态地去更新背景图片并再次模糊。
    BlurEffect of WPF

  3. 使用 Fluent Design System 中的亚克力效果 —— AcrylicBrush。这绝对是 Windows 10 上获得背景模糊效果中视觉效果最好,同时又最省性能的方法了。不过,这种方法只能在 UWP 应用中使用。
    The UWP AcrylicBrush from docs.microsoft.com


SetWindowCompositionAttribute API

SetWindowCompositionAttribute 并没有那么好调用,所以我为此写了一个辅助类类封装对背景模糊效果的调用。使用这个辅助类,你只需要使用一行代码就能开启背景模糊效果。

可以在 XAML 代码中使用 interop:WindowBlur.IsEnabled="True"

<Window x:Class="Walterlv.Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:interop="clr-namespace:Walterlv.Demo.Interop" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" interop:WindowBlur.IsEnabled="True" Background="Transparent"> </Window> 

可以在 cs 代码中使用 WindowBlur.SetIsEnabled(this, true)

public class MainWindow : Window { public MainWindow() { InitializeComponent(); WindowBlur.SetIsEnabled(this, true); } } 

我为 WindowBlur 类准备了一个 GitHub Gist,在这里:https://gist.github.com/walterlv/752669f389978440d344941a5fcd5b00。你只需要将代码全部复制到你的项目中即可开始使用。

当然,我还写了一篇博客专门讲使用 SetWindowCompositionAttribute API 实现背景模糊效果:在 Windows 10 上为 WPF 窗口添加模糊特效(就像开始菜单和操作中心那样)

WPF BlurEffect

WPF 的 UIElement 都有 Effect 属性,将其设置为 BlurEffect 即可获得控件的高斯模糊效果。

<Window x:Class="MejirdrituTeWarqoudear.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" AllowsTransparency="True" WindowStyle="None" Width="540" Height="360"> <Grid> <Image Source="YourImageFile.jpg" Stretch="Fill" Margin="-60"> <Image.Effect> <BlurEffect KernelType="Gaussian" Radius="60" /> </Image.Effect> </Image> <Border CornerRadius="60" Margin="30" Background="#7F000000"> <TextBlock Foreground="White" FontSize="20" FontWeight="Light" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"> <Run Text="Hello World" FontSize="48"/> <LineBreak/> <Run Text="walterlv.github.io"/> </TextBlock> </Border> </Grid> </Window> 

特别注意:此方法有严重地性能问题

如果你的窗口是一个异形窗口,例如是具有圆角的矩形,那么你需要额外为控件设置 RectangleGeometry 来裁剪控件。

Rounded Rectangle

<Window x:Class="MejirdrituTeWarqoudear.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="540" Height="360"> <Grid> <Grid.Clip> <RectangleGeometry RadiusX="60" RadiusY="60" Rect="30 30 480 300" /> </Grid.Clip> <Image Source="High+Sierra.jpg" Stretch="Fill" Margin="-60"> <Image.Effect> <BlurEffect KernelType="Gaussian" Radius="60" /> </Image.Effect> </Image> <Border Background="#7F000000"> <TextBlock Foreground="White" FontSize="20" FontWeight="Light" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"> <Run Text="Hello World" FontSize="48"/> <LineBreak/> <Run Text="walterlv.github.io"/> </TextBlock> </Border> </Grid> </Window> 

如果是圆形窗口,我另外写了一篇文章来说明进行圆形裁剪:WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆)

UWP AcyclicBrush

微软的官方文档 Acrylic material - UWP app developer - Microsoft Docs 讲解了如何使用亚克力效果。

本文会经常更新,请阅读原文: https://walterlv.com/post/create-blur-background-window.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。 

目录
相关文章
|
13天前
|
消息中间件 编译器 API
Windows窗口程序
Windows窗口程序
|
3月前
|
安全 Windows
windows11 永久关闭windows defender的方法
windows11 永久关闭windows defender的方法
279 2
|
3月前
|
Ubuntu Linux Windows
两种Ubuntu和Windows互相共享文件夹的方法
两种Ubuntu和Windows互相共享文件夹的方法
|
1月前
|
数据可视化 数据库 C++
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
|
1月前
|
安全 Windows
关闭Windows自动更新的6种方法
关闭Windows自动更新的6种方法
149 0
|
2月前
|
Windows
不让Windows显示语言栏“中”“英”字符的一种方法
【2月更文挑战第6天】本文介绍在Windows 11操作系统中,将任务栏右下角的语言栏的“中”、“英”标识加以隐藏、消除的一种或许可行的方法~
不让Windows显示语言栏“中”“英”字符的一种方法
|
2月前
|
安全 Linux iOS开发
上传 iOS 应用变得更加容易 - 在 Windows 上架 iOS APP 的工具介绍
上传 iOS 应用变得更加容易 - 在 Windows 上架 iOS APP 的工具介绍
|
3月前
|
存储 Kubernetes 安全
虚拟机测试Windows Server 2016原地升级2019,应用和数据完美保留
Windows Server 2016可以无缝升级到2019版本,确保应用程序和数据在原地升级过程中完整保留。
104 0
|
3月前
|
Linux iOS开发 Windows
windows 如何上架 ios 应用到 app store
windows 如何上架 ios 应用到 app store
|
3月前
|
消息中间件 Java Kafka
windows下kafka的环境配置及rdkafka库的应用
windows下kafka的环境配置及rdkafka库的应用