模糊WPF容器的背景

发布于 2024-12-10 15:57:23 字数 567 浏览 0 评论 0原文

最终,我想要实现的是在某种程度上复制 WPF 内容控件的 Aero glass 功能。

如果我将 BlurEffect 应用于包含 TextBlockStackPanel,我会将 TextBlock 的文本模糊化。

考虑一个例子:

无模糊

no Blu

with Blur

但是有没有一种 WPF 方法可以模糊面板后面的背景,而不是它的内容?

StackPanel 的背景是桌面,并且托管它的窗口设置为 AllowTransparency="True" 以允许自定义形状的外观。

Ultimately, what I want to achieve is a replication to some extend, of an Aero glass functionality of a WPF content control.

If I apply the BlurEffect to a StackPanel that contains a TextBlock, I will have the TextBlock's text blurred.

Consider an examples:

No blur

no blur

and with <BlurEffect Radius="5" KernelType="Gaussian"/>

with blur

But is there a WPF way to blur the background behind the panel, and not it's contents?

The background of the StackPanel is a desktop, and the window that hosts it is set to AllowTransparency="True" to allow the custom-shaped look.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云雾 2024-12-17 15:57:23

不,这是不可能的。该效果应用于元素及其所有子元素,但您可以轻松地将 TextBlock 放置在容器外部,而不是容器内部。

通常你会使用像这样的网格:

<Grid>
  <Border>
     <Border.Effect>
      <BlurEffect Radius="5" KernelType="Gaussian"/>
     </Border.Effect/>
  </Border>
  <TextBlock .../>
</Grid>

在你的例子中,这没有什么区别。你到底想模糊什么?

no, it is not possible. The Effect is applied to the element and all its children but you can easily place the TextBlock outside the container, rather than inside it.

Normally you would use a grid like so:

<Grid>
  <Border>
     <Border.Effect>
      <BlurEffect Radius="5" KernelType="Gaussian"/>
     </Border.Effect/>
  </Border>
  <TextBlock .../>
</Grid>

In your example that will make no difference though. What, exactly, are you trying to blur?

最近可好 2024-12-17 15:57:23

StackPanel的背景是什么?图像画笔?如果是这样,为什么不能应用 BlurEffect 呢?

如果这不可能,那么尝试这个..

1] 使用图像并将其完全绘制在 Grid 上,因为我发现您不需要 TileEffect。将 BlurEffect 添加到此 Image。确保均匀地填充图像。

2] 然后添加具有透明背景的StackPanel作为Grid中的下一个子,即不要颠倒图像和stackpanel的顺序。

3]然后在StackPanel中添加TextBlock

或者

如果您坚持使用 Brush 设置为面板的背景,则使用 VisualBrush 绘制模糊图像作为堆栈面板的背景,而不是 ImageBrush

如果这些提示有帮助,请告诉我。

What the background of the StackPanel? an ImageBrush? If so why cant you apply BlurEffect to that?

If that is not possible then try this..

1] Use an image and draw it completely over Grid as I see you dont want a TileEffect. Add BlurEffect to this Image. Make sure you fill image the uniformly.

2] Then add StackPanel with transparent background as next child in the Grid i.e. dont reverse the order of image and stackpanel.

3] Then add TextBlock in StackPanel.

OR

If you insist on using a Brush to be set as the backgrounnd of the panel then use VisualBrush that draws a blurred image as background of the stackpanel, instead of ImageBrush.

Let me know if any of these tips help.

遇见了你 2024-12-17 15:57:23

您可以在 Systems.Window 上使用 SetWindowCompositionAttribute,但随后您必须将 WindowStyle 设置为“None”并实现您自己的本机 Window 功能和句柄。从自定义控件继承也是相当复杂的。长话短说,有 BlurryControls。

您可以通过 NuGet 浏览“BlurryControls”找到它,或者在

在 GitHub 上,您还可以找到一个名为 BlurryWindowInvoker 的示例应用程序。

