Aracılığıyla paylaş


Nasıl yapılır: Ana hatlı metin oluşturma

Çoğu durumda, Windows Presentation Foundation (WPF) uygulamanızda metin dizelerine süsleme eklerken, metinleri ayrık karakterler veya karakterlerden oluşan bir koleksiyon olarak kullanırsınız. Örneğin, doğrusal gradyan fırça oluşturabilir ve bunu bir Foreground nesnesinin TextBox özelliğine uygulayabilirsiniz. Metin kutusunu görüntüleyip düzenlediğinizde, doğrusal gradyan fırçası otomatik olarak metin dizesindeki geçerli karakter kümesine uygulanır.

Lineer gradyan fırça ile görüntülenen metin

Bununla birlikte, metni Geometry nesnelere dönüştürerek görsel olarak zengin başka metin türleri de oluşturabilirsiniz. Örneğin, bir metin dizesinin ana hattını temel alan bir Geometry nesnesi oluşturabilirsiniz.

Doğrusal gradyan fırça kullanarak Metin ana hattını

Metin bir Geometry nesnesine dönüştürüldüğünde, artık bir karakter koleksiyonu değildir; metin dizesindeki karakterleri değiştiremezsiniz. Ancak, kontur ve dolgu özelliklerini değiştirerek dönüştürülen metnin görünümünü etkileyebilirsiniz. Çizgi, çevrilmiş metnin ana hattını ifade eder; dolgu, çevrilmiş metnin ana hattının içindeki alanı ifade eder.

Aşağıdaki örneklerde, dönüştürülen metnin vuruşunu ve dolgusunu değiştirerek görsel efektler oluşturmanın çeşitli yolları gösterilmektedir.

Dolgu ve kontur için farklı renklerde metin

Üzerine resim fırçası uygulanmış metin vuruşu

Ayrıca, dönüştürülen metnin sınırlayıcı kutu dikdörtgenini değiştirmek veya vurgulamak da mümkündür. Aşağıdaki örnekte, dönüştürülen metnin vuruşunu ve vurgulamasını değiştirerek görsel efektler oluşturmanın bir yolu gösterilmektedir.

Çizgi ve vurgulama için resim fırçası uygulanmış metin

Örnek

Metni Geometry nesnesine dönüştürmenin anahtarı, FormattedText nesnesini kullanmaktır. Bu nesneyi oluşturduktan sonra, metni BuildGeometry nesnelere dönüştürmek için BuildHighlightGeometry ve Geometry yöntemlerini kullanabilirsiniz. İlk yöntem, biçimlendirilmiş metnin geometrisini döndürür; ikinci yöntem, biçimlendirilmiş metnin sınırlayıcı kutusunun geometrisini döndürür. Aşağıdaki kod örneğinde, FormattedText nesnesinin nasıl oluşturulacağı ve biçimlendirilmiş metnin ve sınırlayıcı kutusunun geometrilerinin nasıl alınacağı gösterilmektedir.

/// <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 Geometry nesnelerini görüntülemek için, dönüştürülen metni görüntüleyen nesnenin DrawingContext'ine erişmeniz gerekir. Bu kod örneklerinde bu erişim, kullanıcı tanımlı işlemeyi destekleyen bir sınıftan türetilen özel bir denetim nesnesi oluşturularak elde edilir.

Özel denetimde Geometry nesneleri görüntülemek için OnRender yöntemi için bir geçersiz kılma sağlayın. Geçersiz kılınan yönteminiz, DrawGeometry nesneleri çizmek için Geometry yöntemini kullanmalıdır.

/// <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. C# için OutlineTextControl.cs ve Visual Basiciçin OutlineTextControl.vb.

Ayrıca bakınız