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 nesnenin TextBox özelliğine Foreground 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.

Text displayed with a linear gradient brush

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

Text outline using a linear gradient brush

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 dolgu ö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ına başvurur; dolgu, dönüştürülen 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.

Text with different colors for fill and stroke

Text with image brush applied to stroke

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.

Text with image brush applied to stroke and highlight

Örnek

Metni nesneye Geometry dönüştürmenin anahtarı, nesnesini kullanmaktır FormattedText . Bu nesneyi oluşturduktan sonra ve yöntemlerini kullanarak BuildGeometryBuildHighlightGeometry metni nesnelere Geometry dönüştürebilirsiniz. İ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 bir FormattedText nesnenin 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 nesneleri görüntülemek için, dönüştürülen metni görüntüleyen nesnenin öğesine erişmeniz DrawingContext 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 denetimdeki nesneleri görüntülemek Geometry için yöntemi için OnRender bir geçersiz kılma sağlayın. Geçersiz kılınan yönteminiz, nesneleri çizmek Geometry için yöntemini kullanmalıdırDrawGeometry.

/// <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 Basic için OutlineTextControl.vb.

Ayrıca bkz.