You can use the SetWindowCompositionAttribute on a Systems.Window, but you are then forced to set the WindowStyle to "None" and implement your own native Window funtionality and handles. Also inheriting from a custom control is quite complicated. Long story short, there's BlurryControls.

You can find it via NuGet by browsing for "BlurryControls" or check out the code yourself on GitHub. Eitherway, I hope this is helpful. It uses .NET 4.5.2 and only works for Windows10, since there is no solution to this problem on Windows8, and in earlier versions (Windows7 and Vista) you can achieve this by accessing DwmEnableBlurBehindWindow.

On GitHub you will also find a sample application called BlurryWindowInvoker.

下雨或天晴 2024-12-17 15:57:23

您可以将其应用于网格。

<Grid.Background>
   <VisualBrush>
       <VisualBrush.Visual>
          <Image Source="RESOURCES/BACKGROUNDS/BACKGROUND_01.jpg">
             <Image.BitmapEffect>
                <BlurBitmapEffect KernelType="Gaussian" Radius="20" />
             </Image.BitmapEffect>
          </Image>
       </VisualBrush.Visual>
    </VisualBrush>
 </Grid.Background>

You could apply this to a Grid.

<Grid.Background>
   <VisualBrush>
       <VisualBrush.Visual>
          <Image Source="RESOURCES/BACKGROUNDS/BACKGROUND_01.jpg">
             <Image.BitmapEffect>
                <BlurBitmapEffect KernelType="Gaussian" Radius="20" />
             </Image.BitmapEffect>
          </Image>
       </VisualBrush.Visual>
    </VisualBrush>
 </Grid.Background>
⒈起吃苦の倖褔 2024-12-17 15:57:23

我的五分钱,已经晚了:

您使用控件本身作为模板并从父容器复制图像,现在您可以模糊它并在顶部显示内容:

在此处输入图像描述

            <Grid Height="400" x:Name="MainGrid">
                <Grid.Background>
                    <ImageBrush ImageSource="/Images/nyc.jpg" Stretch="UniformToFill"/>
                </Grid.Background>

                <Border VerticalAlignment="Center" Margin="50" BorderBrush="Black" BorderThickness="5" CornerRadius="10">
                    <Grid>
                        
                    <Grid>
                        <Grid.Effect>
                            <BlurEffect Radius="10"/>
                        </Grid.Effect>
                        <Rectangle Margin="-3">
                            <Rectangle.Fill>
                                <VisualBrush Visual="{Binding ElementName=MainGrid}" Stretch="None" />
                            </Rectangle.Fill>
                            <Rectangle.CacheMode>
                                <BitmapCache/>
                            </Rectangle.CacheMode>
                        </Rectangle>

                    </Grid>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40" Foreground="White">
                            New York City!
                        </TextBlock>
                    </Grid>
                </Border>
            </Grid>

My Five cents, years to late:

You use the control itself as stencil and copy the image from the parent container, now you can blurr it and show things on top:

enter image description here

            <Grid Height="400" x:Name="MainGrid">
                <Grid.Background>
                    <ImageBrush ImageSource="/Images/nyc.jpg" Stretch="UniformToFill"/>
                </Grid.Background>

                <Border VerticalAlignment="Center" Margin="50" BorderBrush="Black" BorderThickness="5" CornerRadius="10">
                    <Grid>
                        
                    <Grid>
                        <Grid.Effect>
                            <BlurEffect Radius="10"/>
                        </Grid.Effect>
                        <Rectangle Margin="-3">
                            <Rectangle.Fill>
                                <VisualBrush Visual="{Binding ElementName=MainGrid}" Stretch="None" />
                            </Rectangle.Fill>
                            <Rectangle.CacheMode>
                                <BitmapCache/>
                            </Rectangle.CacheMode>
                        </Rectangle>

                    </Grid>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40" Foreground="White">
                            New York City!
                        </TextBlock>
                    </Grid>
                </Border>
            </Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文