Windows 11下WPF自定义无边框窗口实现原生圆角与DWM系统阴影的问题求助
Windows 11下WPF自定义无边框窗口实现原生圆角与DWM系统阴影的问题求助
大家好,我正在开发一个WPF自定义Chrome风格应用,想要让它呈现出类似Teams这种微软现代应用的质感——带圆角,并且拥有Windows 11系统DWM绘制的原生阴影(不是WPF自带的DropShadowEffect那种内部模拟的阴影)。但折腾了好一阵,系统原生阴影始终出不来,只能用自己加的DropShadowEffect凑数,效果和原生的自然阴影差得很远。
我想要的是那种和系统自带窗口一致的、外层的柔和原生阴影,而现在实际显示的是我手动添加的生硬灰色内部阴影。
下面是我当前的窗口XAML代码:
<Window x:Class="NG_TAM.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:WindowsResizingLib="clr-namespace:WindowsResizingLib;assembly=WindowsResizingLib" xmlns:local="clr-namespace:NG_TAM" xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework" Title="Demo" Height="900" Width="1600" MouseLeftButtonDown="Window_MouseLeftButtonDown" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip" WindowStyle="None" WindowState="Normal" AllowsTransparency="True" BorderThickness="3" > <Window.Effect> <DropShadowEffect Color="Gray" BlurRadius="20" Direction="-90" RenderingBias="Quality" ShadowDepth="4"/> </Window.Effect> <WindowChrome.WindowChrome> <WindowChrome GlassFrameThickness="0" CornerRadius="4" CaptionHeight="0" UseAeroCaptionButtons="False" /> </WindowChrome.WindowChrome> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Rectangle x:Name="BackgroundRect" Grid.ColumnSpan="2" Grid.RowSpan="5" Height="auto" Width="auto" Fill="#DCE2E8" RadiusX="4" RadiusY="4"> <Rectangle.Effect> <DropShadowEffect Color="Gray" BlurRadius="12" ShadowDepth="4" Opacity="0.15"/> </Rectangle.Effect> </Rectangle> <Border x:Name="MenuBar" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Height="65" Background="White" CornerRadius="4,4,0,0" > <Border.Effect> <DropShadowEffect Opacity="0.20"/> </Border.Effect> </Border> <WindowsResizingLib:WindowsButtons DataContext="{Binding WindowsButtonsVm}" ColorOfButtons="Blue" Height="{Binding WindowsButtonsVm.WindowStyleButtonsHeight}" Width="{Binding WindowsButtonsVm.WindowStyleButtonsWidth}" Grid.Row="0" Grid.Column="4" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,20,0"/> </Grid> </Window>
目前我设置了WindowStyle="None"、AllowsTransparency="True",用WindowChrome实现了圆角,但DWM的原生阴影就是不出现。我怀疑是不是AllowsTransparency或者WindowChrome的某些属性设置冲突了?
有没有大佬能指点一下,怎么调整才能让这个自定义无边框窗口显示Windows 11的DWM原生阴影?感激不尽!




