WPF中阴影效果和模糊效果的使用

总目录



前言

WPF中的控件效果主要通过Effect来实现,而Effect有DropShadowEffect(投影效果)和BlurEffect(模糊效果)两个派生类,本文将主要介绍Effect的运用!


一、DropShadowEffect

1、DropShadowEffect各属性效果图

在这里插入图片描述
另外还有两个常用的属性,没有在上图介绍

  • Color 属性 设置阴影的颜色
  • Opacity 属性 设置阴影的透明度

2、实现代码

    <UniformGrid Background="LightGreen" Columns="4">
        <StackPanel Margin="20">
            <TextBlock Text="Direction=0" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="0" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=90" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="90" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=180" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="180" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=270" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="270" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Direction=360" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Black" Direction="360" ShadowDepth="10"/>
                </TextBlock.Effect>
            </TextBlock>

        </StackPanel>
        <StackPanel Margin="20" Background="White">
            <TextBlock Text="ShadowDepth=0" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="0"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="ShadowDepth=20" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="20"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="ShadowDepth=40" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="40"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="ShadowDepth=80" Margin="0,20">
                <TextBlock.Effect>
                    <DropShadowEffect Color="Red" Direction="0" ShadowDepth="80"/>
                </TextBlock.Effect>
            </TextBlock>
            
        </StackPanel>
        <StackPanel>
            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="0" BlurRadius="20"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=0 BlurRadius=20" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>

            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="0" BlurRadius="40"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=0 BlurRadius=40" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>

            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="10" BlurRadius="60"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=10 BlurRadius=60" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>

            <Border Height="80" Width="200" CornerRadius="25" Background="Green" Margin="0,20">
                <Border.Effect>
                    <DropShadowEffect Color="Blue" Direction="0" ShadowDepth="100" BlurRadius="120"/>
                </Border.Effect>
                <TextBlock Text="Direction=0 ShadowDepth=100 BlurRadius=120" FontSize="14" VerticalAlignment="Center" TextWrapping="Wrap" Width="150"></TextBlock>
            </Border>
        </StackPanel>
    </UniformGrid>

二、BlurEffect

1、BlurEffect各属性效果图

在这里插入图片描述

2、实现代码

    <UniformGrid Background="LightGreen" Columns="3">
        <StackPanel Margin="20">
            <TextBlock Text="Radius=0   KernelType=Box" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="0"  KernelType="Box"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=1   KernelType=Box" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="1"  KernelType="Box"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=2   KernelType=Box" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="2"  KernelType="Box"/>
                </TextBlock.Effect>
            </TextBlock>
        </StackPanel>
        <StackPanel Margin="20">
            <TextBlock Text="Radius=0   KernelType=Gaussian" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="0"  KernelType="Gaussian"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=1   KernelType=Gaussian" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="1"  KernelType="Gaussian"/>
                </TextBlock.Effect>
            </TextBlock>
            <TextBlock Text="Radius=2   KernelType=Gaussian" Margin="0,10">
                <TextBlock.Effect>
                    <BlurEffect Radius="2"  KernelType="Gaussian"/>
                </TextBlock.Effect>
            </TextBlock>
        </StackPanel>

    </UniformGrid>

三、进阶使用

详情可查:[WPF] 使用 Effect 玩玩阴影、内阴影、 长阴影

四、注意事项

1、 由于使用Effect 导致字体模糊

在这里插入图片描述

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">

            <Border Width="200" Height="100" Background="#5Eb978">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="10" ShadowDepth="0"/>
                </Border.Effect>
                <TextBlock Text="这个是错误示范" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"></TextBlock>
            </Border>

            <Grid Width="200" Height="100" Margin="30,0,0,0">
                <Border Width="200" Height="100" Background="#5Eb978">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="10" ShadowDepth="0"/>
                    </Border.Effect>
                </Border>
                <TextBlock Text="正确示范" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="25"></TextBlock>
            </Grid>


        </StackPanel>

提示:当对Border 使用 Effect 的时候,最好不要在Border 内部添加TextBlock等展示元素,否则会造成 TextBlock 模糊。


结语

以上就是本文的内容,希望以上内容可以帮助到您,如文中有不对之处,还请批评指正。


参考资料:
WPF 实现阴影效果
[WPF] 使用 Effect 玩玩阴影、内阴影、 长阴影
WPF Effect 造成的字体模糊

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值