WPF 文字描边

栏目: ASP.NET · 发布时间: 7年前

内容简介:本文告诉大家如何写出描边的字体在WPF如果需要写入描边需要使用 FormattedText 将文字转换为 Geometry 然后通过画出 Geometry 的边框和填充画出描边

本文告诉大家如何写出描边的字体

在WPF如果需要写入描边需要使用 FormattedText 将文字转换为 Geometry 然后通过画出 Geometry 的边框和填充画出描边

WPF 文字描边

首先创建一个类继承 UIElement 这样就可以重写 OnRender 方法在里面画出文字

假设需要画出的文字是 欢迎访问我博客 http://lindexi.gitee.io 里面有大量 UWP WPF 博客

protected override void OnRender(DrawingContext drawingContext)
        {
            var str = "欢迎访问我博客 http://lindexi.gitee.io 里面有大量 UWP WPF 博客";

            base.OnRender(drawingContext);
        }

通过字符串创建 FormattedText 这里需要传入很多参数

var formattedText = new FormattedText(str, CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface
                (
                    new FontFamily("微软雅黑"),
                    FontStyles.Normal,
                    FontWeights.Normal,
                    FontStretches.Normal
                ),
                30,
                Brushes.Black, 96);

WPF 文字描边

调用 formattedText.BuildGeometry 可以创建 Geometry 参数传入左上角坐标

然后就是画出这个 Geometry 通过这个 Pen 设置描边的宽度和颜色

drawingContext.DrawGeometry
            (
                new SolidColorBrush((Color) ColorConverter.ConvertFromString("#F00002")),
                new Pen(new SolidColorBrush(Colors.Black), 1),
                geometry
            );

所有代码请看下面

protected override void OnRender(DrawingContext drawingContext)
        {
            var str = "欢迎访问我博客 http://lindexi.gitee.io 里面有大量 UWP WPF 博客";

            var formattedText = new FormattedText(str, CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface
                (
                    new FontFamily("微软雅黑"),
                    FontStyles.Normal,
                    FontWeights.Normal,
                    FontStretches.Normal
                ),
                30,
                Brushes.Black, 1);

            var geometry = formattedText.BuildGeometry(new Point(10, 10));
            
            drawingContext.DrawGeometry
            (
                new SolidColorBrush((Color) ColorConverter.ConvertFromString("#F00002")),
                new Pen(new SolidColorBrush(Colors.Black), 1),
                geometry
            );

            base.OnRender(drawingContext);
        }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google's PageRank and Beyond

Google's PageRank and Beyond

Amy N. Langville、Carl D. Meyer / Princeton University Press / 2006-7-23 / USD 57.50

Why doesn't your home page appear on the first page of search results, even when you query your own name? How do other web pages always appear at the top? What creates these powerful rankings? And how......一起来看看 《Google's PageRank and Beyond》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具