كيفية القيام بما يلي: إنشاء زخرفة للنص

TextDecoration هو زخرفة مرئية يمكنك إضافتها إلى نص. هناك أربعة أنواع من زخارف النص: رسم خطا و الأساس و يتوسطه خط و تسطير. يظهر المثال التالي مواقع زخارف النص نسبة إلى النص.

مثال لأنواع زخرفة النص

الرسم التخطيطي لمواقع زخرفة النص

لإضافة زخرفة نص إلى نص، قم بإنشاء TextDecoration الكائن و تعديل الخصائص الخاصة به. استخدم خاصية Location لتحديد أين تظهر زخرفة النص مثل التسطير. استخدم خاصية Pen لتحديد مظهر زخرفة النص, مثل التعبئة الخالصة أو لون متدرج. إذا لم تقم بتحديد قيمة من أجل Pen الخاصية ، افتراضيات الزخرفة لنفس اللون مثل النص. حالما تنتهي من تعريف TextDecoration الكائن ، قم بإضافته إلى TextDecorations مجموعة من كائن النص المطلوب.

يظهر المثال التالي زخرفة نص قد تم زخرفتها بفرشاة تدرج خطي و قلم متقطع.

مثال زخرفة تسطير بفرشاة تدرج خطي و قلم متقطع

زخرفة النص بتسطير تدرج خطي

كائن Hyperlink هو عنصر محتوى تدفق مستوى مضمّن تسمح لك باستضافة الارتباطات التشعبية داخل محتوى التدفق. By الافتراضي, Hyperlink uses a TextDecoration object إلى display an underline. TextDecoration objects can be performance intensive إلى instantiate, particularly if you have many Hyperlink objects. إذا قمت بإجراء استخدام شامل لعناصر Hyperlink قد تحتاج إلى الأخذ بعين الاعتبار إظهار تسطير فقط عند تشغيل أحد الأحداث مثل حدث MouseEnter.

في المثال التالي، التسطير للارتباط "MSN الخاص بي" حيوي — يظهر فقط عند تشغيل حدث MouseEnter.

تعريف الارتباطات التشعبية ب TextDecorations

الارتباطات التشعبية التي تعرض TextDecorations

لمزيد من المعلومات، راجع كيفية القيام بما يلي: استخدام زخرفة النص مع ارتباط تشعبي.

مثال

في مثال الرمز التالي، زخرفة تسطير تستخدم قيمة الخط الافتراضية.

        ' Use the default font values for the strikethrough text decoration.
        Private Sub SetDefaultStrikethrough()
            ' Set the underline decoration directly to the text block.
            TextBlock1.TextDecorations = TextDecorations.Strikethrough
        End Sub
// Use the default font values for the strikethrough text decoration.
private void SetDefaultStrikethrough()
{
    // Set the underline decoration directly to the text block.
    TextBlock1.TextDecorations = TextDecorations.Strikethrough;
}
<!-- Use the default font values for the strikethrough text decoration. -->
<TextBlock
  TextDecorations="Strikethrough"
  FontSize="36" >
  The quick red fox
</TextBlock>

في مثال التعليمة البرمجية التالية, يتم إنشاء زخرفة نص تسطير بفرشاة لون خالص للقلم.

        ' Use a Red pen for the underline text decoration.
        Private Sub SetRedUnderline()
            ' Create an underline text decoration. Default is underline.
            Dim myUnderline As New TextDecoration()

            ' Create a solid color brush pen for the text decoration.
            myUnderline.Pen = New Pen(Brushes.Red, 1)
            myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended

            ' Set the underline decoration to a TextDecorationCollection and add it to the text block.
            Dim myCollection As New TextDecorationCollection()
            myCollection.Add(myUnderline)
            TextBlock2.TextDecorations = myCollection
        End Sub
// Use a Red pen for the underline text decoration.
private void SetRedUnderline()
{
    // Create an underline text decoration. Default is underline.
    TextDecoration myUnderline = new TextDecoration();

    // Create a solid color brush pen for the text decoration.
    myUnderline.Pen = new Pen(Brushes.Red, 1);
    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

    // Set the underline decoration to a TextDecorationCollection and add it to the text block.
    TextDecorationCollection myCollection = new TextDecorationCollection();
    myCollection.Add(myUnderline);
    TextBlock2.TextDecorations = myCollection;
}
<!-- Use a Red pen for the underline text decoration -->
<TextBlock
  FontSize="36" >
  jumped over
  <TextBlock.TextDecorations>
    <TextDecorationCollection>
      <TextDecoration 
        PenThicknessUnit="FontRecommended">
        <TextDecoration.Pen>
          <Pen Brush="Red" Thickness="1" />
        </TextDecoration.Pen>
      </TextDecoration>
    </TextDecorationCollection>
  </TextBlock.TextDecorations>
</TextBlock>

في مثال التعليمة البرمجية التالية, يتم إنشاء زخرفة نص تسطير بفرشاة تدرج خطي لقلم متقطع.

        ' Use a linear gradient pen for the underline text decoration.
        Private Sub SetLinearGradientUnderline()
            ' Create an underline text decoration. Default is underline.
            Dim myUnderline As New TextDecoration()

            ' Create a linear gradient pen for the text decoration.
            Dim myPen As New Pen()
            myPen.Brush = New LinearGradientBrush(Colors.Yellow, Colors.Red, New Point(0, 0.5), New Point(1, 0.5))
            myPen.Brush.Opacity = 0.5
            myPen.Thickness = 1.5
            myPen.DashStyle = DashStyles.Dash
            myUnderline.Pen = myPen
            myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended

            ' Set the underline decoration to a TextDecorationCollection and add it to the text block.
            Dim myCollection As New TextDecorationCollection()
            myCollection.Add(myUnderline)
            TextBlock3.TextDecorations = myCollection
        End Sub
// Use a linear gradient pen for the underline text decoration.
private void SetLinearGradientUnderline()
{
    // Create an underline text decoration. Default is underline.
    TextDecoration myUnderline = new TextDecoration();

    // Create a linear gradient pen for the text decoration.
    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.5;
    myPen.Thickness = 1.5;
    myPen.DashStyle = DashStyles.Dash;
    myUnderline.Pen = myPen;
    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

    // Set the underline decoration to a TextDecorationCollection and add it to the text block.
    TextDecorationCollection myCollection = new TextDecorationCollection();
    myCollection.Add(myUnderline);
    TextBlock3.TextDecorations = myCollection;
}
<!-- Use a linear gradient pen for the underline text decoration. -->
<TextBlock FontSize="36">the lazy brown dog.
  <TextBlock.TextDecorations>
    <TextDecorationCollection>
      <TextDecoration  
        PenThicknessUnit="FontRecommended">
        <TextDecoration.Pen>
          <Pen Thickness="1.5">
            <Pen.Brush>
              <LinearGradientBrush Opacity="0.5"
                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="2"/>
            </Pen.DashStyle>
          </Pen>
        </TextDecoration.Pen>
      </TextDecoration>
    </TextDecorationCollection>
  </TextBlock.TextDecorations>
</TextBlock>

راجع أيضًا:

المهام

كيفية القيام بما يلي: استخدام زخرفة النص مع ارتباط تشعبي

المرجع

TextDecoration

Hyperlink