WPF文字阴影 文字描边

参考:https://blog.csdn.net/jetluning/article/details/39178367
参考:https://blog.csdn.net/Vblegend_2013/article/details/83897803
参考:

第一种设置阴影效果,看上去有点像描边
在这里插入图片描述

<TextBlock
            Name="copor"
            Width="994"
            Height="37"
            Margin="144,527,-234,176.667"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="28"
            FontWeight="ExtraBold"
            Foreground="#333333"
            Text="爱学习的小康"
            TextTrimming="CharacterEllipsis">
            <TextBlock.Effect>
                <DropShadowEffect
                    BlurRadius="5"
                    Opacity="1"
                    ShadowDepth="0"
                    Color="Red" />
            </TextBlock.Effect>
        </TextBlock>

第二种,设置描边,但是有问题,描边会削短文字整体
在这里插入图片描述
在这里插入图片描述

 <local:TextPath
            x:Name="PathEdge"
            Margin="639,118,70,595.667"
            Fill="#333333"
            FontFamily="Ariel"
            FontSize="12"
            FontWeight="Bold"
            Stroke="#fff"
            StrokeThickness="0.5"
            Text="爱学习的小康" />
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication3
{
   /// <summary>
	/// This class generates a Geometry from a block of text in a specific font, weight, etc.
	/// and renders it to WPF as a shape.
	/// </summary>
    public class TextPath : Shape
    {
        private Geometry _textGeometry;

        #region Dependency Properties
        public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(typeof(TextPath),
                                                     new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily,
                                                             FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        [Localizability(LocalizationCategory.Font)]
        [TypeConverter(typeof(FontFamilyConverter))]
        public FontFamily FontFamily { get { return (FontFamily)GetValue(FontFamilyProperty); } set { SetValue(FontFamilyProperty, value); } }

        public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(TextPath),
                                                        new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,
                                                             FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        [TypeConverter(typeof(FontSizeConverter))]
        [Localizability(LocalizationCategory.None)]
        public double FontSize { get { return (double)GetValue(FontSizeProperty); } set { SetValue(FontSizeProperty, value); } }

        public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(typeof(TextPath),
                                                     new FrameworkPropertyMetadata(TextElement.FontStretchProperty.DefaultMetadata.DefaultValue,
                                                             FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        [TypeConverter(typeof(FontStretchConverter))]
        public FontStretch FontStretch { get { return (FontStretch)GetValue(FontStretchProperty); } set { SetValue(FontStretchProperty, value); } }

        public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(typeof(TextPath),
                                                     new FrameworkPropertyMetadata(SystemFonts.MessageFontStyle,
                                                             FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        [TypeConverter(typeof(FontStyleConverter))]
        public FontStyle FontStyle { get { return (FontStyle)GetValue(FontStyleProperty); } set { SetValue(FontStyleProperty, value); } }

        public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(typeof(TextPath),
                                                     new FrameworkPropertyMetadata(SystemFonts.MessageFontWeight,
                                                             FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        [TypeConverter(typeof(FontWeightConverter))]
        public FontWeight FontWeight { get { return (FontWeight)GetValue(FontWeightProperty); } set { SetValue(FontWeightProperty, value); } }

        public static readonly DependencyProperty OriginPointProperty =
                                        DependencyProperty.Register("Origin", typeof(Point), typeof(TextPath),
                                                new FrameworkPropertyMetadata(new Point(0, 0),
                                                        FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        [TypeConverter(typeof(PointConverter))]
        public Point Origin { get { return (Point)GetValue(OriginPointProperty); } set { SetValue(OriginPointProperty, value); } }

        public static readonly DependencyProperty TextProperty =
                                        DependencyProperty.Register("Text", typeof(string), typeof(TextPath),
                                                new FrameworkPropertyMetadata(string.Empty,
                                                        FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
                                                             OnPropertyChanged));
        [Bindable(true), Category("Appearance")]
        public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } }
        #endregion

        protected override Geometry DefiningGeometry
        {
            get { return _textGeometry; }

            //_textGeometry = Geometry.Empty;//_textGeometry ;
        }

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TextPath)d).CreateTextGeometry();
        }


        private void CreateTextGeometry()
        {
            var formattedText = new FormattedText(Text, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight,
                                    new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black);
            _textGeometry = formattedText.BuildGeometry(Origin);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            if (_textGeometry == null) CreateTextGeometry();
            if (_textGeometry.Bounds == Rect.Empty)
                return new Size(0, 0);
            // return the desired size
            return new Size(Math.Min(availableSize.Width, _textGeometry.Bounds.Width), Math.Min(availableSize.Height, _textGeometry.Bounds.Height));
        }
    }
}

发现有更好的,后续补充。。。欢迎大佬告诉小弟效果更好的描边

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值