原文:WPF中的文字修饰——上划线,中划线,基线与下划线

我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。如图:

从上至下,分别为上划线(Overline),中划线(StrikeThrough),基线(Baseline)和下划线(Underline)。

如何实现?

(1)XAML代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<TextBlock TextDecorations="Strikethrough" FontSize="72" FontFamily="Arial">A</TextBlock>
</Page>

这里TextDecorations属性可以设置为:OverLine, Strikethrough, Baseline, UnderlineNone,如果没有设置TextDecorations属性,则默认为None,即不带划线修饰。

(2)使用C#代码:
private void SetDefaultStrikethrough()
{
   textBlock1.TextDecorations = TextDecorations.Strikethrough;
}
(为了简洁,这里只列出相关的关键代码,其他代码未用C#列出。textBlock1为TextBlock的名称,在XAML中使用 x:Name="textBlock1"形式标记)

如果要更复杂点的效果,比如需要设置划线的颜色、线粗等,如下图:

如何制作类似效果呢?
方法是:设置TextBlock的TextDecorations属性,再对TextDecoration的Pen属性进行设置。

如下XAML代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Canvas>
<TextBlock FontSize="84" FontFamily="Arial Black" Margin="0,0">
<TextBlock.TextDecorations>
<TextDecoration PenOffset="10" PenOffsetUnit="Pixel" PenThicknessUnit="Pixel">
 <TextDecoration.Pen>
          <Pen Thickness="5">
            <Pen.Brush>
              <LinearGradientBrush Opacity="0.8" StartPoint="0,0.5"  EndPoint="1,0.5">
                <LinearGradientBrush.GradientStops>
                  <GradientStop Color="Yellow" Offset="0" />
                  <GradientStop Color="Red" Offset="1" />
                </LinearGradientBrush.GradientStops>
              </LinearGradientBrush>
            </Pen.Brush>
            <Pen.DashStyle>
              <DashStyle Dashes="1,2,3"/>
            </Pen.DashStyle>
          </Pen>
        </TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
GOOD
</TextBlock>
</Canvas>
</Page>

C#关键代码:
private void SetLinearGradientUnderline()
{
    TextDecoration myUnderline = new TextDecoration();

Pen myPen = new Pen();
    myPen.Brush = new LinearGradientBrush(Colors.Yellow, Colors.Red, new Point(0, 0.5), new Point(1, 0.5));
    myPen.Brush.Opacity = 0.8;
    myPen.Thickness = 5;
    myPen.DashStyle = DashStyles.Dash;
    myUnderline.Pen = myPen;
    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

TextDecorationCollection myCollection = new TextDecorationCollection();
    myCollection.Add(myUnderline);
    textBlockGood.TextDecorations = myCollection;
}

引申问题:
可不可以同时画上划线、中划线和下划线?比如:可不可以画如下图所示的文武线呢?

答案是:可以!留给有兴趣的朋友去思考吧。

相关阅读:Typography in Windows Presentation Foundation http://msdn2.microsoft.com/en-us/library/ms742190.aspx

WPF中的文字修饰——上划线,中划线,基线与下划线的更多相关文章

  1. WPF文字修饰——上、中、下划线与基线

    我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线.基线与下划线.如图: 从上至下,分别为上划线(Overl ...

  2. WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法。

    原文:WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330 ...

  3. WPF中的文字修饰

    我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线.基线与下划线.如图: 从上至下,分别为上划线(Overl ...

  4. Python 私有变量中两个下划线 _ _item 与 一个下划线的区别 _item

    python中没有常量的说法, 但是可以通过元组实现一个常量 在python的私有变量中, 存在两个下划线 _ _item 与一个下划线 _item 的区别 前面带两个下划线的私有变量: 只能在本类中 ...

  5. Visual Studio vs2010 去掉中文注释红色下划线;去掉代码红色下划线;

    vs去掉下挂线也分两种: 1.去掉中文注释红色下划线,需要去掉VisualAssist下划线鸡肋功能: 1.选择Visual AssistX Options: 2.把如图所示的勾去掉,解决. 以后再次 ...

  6. python变量前的单下划线(私有变量)和双下划线()

    1.单下划线 变量前的单下划线表示表面上私有 ,但是其实这样的实例变量外部是可以访问的,但是,按照约定俗成的规定,当你看到这样的变量时,意思就是,“虽然我可以被访问,但是,请把我视为私有变量,不要随意 ...

  7. python实现将字符串中以大写字母开头的单词前面添加“_”下划线

    在工作中写测试用例代码生成的时候,函数命令考虑采用参数文件的名称来命名,但是发现文件命名是驼峰的写写法,所以想按照字符串中的大写字母做分割,每个单词前面添加下划线,主要考虑采用正则的模式来匹配,替换然 ...

  8. Android中自定义ListView实现上拉加载更多和下拉刷新

    ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...

  9. 私有化 : _x: 单前置下划线,私有化属性或方法;__xx:双前置下划线;__xx__:双前后下划线;属性property

    私有化 xx: 公有变量 _x: 单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问 __xx:双前置下划线,避免与子类中的属性命名冲突,无 ...

随机推荐

  1. 【31.58%】【codeforces 719B】 Anatoly and Cockroaches

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. 【u008】瑞瑞的木棍

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 瑞瑞有一堆的玩具木棍,每根木棍的两端分别被染上了某种颜色,现在他突然有了一个想法,想要把这 些木棍连在 ...

  3. [Angular] Angular CLI

    Create an app with routing config: ng new mynewapp --routing If you want to generate a new module wi ...

  4. [Vue] Use Vue.js Watchers to Respond to Async Updates

    Use watchers to keep an eye on your data. Watchers are methods that are invoked when the specified a ...

  5. CF 559B(Equivalent Strings-构造法)

    B. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. erlang一些参考资源

    1. erlang非业余研究 http://blog.yufeng.info/ 2.code.wang http://www.cnblogs.com/codew/ 3.码农生涯 http://www. ...

  7. 5.7-GTID复制搭建

    基本环境   Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.56.157 ...

  8. 学习鸟哥的Linux私房菜笔记(9)——bash1

    一.Shell简介 Shell :命令行解释器,是用户与系统沟通时的媒介 在Unix系统中有各种Shell, Linux采用bash为其默认shell 系统可以使用的shell记录在 /etc/she ...

  9. 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 预开一个20W长度的线段树; 这里a[1..20W]={0} ...

  10. 【序列操作IV】树状数组套线段树/树套树

    题目描述 给出序列 a1,a2,…,an(0≤ai≤109),有关序列的两种操作. 1. ai(1≤i≤n)变成 x(0≤x≤109). 2. 求 al,al+1,…,ar(1≤l≤r≤n)第 k(1 ...