【问题标题】:wpf: custom window drop shadowwpf:自定义窗口阴影
【发布时间】:2010-08-21 22:42:28
【问题描述】:

我正在开发一个带有自定义窗口的 c# wpf 应用程序(allowtransparency = true,resize = none,window style = none)。

现在我想添加类似于 zune pc 软件的投影。我阅读了这篇文章,其中包含的阴影效果并没有覆盖我窗口的所有角度,据说它会影响性能。

我想像这样实现它:我在布局网格中添加一个边距,我在最大化应用程序时以编程方式将其删除。

添加可应用于网格的阴影的最佳方法是什么,既不会影响性能,也不会影响所有方向的阴影?

【问题讨论】:

    标签: wpf


    【解决方案1】:

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

    现在好了,驱动这个效果的代码:

    <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>
    

    【讨论】:

      【解决方案2】:

      DropShadowEffect 不会“扼杀性能”...它是使用硬件加速渲染的,并且在窗口上渲染阴影对于当前的 GPU 来说并不是什么大问题。您可能对DropShadowBitmapEffect 感到困惑,它是软件渲染的。无论如何,所有 BitmapEffects 在 3.5 SP1 中都已过时,在 4.0 中根本无法使用,现在只能使用 Effects

      【讨论】:

      • 那么我在我的网格上使用阴影效果吗?我怎样才能让它在各个方向上投下阴影,还是我必须应用多种效果..?
      【解决方案3】:

      -75 的方向、2 的 ShadowDepth 和 27 的 BlurRadius 对我有帮助。

      最好的方法是使用 blend 来做这些。

      HTH

      【讨论】:

      • 只是想指出 - 2 的 ShadowDepth 在这里产生了惊人的巨大差异,即使有如此大的模糊。您可以通过将 ShadowDepth 设置为 0 来获得完全居中的“阴影”(实际上更像是外发光),但它绝对不会弹出,即使在大型物体上,2 的 ShadowDepth 也将其牢固地建立为阴影而不是轮廓.
      【解决方案4】:

      基于 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>
      

      【讨论】:

      • 这没有考虑窗口镶边,这在实践中很糟糕。
      猜你喜欢
      • 2019-01-10
      • 2012-11-02
      • 1970-01-01
      • 2013-05-19
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      相关资源
      最近更新 更多