title author date CreateTime categories
WPF 文字描边
lindexi
2019-01-24 19:47:18 +0800
2019-1-24 19:40:7 +0800
WPF

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

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

首先创建一个类继承 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);

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

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

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

打开 xaml 添加这个控件运行代码就可以看到上面界面

        <local:CureekaMasar></local:CureekaMasar>

所有代码请看下面

       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);
}

最新文章

  1. UEFI模式安装Win10和Linux双系统
  2. VS2012 单元测试之泛型类(Generics Unit Test)
  3. nodejs处理图片、CSS、JS链接
  4. Windows phone应用开发[18]-下拉刷新
  5. 以下css可以清除浮动
  6. Android Studio 引入Lambda表达式
  7. android sdk国内快速更新下载
  8. visual studio 2012 使用 git/github
  9. Ubuntu14.04下搜狗输入法的安装及配置
  10. SpringMVC中redirect跳转后如何保存Model中的数据?
  11. (转) Linux中profile、bashrc、bash_profile之间的区别和联系
  12. STM32 USB 鼠标+键盘 串口控制
  13. jmeter打开图形化界面时指定代理
  14. appium启动
  15. linux学习笔记-lrmi源码包的编译安装方法
  16. Benchmark Web App 性能瓶颈分析与性能测试工具的使用方法总结
  17. 【转】C#如何创建泛型类T的实例
  18. linux下如何启动sybase
  19. hdu 1042 N! java大数及判断文件末尾
  20. 使用Vmware虚拟机部署Swift开发环境之Mac OSX系统安装

热门文章

  1. 使用Node,Vue和ElasticSearch构建实时搜索引擎
  2. git log的个性化设置
  3. 19.go语言基础学习(下)——2019年12月16日
  4. python开发之virtualenv与virtualenvwrapper
  5. Python---基础---str
  6. 利用xcode Build生成模拟器运行包
  7. django搭建一个小型的服务器运维网站-用户登陆与session
  8. rssi pdf 单双峰正态发布 与 定位
  9. 匿名函数 sorted() filter() map() 递归函数
  10. 实验1 C语言开发环境...