다음을 통해 공유


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

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

그리기 컨텍스트에 텍스트를 그리려면 DrawingContext 개체의 DrawText 메서드를 사용하십시오. 그리기 컨텍스트에서 콘텐츠 그리기가 끝나면 Close 메서드를 호출하여 그리기 컨텍스트를 닫고 콘텐츠를 저장합니다.

예제

        ' 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
// 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;
}
참고참고

앞의 코드 예제를 추출한 전체 코드 샘플을 보려면 Hit Test Using DrawingVisuals 샘플을 참조하십시오.