【C# Demo】真的好炫酷!霓虹灯字体特效

C#霓虹灯特效字体


在这里插入图片描述

介绍

C#中有多种显示框架,常见的普通框架,显示效果一般,用于个人使用或内部人员使用是足够的,但是给用户使用的客户端,界面就不能太过简洁,所以就有了XAML。
XAML简化了.Net Framework 3.0编程模式上的用户界面创建过程,使用XAML开发人员可以对WPF程序的所有用户界面元素(例如文本、按钮、图像和列表框等)进行详细的定置,同时还可以对整个界面进行合理化的布局,这与使用HTML非常相似。但是由于XAML是基于XML的,所以它本身就是一个组织良好的XML文档,而且相对于HTML,它的语法更严谨、更明确。预计以后大部分的XAML都可由相应的软件自动生成,就如同我们现在制作一个静态页面时,几乎不用编写任何HTML代码就可以直接通过Dreamweaver软件生成一个美观的页面。但是最初通过手动编写XAML代码将是一次绝佳的学习体验,虽然实现的过程繁杂了些,但是将加深您对XAML语法和各个元素的理解。

设计

可以从上面的效果看到,该设计是要基于字体进行描边,那么我们该如何对字体进行描边呢?这就需要用到【Shape】类了,下面是对字体进行描边的功能的代码:

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace NoneLightText.Media
{
    public class TextShape : Shape
    {
        private double _height;

        private Geometry _textGeometry;

        private double _width;

        protected sealed override Geometry DefiningGeometry => _textGeometry ?? Geometry.Empty;

        protected override Size MeasureOverride(Size availableSize)
        {
            RealizeGeometry();
            return new Size(Math.Min(availableSize.Width, _width), Math.Min(availableSize.Height, _height));
        }

        private void RealizeGeometry()
        {
            var formattedText = new FormattedText(
                Text,
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black, 100);

            _height = formattedText.Height;
            _width = formattedText.Width;
            _textGeometry = formattedText.BuildGeometry(new Point());

            if (Text == " ")
            {
                formattedText = new FormattedText(
                    "_",
                    CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight,
                    new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black, 100);
                _width = formattedText.Width;
            }
        }

        #region Dependency Properties

        /// <summary>
        ///     DependencyProperty for <see cref="FontFamily" /> property.
        /// </summary>
        public static readonly DependencyProperty FontFamilyProperty =
            TextElement.FontFamilyProperty.AddOwner(typeof(TextShape));

        /// <summary>
        ///     DependencyProperty for <see cref="FontSize" /> property.
        /// </summary>
        public static readonly DependencyProperty FontSizeProperty =
            TextElement.FontSizeProperty.AddOwner(typeof(TextShape));

        /// <summary>
        ///     DependencyProperty for <see cref="FontStretch" /> property.
        /// </summary>
        public static readonly DependencyProperty FontStretchProperty =
            TextElement.FontStretchProperty.AddOwner(typeof(TextShape));

        /// <summary>
        ///     DependencyProperty for <see cref="FontStyle" /> property.
        /// </summary>
        public static readonly DependencyProperty FontStyleProperty =
            TextElement.FontStyleProperty.AddOwner(typeof(TextShape));

        /// <summary>
        ///     DependencyProperty for <see cref="FontWeight" /> property.
        /// </summary>
        public static readonly DependencyProperty FontWeightProperty =
            TextElement.FontWeightProperty.AddOwner(typeof(TextShape));

        /// <summary>
        ///     DependencyProperty for <see cref="Text" /> property.
        /// </summary>
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(TextShape),
            new FrameworkPropertyMetadata(
                string.Empty,
                FrameworkPropertyMetadataOptions.AffectsMeasure |
                FrameworkPropertyMetadataOptions.AffectsRender));

        /// <summary>
        ///     The FontFamily property specifies the name of font family.
        /// </summary>
        [Localizability(LocalizationCategory.Font)]
        public FontFamily FontFamily
        {
            get => (FontFamily)GetValue(FontFamilyProperty);
            set => SetValue(FontFamilyProperty, value);
        }

        /// <summary>
        ///     The FontSize property specifies the size of the font.
        /// </summary>
        [TypeConverter(typeof(FontSizeConverter))]
        [Localizability(LocalizationCategory.None)]
        public double FontSize
        {
            get => (double)GetValue(FontSizeProperty);
            set => SetValue(FontSizeProperty, value);
        }

        /// <summary>
        ///     The FontStretch property selects a normal, condensed, or extended face from a font family.
        /// </summary>
        public FontStretch FontStretch
        {
            get => (FontStretch)GetValue(FontStretchProperty);
            set => SetValue(FontStretchProperty, value);
        }

        /// <summary>
        ///     The FontStyle property requests normal, italic, and oblique faces within a font family.
        /// </summary>
        public FontStyle FontStyle
        {
            get => (FontStyle)GetValue(FontStyleProperty);
            set => SetValue(FontStyleProperty, value);
        }

        /// <summary>
        ///     The FontWeight property specifies the weight of the font.
        /// </summary>
        public FontWeight FontWeight
        {
            get => (FontWeight)GetValue(FontWeightProperty);
            set => SetValue(FontWeightProperty, value);
        }

        /// <summary>
        ///     The Text property defines the content (text) to be displayed.
        /// </summary>
        [Localizability(LocalizationCategory.Text)]
        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }

        #endregion Dependency Properties
    }
}

