没有BitmapEffects的WPF辉光效果

时间:2014-03-06 04:17:15

标签: c# wpf effects

我已经尝试过寻找这个问题的解决方案,但是没找到任何东西。

我有一个带有自定义C ++ / CLI挂钩的WPF窗口来扩展框架(DWMAPI)并将客户区扩展到框架中(Win32 / NCCALCSIZE)。我在WPF中添加了自定义图标和标题。窗口的标记如下(请记住,客户区域的大小调整为玻璃框架的边缘):

<Window x:Class="ClrDwmHelper.WpfHost.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ns="clr-namespace:ClrDwmHelper.WpfHost"
        Title="My Window Title" Height="350" Width="300"
        Background="{x:Null}"
        SourceInitialized="Window_SourceInitialized"
        Loaded="Window_Loaded">
  <Window.Icon>
    <DrawingImage>
      <DrawingImage.Drawing>
        <GeometryDrawing Brush="White">
          <GeometryDrawing.Geometry>
            <RectangleGeometry Rect="0,0 16,16"/>
          </GeometryDrawing.Geometry>
        </GeometryDrawing>
      </DrawingImage.Drawing>
    </DrawingImage>
  </Window.Icon>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="6"/>
      <RowDefinition Height="22"/>
      <RowDefinition Height="1*"/>
      <RowDefinition Height="6"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="6"/>
      <ColumnDefinition Width="1*"/>
      <ColumnDefinition Width="6"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.RowSpan="2" Grid.ColumnSpan="3">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="1*"/>
      </Grid.ColumnDefinitions>
      <Image Name="SysMenu" Height="16" Width="16" Margin="6,7,6,5" Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Icon}"/>
      <Grid Grid.Column="1" Margin="1,6,5,5">
        <TextBlock Name="Caption" IsHitTestVisible="False" Foreground="Black" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Title}"
                   FontFamily="{x:Static SystemFonts.CaptionFontFamily}"
                   FontSize="{x:Static SystemFonts.CaptionFontSize}"
                   FontStretch="Normal"
                   FontStyle="{x:Static SystemFonts.CaptionFontStyle}"
                   TextDecorations="{x:Static SystemFonts.CaptionFontTextDecorations}"
                   FontWeight="{x:Static SystemFonts.CaptionFontWeight}"
                   TextTrimming="WordEllipsis">
          <TextBlock.Effect>
            <DropShadowEffect ShadowDepth="0" Color="#FFFFFF" BlurRadius="15"/>
          </TextBlock.Effect>
        </TextBlock>
      </Grid>
    </Grid>

    <Border Grid.Row="2" Grid.Column="1" BorderBrush="#7FFFFFFF" BorderThickness="1" CornerRadius="1" IsHitTestVisible="False">
      <Border Background="White" BorderBrush="#9F000000" BorderThickness="1" CornerRadius="1" IsHitTestVisible="False">

      </Border>
    </Border>
  </Grid>
</Window>

窗口如下所示:

Image of the window

我希望文本周围有一个更不透明的光晕(文本块),而不是现在(半径为15的DropShadowEffect几乎看不见,但是WINAPI会发光半径为15更不透明)。最好的方法是什么? (自定义效果包含首选)

2 个答案:

答案 0 :(得分:0)

作为临时解决方案,我在TextBlock下面放置了一个模糊的白色半透明矩形。工作正常,虽然这是我想要的东西 - 我希望文字模糊而不必添加矩形。

答案 1 :(得分:0)

据我了解您的要求,我猜您应该使用BlurEffect课程来实现目标。

enter image description here

我创建了一个简单的XAML文件,您可以在本文结尾处找到该文件,其中我使用了DropShadowEffect(第一列)和BlurEffect(第二列)的各种参数。

在BlurEffect的情况下,技巧是包含另一个没有此效果的TextBlock,因此文本仍然可读:

<Grid>
    <!-- Blurred text -->
    <TextBlock Foreground="#FFCCCCCC" Text="My Window Title">
    <TextBlock.Effect>
        <BlurEffect KernelType="Box" Radius="3.5"/>
    </TextBlock.Effect>
    </TextBlock>
    <!-- Crisp text -->
    <TextBlock Text="My Window Title"/>
</Grid>

以下是完整标记:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Tan">
    <Grid Margin="20">
        <StackPanel Orientation="Horizontal">
            <StackPanel Width="200">
                <TextBlock Text="My Window Title"/>
                <TextBlock Text="My Window Title">
                    <TextBlock.Effect>
                        <DropShadowEffect
                            BlurRadius="5"
                            Color="#FFFFFF"
                            Direction="0"
                            ShadowDepth="0"/>
                    </TextBlock.Effect>
                </TextBlock>
                <TextBlock Text="My Window Title">
                    <TextBlock.Effect>
                        <DropShadowEffect
                            BlurRadius="5"
                            Color="#EEEEEE"
                            Direction="0"
                            ShadowDepth="0"/>
                    </TextBlock.Effect>
                </TextBlock>
                <TextBlock Text="My Window Title">
                    <TextBlock.Effect>
                        <DropShadowEffect
                            BlurRadius="2"
                            Color="#777777"
                            Direction="0"
                            ShadowDepth="0"/>
                    </TextBlock.Effect>
                </TextBlock>
                <TextBlock Text="My Window Title">
                    <TextBlock.Effect>
                        <DropShadowEffect
                            BlurRadius="3"
                            Color="#AAAAAA"
                            Direction="0"
                            ShadowDepth="0"/>
                    </TextBlock.Effect>
                </TextBlock>
            </StackPanel>
            <StackPanel>
                <TextBlock Text="My Window Title"/>
                <Grid>
                    <TextBlock Foreground="#FFFFFFFF" Text="My Window Title">
                        <TextBlock.Effect>
                            <BlurEffect KernelType="Box" Radius="2.0"/>
                        </TextBlock.Effect>
                    </TextBlock>
                    <TextBlock Text="My Window Title"/>
                </Grid>
                <Grid>
                    <TextBlock Foreground="#99FFFFFF" Text="My Window Title">
                        <TextBlock.Effect>
                            <BlurEffect KernelType="Box" Radius="3.0"/>
                        </TextBlock.Effect>
                    </TextBlock>
                    <TextBlock Text="My Window Title"/>
                </Grid>
                <Grid>
                    <TextBlock Foreground="#BB999999" Text="My Window Title">
                        <TextBlock.Effect>
                            <BlurEffect KernelType="Box" Radius="5.0"/>
                        </TextBlock.Effect>
                    </TextBlock>
                    <TextBlock Text="My Window Title"/>
                </Grid>
                <Grid>
                    <TextBlock Foreground="#FFCCCCCC" Text="My Window Title">
                        <TextBlock.Effect>
                            <BlurEffect KernelType="Box" Radius="3.5"/>
                        </TextBlock.Effect>
                    </TextBlock>
                    <TextBlock Text="My Window Title"/>
                </Grid>
            </StackPanel>
        </StackPanel>
    </Grid>
</Page>