다음을 통해 공유


방법: 시각적 요소에 텍스트 그리기

다음 예제에서는 DrawingContext 개체를 사용하여 DrawingVisual에 텍스트를 그리는 방법을 보여 줍니다. 그리기 컨텍스트 호출에서 반환 되는 RenderOpen 메서드는 DrawingVisual 개체입니다. 그래픽 및 텍스트를 그리기 컨텍스트에 그릴 수 있습니다.

텍스트를 그리기 컨텍스트에 그리려면 DrawingContext 개체의 DrawText 메서드를 사용합니다. 그리기 컨텍스트에 콘텐츠 그리기를 마치면 Close 메서드를 호출하여 그리기 컨텍스트를 닫고 콘텐츠를 유지합니다.

예시

// Create a DrawingVisual that contains text.
private DrawingVisual CreateDrawingVisualText()
{
    // Create an instance of a DrawingVisual.
    DrawingVisual drawingVisual = new DrawingVisual();

    // Retrieve the DrawingContext from the DrawingVisual.
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    // Draw a formatted text string into the DrawingContext.
    drawingContext.DrawText(
       new FormattedText("Click Me!",
          CultureInfo.GetCultureInfo("en-us"),
          FlowDirection.LeftToRight,
          new Typeface("Verdana"),
          36, System.Windows.Media.Brushes.Black),
          new System.Windows.Point(200, 116));

    // Close the DrawingContext to persist changes to the DrawingVisual.
    drawingContext.Close();

    return drawingVisual;
}
' Create a DrawingVisual that contains text.
Private Function CreateDrawingVisualText() As DrawingVisual
    ' Create an instance of a DrawingVisual.
    Dim drawingVisual As New DrawingVisual()

    ' Retrieve the DrawingContext from the DrawingVisual.
    Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()

    ' Draw a formatted text string into the DrawingContext.
    drawingContext.DrawText(New FormattedText("Click Me!", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Verdana"), 36, Brushes.Black), New Point(200, 116))

    ' Close the DrawingContext to persist changes to the DrawingVisual.
    drawingContext.Close()

    Return drawingVisual
End Function

비고

위의 코드 예제가 발췌된 전체 코드 샘플을 보려면 DrawingVisuals를 사용하여 적중 테스트 샘플을 참조하세요.