之后就是在界面中设计出要显示的内容,然后如何进行描边,这里要设计三个描边的字体,让3个描边的动画都跟着,这样就会形成霓虹灯一样的效果了,代码如下:

<Window x:Class="NoneLightText.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NoneLightText"
        xmlns:media="clr-namespace:NoneLightText.Media"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="450" 
        Width="800"
        Background="#FF222222">

    <FrameworkElement.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation AutoReverse="True"
                                     FillBehavior="HoldEnd"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="TextPathRed"
                                     Storyboard.TargetProperty="StrokeDashOffset"
                                     From="0"
                                     To="1000"
                                     Duration="0:0:30" />
                    <DoubleAnimation AutoReverse="True"
                                     FillBehavior="HoldEnd"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="TextPathWhite"
                                     Storyboard.TargetProperty="StrokeDashOffset"
                                     From="10"
                                     To="1000"
                                     Duration="0:0:30" />
                    <DoubleAnimation AutoReverse="True"
                                     FillBehavior="HoldEnd"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="TextPathBlue"
                                     Storyboard.TargetProperty="StrokeDashOffset"
                                     From="20"
                                     To="1000"
                                     Duration="0:0:30" />
                    <DoubleAnimation AutoReverse="True"
                                     FillBehavior="HoldEnd"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="TextPathRed2"
                                     Storyboard.TargetProperty="StrokeDashOffset"
                                     From="0"
                                     To="1000"
                                     Duration="0:0:30" />
                    <DoubleAnimation AutoReverse="True"
                                     FillBehavior="HoldEnd"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="TextPathWhite2"
                                     Storyboard.TargetProperty="StrokeDashOffset"
                                     From="10"
                                     To="1000"
                                     Duration="0:0:30" />
                    <DoubleAnimation AutoReverse="True"
                                     FillBehavior="HoldEnd"
                                     RepeatBehavior="Forever"
                                     Storyboard.TargetName="TextPathBlue2"
                                     Storyboard.TargetProperty="StrokeDashOffset"
                                     From="20"
                                     To="1000"
                                     Duration="0:0:30" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </FrameworkElement.Triggers>

    <Grid>

        <Grid HorizontalAlignment="Center" Margin="92,50,92,180">
            <media:TextShape x:Name="TextPathRed"
                             HorizontalAlignment="Center"
                             Fill="Transparent"
                             FontSize="100"
                             FontWeight="Bold"
                             Stroke="#c43bb7"
                             StrokeDashArray="10 50"
                             StrokeThickness="4"
                             Text="Hello World">
                <FrameworkElement.Effect>
                    <DropShadowEffect BlurRadius="32"
                                      RenderingBias="Quality"
                                      ShadowDepth="0"
                                      Color="#c43bb7" />
                </FrameworkElement.Effect>
            </media:TextShape>

            <media:TextShape x:Name="TextPathWhite"
                             HorizontalAlignment="Center"
                             Fill="Transparent"
                             FontSize="100"
                             FontWeight="Bold"
                             Stroke="White"
                             StrokeDashArray="10 50"
                             StrokeDashOffset="10"
                             StrokeThickness="4"
                             Text="Hello World">
                <FrameworkElement.Effect>
                    <DropShadowEffect BlurRadius="32"
                                      RenderingBias="Quality"
                                      ShadowDepth="0"
                                      Color="White" />
                </FrameworkElement.Effect>
            </media:TextShape>

            <media:TextShape x:Name="TextPathBlue"
                             HorizontalAlignment="Center"
                             Fill="Transparent"
                             FontSize="100"
                             FontWeight="Bold"
                             Stroke="#2de4ea"
                             StrokeDashArray="10 50"
                             StrokeDashOffset="20"
                             StrokeThickness="4"
                             Text="Hello World">
                <FrameworkElement.Effect>
                    <DropShadowEffect BlurRadius="32"
                                      RenderingBias="Quality"
                                      ShadowDepth="0"
                                      Color="#1de4ea" />
                </FrameworkElement.Effect>
            </media:TextShape>
        </Grid>

        <Grid HorizontalAlignment="Center" Margin="92,240,92,180">
            <media:TextShape x:Name="TextPathRed2"
                             HorizontalAlignment="Center"
                             Fill="Transparent"
                             FontSize="100"
                             FontWeight="Bold"
                             Stroke="#c43bb7"
                             StrokeDashArray="10 50"
                             StrokeThickness="4"
                             Text="二哲科技">
                <FrameworkElement.Effect>
                    <DropShadowEffect BlurRadius="32"
                                      RenderingBias="Quality"
                                      ShadowDepth="0"
                                      Color="#c43bb7" />
                </FrameworkElement.Effect>
            </media:TextShape>

            <media:TextShape x:Name="TextPathWhite2"
                             HorizontalAlignment="Center"
                             Fill="Transparent"
                             FontSize="100"
                             FontWeight="Bold"
                             Stroke="White"
                             StrokeDashArray="10 50"
                             StrokeDashOffset="10"
                             StrokeThickness="4"
                             Text="二哲科技">
                <FrameworkElement.Effect>
                    <DropShadowEffect BlurRadius="32"
                                      RenderingBias="Quality"
                                      ShadowDepth="0"
                                      Color="White" />
                </FrameworkElement.Effect>
            </media:TextShape>

            <media:TextShape x:Name="TextPathBlue2"
                             HorizontalAlignment="Center"
                             Fill="Transparent"
                             FontSize="100"
                             FontWeight="Bold"
                             Stroke="#2de4ea"
                             StrokeDashArray="10 50"
                             StrokeDashOffset="20"
                             StrokeThickness="4"
                             Text="二哲科技">
                <FrameworkElement.Effect>
                    <DropShadowEffect BlurRadius="32"
                                      RenderingBias="Quality"
                                      ShadowDepth="0"
                                      Color="#1de4ea" />
                </FrameworkElement.Effect>
            </media:TextShape>
        </Grid>
    </Grid>
</Window>

源码

该项目是基于VS2019+.Net Framework4.7.2环境开发的。
源码已经上传CSDN有需要的可以去下载:https://download.csdn.net/download/qq_38390060/87603553
如果资源不够,可以关注我的公众号,发送【CSharp霓虹灯字体】就可以获取源码百度云盘地址。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二哲科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值