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

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

<Style x:Key="for_noresize_window" TargetType="{x:Type Window}">
    <Setter Property="AllowsTransparency" Value="true"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontFamily" Value="Consolas, Microsoft YaHei"/>
    <Setter Property="FontSize" Value="24"/>
    <Setter Property="ResizeMode" Value="NoResize"/>
    <Setter Property="WindowStyle" Value="None"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Margin="10">
                    <Rectangle Fill="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                               RadiusX="5" RadiusY="5">
                        <Rectangle.Effect>
                            <DropShadowEffect BlurRadius="10" ShadowDepth="0"/>
                        </Rectangle.Effect>
                    </Rectangle>
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Margin}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            CornerRadius="5">
                        <ContentPresenter />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Window x:Class="TestProject.Windows.NoResizeWithShadowEffect"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="Transparent" Height="500" Width="500"
        Title="NoResizeWithShadowEffect"
        WindowStartupLocation="CenterScreen"
        Style="{StaticResource for_noresize_window}">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Label Content="NoResizeWithShadowEffect" Foreground="Olive"/>
            <TextBlock Text=""/>
            <Button Padding="20,5" Content="Close Window" Click="Clicked"/>
            <x:Code>
                <![CDATA[
                void Clicked(object sender, RoutedEventArgs e)
                {
                    this.Close();
                }
                ]]>
            </x:Code>
        </StackPanel>
    </Grid>
</Window>


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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控件的阴影效果,可以根据实际需求选择最合适的方法,从而大大提高了应用程序的用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值