다음을 통해 공유


방법: 사각형 안에 줄 바꿈된 텍스트 그리기

Rectangle 또는 RectangleF 매개 변수를 사용하는 Graphics 클래스의 오버로드된 DrawString 메서드를 사용하여 사각형 안에 줄 바꿈된 텍스트를 그릴 수 있습니다. BrushFont도 사용해야 합니다.

RectangleTextFormatFlags 매개 변수를 사용하는 TextRenderer의 오버로드된 DrawText 메서드를 사용하여 사각형 안에 줄 바꿈된 텍스트를 그릴 수도 있습니다. ColorFont도 사용해야 합니다.

다음 그림에서는 DrawString 메서드를 사용하여 사각형 안에 그린 텍스트의 출력을 보여 줍니다.

글꼴 텍스트

GDI+를 사용하여 사각형 안에 줄 바꿈된 텍스트를 그리려면

  • 오버로드된 DrawString 메서드를 사용하여 원하는 텍스트, Rectangle 또는 RectangleF, FontBrush를 전달합니다.

    Dim text1 As String = "Draw text in a rectangle by passing a RectF to the DrawString method."
    Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rectF1 As New RectangleF(30, 10, 100, 122)
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1)
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1))
    Finally
        font1.Dispose()
    End Try
    
    string text1 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
    }
    

GDI를 사용하여 사각형 안에 줄 바꿈된 텍스트를 그리려면

  • TextFormatFlags 열거형 값을 사용하면 원하는 텍스트, Rectangle, FontColor를 전달하여 오버로드된 DrawText 메서드로 줄 바꿈할 텍스트를 지정할 수 있습니다.

    Dim text2 As String = _
        "Draw text in a rectangle by passing a RectF to the DrawString method."
    Dim font2 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect2 As New Rectangle(30, 10, 100, 122)
    
        ' Specify the text is wrapped.
        Dim flags As TextFormatFlags = TextFormatFlags.WordBreak
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags)
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2))
    Finally
        font2.Dispose()
    End Try
    
    string text2 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect2 = new Rectangle(30, 10, 100, 122);
    
        // Specify the text is wrapped.
        TextFormatFlags flags = TextFormatFlags.WordBreak;
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2));
    
    }
    

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

참고 항목

작업

방법: GDI를 사용하여 텍스트 그리기

방법: 글꼴 패밀리 및 글꼴 만들기

방법: 지정된 위치에 텍스트 그리기

기타 리소스

글꼴 및 텍스트 사용