WPF: 自定义窗口阴影效果

6
我正在开发一个带有自定义窗口的c# wpf应用程序(allowtransparency = true,resize = none,window style = none)。
现在我想添加与zune pc软件类似的投影。我查阅了相关资料,发现包含的dropshadoweffect无法覆盖我的窗口所有角度,并且据说会降低性能。
我想实现的方法是:我向布局网格添加一个边距,在最大化应用程序时以编程方式将其删除。
如何最好地添加投影,以应用于网格,不会降低性能并在所有方向上产生投影?
4个回答

12

我尝试了这里发布的解决方案,但都未能让我接近我想要的最终结果(见下面的截图)。所以,我尝试了几种不同的方法,并在此发布我的解决方案,以防某人有兴趣实现类似的功能。顺便说一句:如果你能改进我的解决方案,请告诉我,因为我觉得它现在有点多余。

带蓝色投影效果的窗口

现在来看一下产生此效果的代码:

    <Window ...
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        ...>

    <Border>
        <Border.Effect>
            // opacity does not need to be specified but it looks cooler when you do
            <DropShadowEffect BlurRadius="20" ShadowDepth="0" Opacity="0.8" 
                Color="Blue" />
        </Border.Effect>

        // make sure the value for Grid Margin is the same as DropShadowEffect 
        // BlurRadius
        <Grid Background="White" Margin="20">

            // I tried setting borderthickness and borderbrush to the previous 
            // <Border> element but instead of the border being shown right after  
            // the grid and before the drop shadow, it would show after the drop 
            // shadow making the overall effect very ugly
            <Border BorderThickness="1" BorderBrush="Black">
               // now you can specify whatever you want to display in the window
                <Grid>
                    ....
                </Grid>
            </Border>
       </Grid>
</Window>

6

DropShadowEffect不会“影响性能”……它是通过硬件加速渲染的,而在窗口上渲染投影阴影对于当前的GPU来说并不是什么大问题。你可能将其与DropShadowBitmapEffect混淆了,后者是软件渲染的。无论如何,所有的BitmapEffects在3.5 SP1中都已经过时,在4.0中根本无法使用,现在只能使用Effects


那我在我的网格上使用dropshadoweffect?我怎样才能让它在所有方向上都有阴影,还是我必须应用多个效果..? - internetmw

4

对于我来说,方向为-75度,阴影深度为2,模糊半径为27是有帮助的。

最好的方法是使用混合来完成这些操作。

希望有所帮助。


2
只是想指出 - ShadowDepth为2在这里产生了惊人的差异,即使有如此大的模糊。将ShadowDepth设置为0可以获得完全居中的“阴影”(实际上更像是外发光),但是它绝对没有弹性,而即使在大型物体上,ShadowDepth为2也可以将其牢固地确立为阴影而不是轮廓线。 - Ben

2
建立在Princes的代码基础上,我想粘贴最终产品。
<Window x:Class="RDNScoreboard.Views.InitialWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="InitialWindow" Height="300" Width="300"
    WindowStyle="None"  
AllowsTransparency="True" Background="Transparent"
   BorderThickness="3"  >
<Border>
    <Border.Effect>
        <DropShadowEffect BlurRadius="27" Color="Black" Opacity="0.8" ShadowDepth="2" Direction="-75" />
    </Border.Effect>
    <Grid Background="White"  >
    </Grid>
</Border>


1
这并不考虑窗口的边框,实际上非常糟糕。 - user1618054

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接