방법: 윤곽선이 있는 텍스트 만들기

대부분의 경우 WPF(Windows Presentation Foundation) 애플리케이션에서 텍스트 문자열에 장식을 추가할 때 불연속 문자 또는 문자 모양의 컬렉션 측면에서 텍스트를 사용합니다. 예를 들어, 선형 그라데이션 브러시를 만들고 TextBox 개체의 Foreground 속성에 적용할 수 있습니다. 텍스트 상자를 표시하거나 편집할 때 선형 그라데이션 브러시는 텍스트 문자열의 현재 문자 세트에 자동으로 적용됩니다.

선형 그라데이션 브러시로 표시된 텍스트

그러나 텍스트를 Geometry 개체로 변환하여 다른 형식의 시각적으로 서식 있는 텍스트를 만들 수도 있습니다. 예를 들어, 텍스트 문자열의 윤곽선을 기반으로 Geometry 개체를 만들 수 있습니다.

선형 그라데이션 브러시를 사용하여 표시한 텍스트 윤곽선

텍스트를 Geometry 개체로 변환하면 더 이상 문자 컬렉션이 아니므로, 텍스트 문자열의 문자를 수정할 수 없습니다. 그러나 스트로크와 채우기 속성을 수정하여 변환된 텍스트의 모양에 영향을 줄 수 있습니다. 스트로크는 변환된 텍스트의 윤곽선을 나타내고, 채우기는 변환된 텍스트의 윤곽선 내부에 있는 영역을 나타냅니다.

다음 예제에서는 변환된 텍스트의 스트로크와 채우기를 수정하여 시각적으로 시각 효과를 만드는 여러 방법에 대해 설명합니다.

채우기와 스트로크에 다른 색을 사용하는 텍스트

스트로크에 이미지 브러시가 적용된 텍스트

변환된 텍스트의 경계 상자 사각형 또는 강조 표시도 수정할 수 있습니다. 다음 예제에서는 변환된 텍스트의 스트로크와 강조 표시를 수정하여 시각 효과를 만드는 방법을 보여 줍니다.

스트로크 및 하이라이트에 이미지 브러시가 적용된 텍스트

예제

텍스트를 Geometry 개체로 변환하려면 FormattedText 개체를 사용해야 합니다. 이 개체를 만든 후에는 BuildGeometryBuildHighlightGeometry 메서드를 사용하여 텍스트를 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 메서드에 대한 재정의를 제공합니다. 재정의된 메서드는 Geometry 개체를 그리는 데 DrawGeometry 메서드를 사용해야 합니다.

/// <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를 참조하세요.

참고 항목