如何:建立外框文字

在大部分情況下,當您將裝飾新增至 Windows Presentation Foundation (WPF) 應用程式中的文字字串時,您正在離散字元或字符集合中使用文字。 例如,您可以建立線性漸層筆刷,並將其套用至 TextBox 物件的 Foreground 屬性。 當您顯示或編輯文字方塊時,線性漸層筆刷會自動套用至文字字串中的目前字元集。

以線性漸層筆刷顯示的文字

不過,您也可將文字轉換成 Geometry 物件,讓您建立其他類型的視覺化 RTF。 例如,您可以根據文字字串的外框建立 Geometry 物件。

以線性漸層筆刷繪製外框的文字

當文字轉換成 Geometry 物件時,它就不再是字元集合,您無法修改文字字串中的字元。 不過,您可以修改其筆劃與填滿屬性來影響轉換文字的外觀。 筆劃是指轉換文字的外框,填滿是指轉換文字外框內的區域。

下列範例說明數種藉由修改筆劃和填滿轉換的文字來建立視覺效果的方式。

使用不同填色和筆觸色彩的文字

影像筆刷套用至筆觸的文字

您也可以修改已轉換文字的週框方塊或醒目顯示。 下列範例說明如何藉由修改已轉換文字的筆劃和醒目提示來建立視覺效果。

套用至筆劃和反白顯示影像筆刷的文字

範例

將文字轉換成 Geometry 物件的關鍵是使用 FormattedText 物件。 建立此物件後,您可以使用 BuildGeometryBuildHighlightGeometry 方法,將文字轉換成 Geometry 物件。 第一個方法會傳回格式化文字的幾何;第二個方法會傳回格式化文字週框方塊的幾何。 下列程式碼範例示範如何建立 FormattedText 物件,以及擷取格式化文字及其週框方塊的幾何。

/// <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

若要顯示已擷取的 Geometry 物件,您必須存取顯示已轉換文字之物件的 DrawingContext。 在這些程式碼範例中,建立衍生自支援使用者定義轉譯之類別的自訂控制項物件,即可達成此存取。

若要在自訂控制項中顯示 Geometry 物件,請提供 OnRender 方法的覆寫。 覆寫的方法應該使用 DrawGeometry 方法來繪製 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

如需範例自訂使用者控制項物件的來源,請參閱適用於 C# 的 OutlineTextControl.cs適用於 Visual Basic 的 OutlineTextControl.vb

另請參閱