原文:WPF文字描边的解决方法



由于项目原因,今天研究了一下午WPF的文字描边,网上这方面的资料奇少,搞了半天才发现强大的WPF原来不直接支持文字描边啊。最后求助于MSDN,找到了方案,和大家分享一下:

主要思路:用FormattedText将字符串转换为Geometry,再在重写的OnRender(DrawingContext
drawingContext)方法中绘制Geometry。效果如图。

组件的主要属性:

Text属性设置文字

Fill属性设置文本本身的画刷

Stroke属性是描边画刷

StrokeThicknes是描边宽度

其他属性同Label(继承自Label)

XML配置:

<Window x:Class="StrokeableLabelTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpflib="clr-namespace:BLCTClassLibrary.WpfLib;assembly=BLCTClassLibrary.WpfLib"
Title="MainWindow" Height="422" Width="579">
<Grid ShowGridLines="True">
<StackPanel Orientation="Vertical" >
<Label Margin="10" Content="以下是StrokeableLabel类(相对轻量级)"/>
<wpflib:StrokeableLabel Text="测试文本" Fill="Yellow" Stroke="Black" StrokeThickness="0.3" FontWeight="Bold" FontSize="50"/>
<wpflib:StrokeableLabel Text="测试文本" Fill="Yellow" Stroke="Red" StrokeThickness="0.7" FontWeight="DemiBold" FontSize="50">
<wpflib:StrokeableLabel.Effect>
<DropShadowEffect Color="Black" BlurRadius="15" RenderingBias="Quality" Direction="290" ShadowDepth="5" Opacity="1" />
</wpflib:StrokeableLabel.Effect>
</wpflib:StrokeableLabel>
<wpflib:StrokeableLabel Text="测试文本" Fill="White" StrokeThickness="2" FontWeight="Bold" FontSize="50">
<wpflib:StrokeableLabel.Stroke>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="Blue" Offset="0.2"/>
<GradientStop Color="Brown" Offset="0.3"/>
<GradientStop Color="PowderBlue" Offset="0.7"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</wpflib:StrokeableLabel.Stroke>
</wpflib:StrokeableLabel>
<wpflib:StrokeableLabel Text="测试文本" Stroke="red" StrokeThickness="2" FontWeight="Bold" FontSize="50">
<wpflib:StrokeableLabel.Fill>
<ImageBrush ImageSource="/StrokeableLabelTest;component/Images/20085385922474_2.jpg" />
</wpflib:StrokeableLabel.Fill>
</wpflib:StrokeableLabel>
<wpflib:StrokeableLabel Fill="Transparent" FontSize="50" FontWeight="Light" StrokeThickness="8" Text="测试文本" >
<wpflib:StrokeableLabel.Stroke>
<ImageBrush ImageSource="/StrokeableLabelTest;component/Images/05.jpg" />
</wpflib:StrokeableLabel.Stroke>
</wpflib:StrokeableLabel>
</StackPanel> </Grid>
</Window>

库核心代码:

        private void getformattedText(string str)
{
// Create the formatted text based on the properties set.
FormattedText formattedText = new FormattedText(
str,
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(
FontFamily,
FontStyle,
FontWeight,
FontStretch),
FontSize,
System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text.
); this.Width = formattedText.Width;
this.Height = formattedText.Height;
// Build the geometry object that represents the text.
//pg.AddGeometry(formattedText.BuildGeometry(new System.Windows.Point(5, 5)));
TextGeometry = formattedText.BuildGeometry(new System.Windows.Point(0,0));
// Build the geometry object that represents the text hightlight.
if (Highlight == true)
{
TextHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0));
}
} /// <summary>
/// OnRender override draws the geometry of the text and optional highlight.
/// </summary>
/// <param name="drawingContext">Drawing context of the OutlineText control.</param>
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
CreateText();
// Draw the outline based on the properties that are set.
drawingContext.DrawGeometry(Fill, new System.Windows.Media.Pen(Stroke, StrokeThickness), TextGeometry);
// Draw the text highlight based on the properties that are set.
if (Highlight == true)
{
drawingContext.DrawGeometry(null, new System.Windows.Media.Pen(Stroke, StrokeThickness), TextHighLightGeometry);
}
}

具体代码请参照如下资源:

http://download.csdn.net/detail/wblct/8697005

需要2分的资源分,快没分了,大家谅解啊。

WPF文字描边的解决方法的更多相关文章

  1. WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整

    原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...

  2. RecyclerView嵌套TextView时显示文字不全的解决方法之一

    先描述一下这个小bug:简单的TextView嵌套RecyclerView作为itemView时,可能会在文本中出现布局覆盖的现象,itemView的布局其实很简单,就是一个RelativeLayou ...

  3. input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法

    1.只允许输入数字: <input type="text" onkeyup="this.value=this.value.replace(/[^0-9]/g,'') ...

  4. wpf ActualWidth为0解决方法

    LocalNewsControl() { var descriptor = DependencyPropertyDescriptor.FromProperty(ActualWidthProperty, ...

  5. WPF文字排列方式解析zz

      WPF文字的处理是一个比较基础的技能.在使用WPF开发工具时,对于各种文字的处理时经常会遇到的情况.希望大家可以通过实践经验的积累,牢固掌握这一方面知识. AD:WOT2014:用户标签系统与用户 ...

  6. CSS3实现文字描边的2种方法

    问题 最近遇到一个需求,需要实现文字的描边效果,如下图           解决方法一     首先想到去看CSS3有没有什么属性可以实现,后来被我找到了text-stroke     该属性是一个复 ...

  7. [WPF] 如何实现文字描边

    1. 前言 WPF 的 TextBlock 提供了大部分常用的文字修饰方法,在日常使用中基本够用.如果需要更丰富的表现方式,WPF 也提供了其它用起来复杂一些的工具去实现这些需求.例如这篇文章介绍的文 ...

  8. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  9. WPF:指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效的解决方法

    文/嶽永鹏 WPF 数据绑定中绑定到ENTITY,如果把数据文件做成一个类库,在UI文件中去应用它,可能遇到下面这种情况. 指定的命名连接在配置中找不到.非计划用于 EntityClient 提供程序 ...

随机推荐

  1. iTestin云测工具

    软件概述 iTestin是免费服务移动App开发者的真机自动化云测试客户端工具.基于真实的智能终端设备录制一个测试脚本然后运行,并输出运行结果.覆盖Android和iOS两大设备平台,支持Pad/Ph ...

  2. 使用doxygen为C/C++程序生成中文文档

    文章来自:http://www.fmddlmyy.cn/text21.html 依照约定的格式凝视源码,用工具处理凝视过的源码产生文档.通过这样的方式产生文档至少有下面优点: 便于代码和文档保持同步. ...

  3. html的meta标签的charset应该用UTF-8还是utf-8?

    之前我也纠结过写html的时候是用<meta charset="UTF-8"/> 或者是 <meta charset="utf-8"/> ...

  4. 一、Github博客搭建之jekyll安装

    注意:以下步骤是FQ后操作的,需要了解FQ的可以移步 -> 枫叶主机 一.安装jekyll需要Ruby-2.1.0以上版本,本人是mac pro系统版本10.12.5(macOS Sierra) ...

  5. Bootstrap相关优质项目学习清单

    1:编码规范 by @mdo编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范 http://codeguide.bootcss.com/ 2:快速.可靠.安全的依赖管理工具.Yarn 缓存了 ...

  6. [CSS] Use Generated Content to Augment Information

    When you create a pseudo element, you have access to the parent HTML attributes. They can be used in ...

  7. 数据类型总结——Boolean类型(布尔类型)

    相关文章 简书原文:https://www.jianshu.com/p/e5c75d4be636 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...

  8. bc-win32-power-echo-vim-not-work

    http://gnuwin32.sourceforge.net/packages.html linux ok, but win32 not ok [root@130-255-8-100 ~]# ech ...

  9. iOS开发Quartz2D 三 进度条的应用

    一:效果如图: 二:代码 #import "ViewController.h" #import "ProgressView.h" @interface View ...

  10. 【机器学习实战】第4章 朴素贝叶斯(Naive Bayes)

    第4章 基于概率论的分类方法:朴素贝叶斯 朴素贝叶斯 概述 贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯定理为基础,故统称为贝叶斯分类.本章首先介绍贝叶斯分类算法的基础——贝叶斯定理.最后,我们 ...