【问题标题】:Outer Glow Effect and rotation On A Label in WPFWPF中标签上的外发光效果和旋转
【发布时间】:2019-05-17 23:10:33
【问题描述】:

我需要在标签上创建外部发光效果并使其旋转一点(大约 20 度)。我正在使用以下代码,但它没有按照我想要的方式工作:

<Label Height="106" Margin="80,57,36,0" Name="lblHeading" FontSize="35">
    Brian's 15th Birthday Party
    <Label.Effect>
        <DropShadowEffect BlurRadius="100" ShadowDepth="0" Opacity="1" 
                          Color="White"/>
    </Label.Effect>
</Label>

是否可以在窗口的某处添加一些文本并为其添加外部发光效果和旋转?如果有人可以帮助我在标签上添加相同的效果或以任何其他方式在不使用标签控件的情况下添加效果,那就太好了。

我也尝试了以下方法,但没有帮助。也许我不知道如何使用它,因为它只是导致错误:

<OuterGlowBitmapEffect GlowColor="Blue" GlowSize="30" Noise="1" Opacity="0.4" />

【问题讨论】:

  • 我对 WPF 非常陌生,因此更感谢简单的代码或提供一些解释的帮助。
  • 位图效果在 .Net 4 中已被弃用,因此如果您使用的是该版本,这将是您收到错误的原因。

标签: c# wpf xaml


【解决方案1】:
  1. 您可能希望使用较小的BlurRadius,将其设置为 100 将使效果接近于不可见。我建议 10 个。
  2. RenderTransformOrigin 设置为您希望文本围绕旋转的点(0.5, 0.5 表示围绕中心旋转)。
  3. Label.RenderTransform 内添加RotateTransform

完整的代码应该接近这个:

<Label Height="106" Margin="80,57,36,0" Name="lblHeading" FontSize="35"
       RenderTransformOrigin="0.5, 0.5">
    Brian's 15th Birthday Party
    <Label.Effect>
        <DropShadowEffect BlurRadius="10" ShadowDepth="0" Opacity="1" 
                      Color="White"/>
    </Label.Effect>
    <Label.RenderTransform>
        <RotateTransform Angle="20"/>
    </Label.RenderTransform>
</Label>

【讨论】:

    【解决方案2】:

    这是您可以旋转标签的方式:

    <Label>
      <Label.LayoutTransform>
        <RotateTransform Angle="20"/>
      </Label.LayoutTransform>
      <Label.Content>text</Label.Content>
    </Label>
    

    【讨论】:

    • +1 到 Codesparkle - 打败我,更完整(在 iPad 上打字很难!) - 一旦我发布,我就开始在发光部分工作,但现在为时已晚!
    【解决方案3】:

    这可以通过修改ControlTemplate来完成。

    关键是使用两个ContentPresenter来显示你的文字,并将BlurEffect附加到一个ContentPresenter

    代码:

    <Label Content="TestContent" Foreground="White" FontSize="20">
        <Label.Template>
            <ControlTemplate TargetType="Label">
                <Border>
                    <Grid>
                        <ContentPresenter TextBlock.Foreground="{TemplateBinding Foreground}"/>
                        <ContentPresenter TextBlock.Foreground="{TemplateBinding Foreground}">
                            <ContentPresenter.Effect>
                                <BlurEffect Radius="5"/>
                            </ContentPresenter.Effect>
                        </ContentPresenter>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Label.Template>
    </Label>
    

    How it looks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2012-06-22
      • 1970-01-01
      相关资源
      最近更新 更多