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, è possibile creare un pennello sfumato lineare e applicarlo alla Foreground proprietà di un TextBox oggetto . Quando si visualizza o si modifica la casella di testo, il pennello sfumato lineare viene applicato automaticamente al set corrente di caratteri nella stringa di testo.

Text displayed with a linear gradient brush

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

Text outline using a linear gradient brush

Quando il testo viene convertito in un Geometry oggetto , non è più una raccolta di caratteri. Non è possibile modificare i caratteri nella stringa di testo. Tuttavia, è possibile intervenire sull'aspetto del testo convertito modificandone le proprietà del tratto e del riempimento. Il tratto fa riferimento alla struttura del testo convertito, mentre 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.

Text with different colors for fill and stroke

Text with image brush applied to stroke

È anche possibile modificare il rettangolo del rettangolo del rettangolo di selezione 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.

Text with image brush applied to stroke and highlight

Esempio

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

/// <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 Geometry oggetti recuperati, è necessario accedere all'oggetto DrawingContext 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 Geometry gli oggetti nel controllo personalizzato, fornire un override per il OnRender metodo . Il metodo sottoposto a override deve utilizzare il DrawGeometry metodo per disegnare gli Geometry oggetti.

/// <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 l'origine dell'oggetto controllo utente personalizzato di esempio, vedere OutlineTextControl.cs per C# e OutlineTextControl.vb per Visual Basic.

Vedi anche