如何:建立外框文字
在大部分情況下,當您將裝飾新增至 Windows Presentation Foundation (WPF) 應用程式中的文字字串時,您會在離散字元或字元集合中使用文字。 例如,您可以建立線性漸層筆刷,並將其套用至 Foreground 物件的 屬性 TextBox 。 當您顯示或編輯文字方塊時,線性漸層筆刷會自動套用至文字字串中的目前字元集。
不過,您也可以將文字 Geometry 轉換成物件,讓您能夠建立其他類型的視覺化 RTF。 例如,您可以根據文字字串的大綱建立 Geometry 物件。
當文字轉換成 Geometry 物件時,它不再是字元集合,您無法修改文字字串中的字元。 不過,您可以修改其筆劃與填滿屬性來影響轉換文字的外觀。 筆劃是指轉換文字的外框,填滿是指轉換文字外框內的區域。
下列範例說明透過修改轉換文字的筆劃和填滿來建立視覺效果的幾種方式。
您也可以修改已轉換文字的周框或反白顯示。 下列範例說明藉由修改轉換文字的筆劃和反白顯示來建立視覺效果的方法。
範例
將文字 Geometry 轉換成 物件的索引鍵是使用 FormattedText 物件。 建立此物件之後,您可以使用 BuildGeometry 和 BuildHighlightGeometry 方法,將文字 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。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應