2011-02-11 88 views
7

我使用WPF 3.5 SP1和我想要实现这样的事情(在玻璃部已经完成),在玻璃上的标签/ TextBlock的可读:制作采用模糊效果

http://lh3.ggpht.com/_0B12UOQNmgU/SwPqI4FcYaI/AAAAAAAAASM/HKFPyZ9uX3o/Aero%20Glass.jpg
Source


Source

你可以看到周围的文字,这使得它非常好可读性不错模糊。我还发现正确的方法是使用API​​ DrawThemeTextEx,它使用推荐的系统选项渲染模糊。但是,如何使用WPF实现相同的效果?

我能找到包含有用的资源这些链接:
How to make WPF text on Aero glass background readable?
Glowing Label Controls On A Glass Surface

他们通过复制的TextBlock,并设置上有一个模糊效果做到这一点。但是,这不是一个真正的解决方案。下面是它的样子:

http://i53.tinypic.com/2ly67nq.png

比较结果效果与图像上面,你会看到的解决方案还很遥远。那么如何才能正确使用WPF获得理想的效果?我很喜欢模拟(没有使用DrawThemeTextEx API),只要结果非常相似。

谢谢。

回答

7
<TextBlock ...> 
     <TextBlock.Effect> 
      <DropShadowEffect BlurRadius="10" Color="White" ShadowDepth="0" /> 
     </TextBlock.Effect> 
    </TextBlock> 
+0

谢谢,这是一个正确的方法。在最后的解决方案中,我使用了3个嵌套的装饰器,DropShadowEffect +克隆的TextBlock和BlurEffect,结果令人印象深刻。 – Paya 2011-02-11 20:05:09

+0

Paja,你能否提供一个关于你如何嵌套DropShadowEffect的答案?我不确定如何使用TextBlock来做到这一点,因为我对Decorator不太了解。 – Luke 2011-05-26 12:49:39

2

沿着这些线条的东西如果你的文字背后有一个矩形稍微有点模糊,我已经使用了这几次。我发现它使得它更具可读性,因为模糊覆盖了更大的区域。

  <Grid> 
       <Rectangle Fill="#8FFFFFFF" 
          Stroke="{x:Null}" 
          StrokeThickness="0" 
          VerticalAlignment="Center" 
          Width="{Binding ActualWidth, ElementName=PART_Title, Mode=Default}" 
          Height="{Binding ActualHeight, ElementName=PART_Title, Mode=Default}" 
          RadiusX="2" 
          RadiusY="2"> 
        <Rectangle.Effect> 
         <BlurEffect Radius="10" /> 
        </Rectangle.Effect> 
       </Rectangle> 

       <TextBlock x:Name="PART_Title" 
          Text="{Binding Title}" 
          Foreground="Black" 
          TextWrapping="NoWrap" 
          TextTrimming="CharacterEllipsis" /> 
      </Grid> 
+0

好是的,那是有效的,但我希望尽可能地模拟操作系统的行为。也许堆叠50个模糊TextBlocks可以做到这一点,但必须有更好的方法。 – Paya 2011-02-11 18:47:24

7

作为每Luke的要求,我包括XAMLDecorator S:

<Decorator> 
    <Decorator.Effect> 
     <DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" /> 
    </Decorator.Effect> 
    <Decorator> 
     <Decorator.Effect> 
      <DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" /> 
     </Decorator.Effect> 
     <Decorator> 
      <Decorator.Effect> 
       <DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" /> 
      </Decorator.Effect> 

      <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
        Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
     </Decorator> 
    </Decorator> 
</Decorator> 

我创建了一个ControlTemplate用于与前面提到的XAML一个Label,到处都用它我需要文字发光。

1

我在实现装饰器时遇到了麻烦,因为它站在Paya的答案中,所以我展示了它如何包装成完整的,随时可用的样式资源,可以应用于任何标签,这将显示玻璃效果,也将暗淡标签时禁用并保留对齐方式,边框等:

<Style x:Key="GlassLabelStyle" TargetType="{x:Type Label}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Label}"> 
       <Border BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       Background="{TemplateBinding Background}" 
       Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"> 
        <Grid> 
         <Decorator> 
          <Decorator.Effect> 
           <DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" /> 
          </Decorator.Effect> 
          <Decorator> 
           <Decorator.Effect> 
            <DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" /> 
           </Decorator.Effect> 
           <Decorator> 
            <Decorator.Effect> 
             <DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" /> 
            </Decorator.Effect> 
            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
        Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" 
        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 
            </ContentPresenter> 
           </Decorator> 
          </Decorator> 
         </Decorator> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         <Setter Property="Opacity" Value="0.5"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

如果风格在你的窗口或应用程序的资源,那么你可以申请它像这样:

<Label Style="{StaticResource GlassLabelStyle}" 

虽然我在这里,但我也遇到过TextBox的问题当你禁用控制(它只是保持还原为白色)时,你根本无法改变背景颜色,所以有人发现你必须覆盖整个模板!参见(https://stackoverflow.com/a/3752517/88409)。因此,这里是一个随时可以使用的风格禁用时,这将使文本框半透明(在玻璃上看起来不错),并使其背景半透明的白色有较明显的边界启用时:

<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#01000000" /> 
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#40000000" /> 
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#88ffffff" /> 

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="Background" Value="#88ffffff"/> 
    <Setter Property="BorderBrush" Value="Black"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="TextBox"> 
       <Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            Background="{TemplateBinding Background}" 
            SnapsToDevicePixels="true"> 
        <ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}" 
           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Value="{StaticResource DisabledBackgroundBrush}" Property="Background" /> 
         <Setter Value="{StaticResource DisabledBorderBrush}" Property="BorderBrush"/> 
         <Setter Value="{StaticResource DisabledForegroundBrush}" Property="Foreground" /> 
         <Setter TargetName="PART_ContentHost" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/> 
         <Setter TargetName="PART_ContentHost" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>