Text decoration in WPF-underline, underline, baseline and underline

Source: Internet
Author: User

We know that the modification of the text includes: Hollow words, three-dimensional words, crossed words, shadow words, bold, tilt and so on. Here only the style of the underlined words, the position of the underlined, we can divide it into: underlined, underlined, baseline and underline. As shown in figure:

Top to bottom, underlined (overline), underlined (strikethrough), baselines (Baseline), and underscores (underline).

How to achieve it.

(1) XAML Code:
<page xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com /winfx/2006/xaml ">
textdecorations= "Strikethrough" fontsize= "<textblock" fontfamily= "Arial" >A</TextBlock>
</Page>

Here the TextDecorations attribute can be set to:overline, Strikethrough, Baseline, underline , or None, If the TextDecorations property is not set, the default is None, which is no dash decoration.

(2) using C # code:
private void Setdefaultstrikethrough ()
{
Textblock1.textdecorations = Textdecorations.strikethrough;
}
(For simplicity, only relevant key code is listed here, and other code is not listed in C #.) TextBlock1 is the name of the TextBlock and is marked with the X:name= "TextBlock1" form in XAML

If you want a more complex point of effect, such as the need to set the color of the dash, line thickness, and so on, the following figure:


How to make a similar effect.
Set the TextDecorations property of the TextBlock, and then set the Pen property for TextDecoration.

As followsXAML Code:
<page xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com /winfx/2006/xaml ">
<Canvas>
<textblock fontsize= "fontfamily=" "Arial Black" margin= "0,0" >
<TextBlock.TextDecorations>
<textdecorationpenoffset= "Ten"penoffsetunit= "Pixel" penthicknessunit= "Pixel" >
<TextDecoration.Pen>
<Penthickness= "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 # key Code:
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;
}

Extended question:
Can be underlined, underlined, and underlined at the same time. For example: Can you draw the following picture of the Mo-wen.

The answer is: yes. Leave it to the interested friends to think about it.

Related reading: Typography in Windows presentation Foundation http://msdn2.microsoft.com/en-us/library/ms742190.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.