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

13 篇文章 0 订阅

需要将窗口样式添加到 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>  

  1. </Window>  

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中的Popup控件是一个常用的容器控件,在实现各种功能时经常用到。而对于Popup控件的阴影设置,可以通过以下几种方式实现: 1. 使用DropShadowEffect WPF中可以通过DropShadowEffect效果给控件添加阴影,可以使用以下代码将阴影效果添加到Popup控件中: ``` <Popup Name="MyPopup" Width="300" Height="250"> <Border Background="White" BorderThickness="2" BorderBrush="Gray"> <Border.Effect> <DropShadowEffect Color="Gray" BlurRadius="15" Opacity=".75" ShadowDepth="5" /> </Border.Effect> <!-- 添加Popup控件内容 --> </Border> </Popup> ``` 2. 添加阴影Border 除了使用DropShadowEffect效果,还可以通过在Popup控件外部添加一个带阴影的Border元素来实现阴影效果。以下是示例代码: ``` <Border Background="Transparent" BorderThickness="0"> <Border Background="White" BorderThickness="2" BorderBrush="Gray"> <Popup Name="MyPopup" Width="300" Height="250"> <!-- 添加Popup控件内容 --> </Popup> </Border> <Border.Effect> <DropShadowEffect Color="Gray" BlurRadius="15" Opacity=".75" ShadowDepth="5" /> </Border.Effect> </Border> ``` 3. 使用VisualBrush 还可以通过使用VisualBrush将Popup控件的内容绘制到Canvas上,并在Canvas上添加阴影效果来实现。以下是示例代码: ``` <Popup Name="MyPopup" Width="300" Height="250"> <!-- 定义VisualBrush --> <Popup.Resources> <VisualBrush x:Key="PopupContent"> <VisualBrush.Visual> <ContentControl Content="{Binding ElementName=MyPopup, Path=Child}"/> </VisualBrush.Visual> </VisualBrush> </Popup.Resources> <Canvas> <!-- 绘制Popup内容 --> <Rectangle Fill="{StaticResource PopupContent}" Width="{Binding ActualWidth, ElementName=MyPopup}" Height="{Binding ActualHeight, ElementName=MyPopup}" /> <!-- 添加阴影效果 --> <Rectangle Fill="Gray" Opacity="0.5" Width="{Binding ActualWidth, ElementName=MyPopup}" Height="{Binding ActualHeight, ElementName=MyPopup}"> <Rectangle.Effect> <DropShadowEffect BlurRadius="15" Opacity=".75" ShadowDepth="5" Direction="-90" /> </Rectangle.Effect> </Rectangle> </Canvas> </Popup> ``` 通过以上几种方式实现Popup控件的阴影效果,可以根据实际需求选择最合适的方法,从而大大提高了应用程序的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值