Condividi tramite


Procedura: Creare testo delineato

Nella maggior parte dei casi, quando si aggiunge ornamento alle stringhe di testo nell'applicazione Windows Presentation Foundation (WPF), si usa il testo in termini di una raccolta di caratteri discreti o glifi. Ad esempio, si può creare un pennello a sfumatura lineare e applicarlo alla proprietà Foreground di un oggetto TextBox. Quando si visualizza o si modifica la casella di testo, il pennello di sfumatura lineare viene applicato automaticamente al set corrente di caratteri nella stringa di testo.

Testo visualizzato con una pennellata a gradiente lineare

Tuttavia, è anche possibile convertire il testo in oggetti Geometry, che consente di creare altri tipi di testo visivamente ricco. Ad esempio, è possibile creare un oggetto Geometry in base alla struttura di una stringa di testo.

la struttura del testo usando un pennello a gradiente lineare

Quando il testo viene convertito in un oggetto Geometry, non è più una raccolta di caratteri. Non è possibile modificare i caratteri nella stringa di testo. Tuttavia, è possibile influire sull'aspetto del testo convertito modificandone le proprietà di tratto e riempimento. Il tratto fa riferimento alla struttura del testo convertito; il riempimento fa riferimento all'area all'interno della struttura del testo convertito.

Gli esempi seguenti illustrano diversi modi per creare effetti visivi modificando il tratto e il riempimento del testo convertito.

Testo con colori differenti per riempimento e contorno

Testo con pennello con immagine applicato al tratto

È anche possibile modificare il rettangolo del riquadro di delimitazione o l'evidenziazione del testo convertito. Nell'esempio seguente viene illustrato un modo per creare effetti visivi modificando il tratto e l'evidenziazione del testo convertito.

Testo con pennello per immagini applicato al tratto ed evidenziazione

Esempio

La chiave per convertire il testo in un oggetto Geometry consiste nell'utilizzare l'oggetto FormattedText. Dopo aver creato questo oggetto, è possibile utilizzare i metodi BuildGeometry e BuildHighlightGeometry per convertire il testo in oggetti Geometry. Il primo metodo restituisce la geometria del testo formattato; il secondo metodo restituisce la geometria del riquadro di delimitazione del testo formattato. Nell'esempio di codice seguente viene illustrato come creare un oggetto FormattedText e recuperare le geometrie del testo formattato e del relativo rettangolo di delimitazione.

/// <summary>
/// Create the outline geometry based on the formatted text.
/// </summary>
public void CreateText()
{
    System.Windows.FontStyle fontStyle = FontStyles.Normal;
    FontWeight fontWeight = FontWeights.Medium;

    if (Bold == true) fontWeight = FontWeights.Bold;
    if (Italic == true) fontStyle = FontStyles.Italic;

    // Create the formatted text based on the properties set.
    FormattedText formattedText = new FormattedText(
        Text,
        CultureInfo.GetCultureInfo("en-us"),
        FlowDirection.LeftToRight,
        new Typeface(
            Font,
            fontStyle,
            fontWeight,
            FontStretches.Normal),
        FontSize,
        System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text.
        );

    // Build the geometry object that represents the text.
    _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0));

    // Build the geometry object that represents the text highlight.
    if (Highlight == true)
    {
        _textHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0));
    }
}
''' <summary>
''' Create the outline geometry based on the formatted text.
''' </summary>
Public Sub CreateText()
    Dim fontStyle As FontStyle = FontStyles.Normal
    Dim fontWeight As FontWeight = FontWeights.Medium

    If Bold = True Then
        fontWeight = FontWeights.Bold
    End If
    If Italic = True Then
        fontStyle = FontStyles.Italic
    End If

    ' Create the formatted text based on the properties set.
    Dim formattedText As New FormattedText(Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface(Font, fontStyle, fontWeight, FontStretches.Normal), FontSize, Brushes.Black) ' This brush does not matter since we use the geometry of the text.

    ' Build the geometry object that represents the text.
    _textGeometry = formattedText.BuildGeometry(New Point(0, 0))

    ' Build the geometry object that represents the text highlight.
    If Highlight = True Then
        _textHighLightGeometry = formattedText.BuildHighlightGeometry(New Point(0, 0))
    End If
End Sub

Per visualizzare gli oggetti Geometry recuperati, è necessario accedere al DrawingContext dell'oggetto che visualizza il testo convertito. In questi esempi di codice, questo accesso viene ottenuto creando un oggetto di controllo personalizzato derivato da una classe che supporta il rendering definito dall'utente.

Per visualizzare gli oggetti Geometry nel controllo personalizzato, fornire una sovrascrittura per il metodo OnRender. Il metodo che hai sovrascritto dovrebbe utilizzare il metodo DrawGeometry per disegnare gli oggetti Geometry.

/// <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)
{
    // 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);
    }
}
''' <summary>
''' OnRender override draws the geometry of the text and optional highlight.
''' </summary>
''' <param name="drawingContext">Drawing context of the OutlineText control.</param>
Protected Overrides Sub OnRender(ByVal drawingContext As DrawingContext)
    ' Draw the outline based on the properties that are set.
    drawingContext.DrawGeometry(Fill, New Pen(Stroke, StrokeThickness), _textGeometry)

    ' Draw the text highlight based on the properties that are set.
    If Highlight = True Then
        drawingContext.DrawGeometry(Nothing, New Pen(Stroke, StrokeThickness), _textHighLightGeometry)
    End If
End Sub

Per il codice sorgente dell'oggetto controllo utente personalizzato di esempio, vedere OutlineTextControl.cs per C# e OutlineTextControl.vb per Visual Basic.

Vedere anche