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

需要将窗口样式添加到 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.                  
  15.                 void Clicked(object sender, RoutedEventArgs e) 
  16.                 { 
  17.                     this.Close(); 
  18.                 } 
  19.                 ]]>  
  20.             x:Code>  
  21.         StackPanel>  
  22.     Grid>  

  1. Window>  

转载地址:http://blog.csdn.net/qqamoon/article/details/6912513

你可能感兴趣的:(C#,WPF)