Nasıl yapılır: anahatları belirlenmiş metin oluşturma
Çoğu durumda, uygulamanızdaki metin dizelerine daha fazla süs yaparken Windows Presentation Foundation (WPF) , metni bir ayrık karakter veya karakter koleksiyonu açısından kullanırsınız. Örneğin, doğrusal bir gradyan fırçası oluşturabilir ve bunu Foreground bir nesnenin özelliğine uygulayabilirsiniz TextBox . Metin kutusunu görüntülerken veya düzenlediğinizde, doğrusal gradyan fırçası metin dizesindeki geçerli karakter kümesine otomatik olarak uygulanır.

Öte yandan, metni nesnelere dönüştürebilirsiniz, bu da Geometry görsel açıdan zengin metin türlerini oluşturmanızı sağlar. Örneğin, bir Geometry metin dizesinin ana hattını temel alan bir nesne oluşturabilirsiniz.

Metin bir Geometry nesneye dönüştürüldüğünde, artık bir karakter koleksiyonu değildir; metin dizesindeki karakterleri değiştiremezsiniz. Ancak, kontur ve Fill özelliklerini değiştirerek dönüştürülen metnin görünümünü etkileyebilirsiniz. Vuruş, dönüştürülen metnin ana hattını ifade eder; Fill, dönüştürülen metnin ana hattının içindeki alanı ifade eder.
Aşağıdaki örneklerde, dönüştürülmüş metnin konturunu ve doldurmasını değiştirerek görsel efekt oluşturmanın birkaç yolu gösterilmektedir.


Ayrıca, dönüştürülmüş metnin sınırlayıcı kutusu dikdörtgenini veya vurgulanmasını değiştirmek mümkündür. Aşağıdaki örnek, dönüştürülmüş metnin konturunu ve vurgulamasını değiştirerek görsel etkiler oluşturmanın bir yolunu gösterir.

Örnek
Bir nesneye metin dönüştürmenin anahtarı Geometry FormattedText nesnesini kullanmaktır. Bu nesneyi oluşturduktan sonra, BuildGeometry BuildHighlightGeometry metni nesnelerine dönüştürmek için ve yöntemlerini kullanabilirsiniz Geometry . İlk yöntem, biçimlendirilen metnin geometrisini döndürür; İkinci yöntem, biçimlendirilen metnin sınırlayıcı kutusunun geometrisini döndürür. Aşağıdaki kod örneği, bir nesnesinin nasıl oluşturulduğunu FormattedText ve biçimlendirilen metnin ve onun sınırlayıcı kutusunun geometrilerin nasıl alınacağını gösterir.
/// <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
Alınan nesneleri görüntülemek için Geometry , DrawingContext dönüştürülen metni görüntüleyen nesnesine erişmeniz gerekir. Bu kod örneklerinde, bu erişim, Kullanıcı tanımlı işlemeyi destekleyen bir sınıftan türetilmiş özel bir denetim nesnesi oluşturularak elde edilir.
GeometryÖzel denetimdeki nesneleri göstermek için, yöntemi için bir geçersiz kılma sağlayın OnRender . Geçersiz kılınan yönteminizin DrawGeometry nesneleri çizmek için yöntemini kullanması gerekir 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
Örnek özel kullanıcı denetimi nesnesinin kaynağı için, bkz. OutlineTextControl.cs for C# and OutlineTextControl. vb. Visual Basic.