語言

FormattedText.BuildGeometry(Point) 方法

定義

回傳 Geometry 一個代表格式化文字的物件,包括所有字形與文字裝飾。

public:
 System::Windows::Media::Geometry ^ BuildGeometry(System::Windows::Point origin);
public System.Windows.Media.Geometry BuildGeometry(System.Windows.Point origin);
member this.BuildGeometry : System.Windows.Point -> System.Windows.Media.Geometry
Public Function BuildGeometry (origin As Point) As Geometry

參數

origin
Point

所得幾何形狀的左上角原點。

傳回

格式 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) fontWeight = FontWeights.Bold;
    if (Italic) 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)
    {
        _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 物件時,它不再是字元集合——你無法修改文字字串中的字元。 不過,您可以修改其筆劃與填滿屬性來影響轉換文字的外觀。

以下範例說明了透過修改轉換後文字的筆劃與填充來創造視覺效果的多種方法。

填充與筆劃用不同顏色的文字 填充設定筆劃與填充為不同顏色的範例

文字筆觸上畫筆 文字筆觸上畫筆 文字 文字筆觸上畫筆畫筆施加於筆觸的範例

轉換成物件並渲染 Geometry 的文字,可能與直接呈現的文字外觀不同:

  • 轉換成 Geometry 物件的文字不會使用 ClearType 來渲染。 此外,轉換後的基線不會被吸附成整個顯示像素。

  • 小型字體,如常用於正文的字體,可能會失去可讀性、模糊且外觀各異。

適用於