[WPF] 如何为无边框窗口设置阴影效果

本文介绍如何使用 WPF 创建一个具有自定义阴影效果且不可调整大小的无边框窗口。通过设置窗口样式、字体及背景透明度等属性,并自定义窗口模板来实现美观的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要将窗口样式添加到 App.xaml 中

[html]  view plain copy
  1. <Style x:Key="for_noresize_window" TargetType="{x:Type Window}">  
  2.     <Setter Property="AllowsTransparency" Value="true"/>  
  3.     <Setter Property="Background" Value="Transparent"/>  
  4.     <Setter Property="FontFamily" Value="Consolas, Microsoft YaHei"/>  
  5.     <Setter Property="FontSize" Value="24"/>  
  6.     <Setter Property="ResizeMode" Value="NoResize"/>  
  7.     <Setter Property="WindowStyle" Value="None"/>  
  8.     <Setter Property="Template">  
  9.         <Setter.Value>  
  10.             <ControlTemplate TargetType="{x:Type Window}">  
  11.                 <Grid Margin="10">  
  12.                     <Rectangle Fill="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"  
  13.                                RadiusX="5" RadiusY="5">  
  14.                         <Rectangle.Effect>  
  15.                             <DropShadowEffect BlurRadius="10" ShadowDepth="0"/>  
  16.                         </Rectangle.Effect>  
  17.                     </Rectangle>  
  18.                     <Border Background="{TemplateBinding Background}"  
  19.                             BorderBrush="{TemplateBinding BorderBrush}"  
  20.                             BorderThickness="{TemplateBinding BorderThickness}"  
  21.                             Padding="{TemplateBinding Margin}"  
  22.                             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  
  23.                             CornerRadius="5">  
  24.                         <ContentPresenter />  
  25.                     </Border>  
  26.                 </Grid>  
  27.             </ControlTemplate>  
  28.         </Setter.Value>  
  29.     </Setter>  
  30. </Style>  

[html]  view plain copy
  1. <Window x:Class="TestProject.Windows.NoResizeWithShadowEffect"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Background="Transparent" Height="500" Width="500"  
  5.         Title="NoResizeWithShadowEffect"  
  6.         WindowStartupLocation="CenterScreen"  
  7.         Style="{StaticResource for_noresize_window}">  
  8.     <Grid>  
  9.         <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">  
  10.             <Label Content="NoResizeWithShadowEffect" Foreground="Olive"/>  
  11.             <TextBlock Text=""/>  
  12.             <Button Padding="20,5" Content="Close Window" Click="Clicked"/>  
  13.             <x:Code>  
  14.                 <![CDATA[ 
  15.                 void Clicked(object sender, RoutedEventArgs e) 
  16.                 { 
  17.                     this.Close(); 
  18.                 } 
  19.                 ]]>  
  20.             </x:Code>  
  21.         </StackPanel>  
  22.     </Grid>  
  23. </Window>  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值