VB.NET 2008/VB - 그래픽

그래픽 - 텍스트 그리기

본클라쓰 2012. 4. 5. 12:11

 

GDI+ 및 GDI는 Windows Forms에서 텍스트를 그리는 데 사용할 수 있는 여러 클래스를 제공한다. GDI+ Graphics 클래스에는 텍스트의 위치, 경계 사각형, 글꼴 및 서식과 같은 다양한 기능을 지정할 수 있는 몇 가지 DrawString 메서드가 있다.

 

또한 TextRenderer 클래스에서 제공하는 정적 DrawText 및 MeasureText 메서드를 사용하여 GDI로 텍스트를 그리고 길이를 맞출 수 있다. 또한 GDI 메서드를 사용하여 위치, 글꼴 및 서식을 지정할 수 있다. GDI와 GDI+ 중 하나를 선택하여 텍스트를 렌더링 할 수 있지만 일반적으로 GDI가 성능이 더 좋으며 텍스트 맞춤도 정확하다.

 

 

 

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

 

사용자 지정 그리기를 수행할 때 텍스트를 지정된 위치에서 가로줄 하나로 그릴 수 있다. Point 또는 PointF 매개 변수를 사용하는 Graphics 클래스의 오버로드된 DrawString 메서드를 사용하여 이러한 방법으로 텍스트를 그릴 수 있다. DrawString 메서드에는 Brush Font도 필요하다.

 

TextRenderer에서 Point를 사용하는 오버로드된 DrawText 메서드를 사용할 수도 있다. DrawText에는 Color Font도 필요하다.

 

 

GDI+를 사용하여 한 줄의 텍스트를 그리려면

 

Dim font1 As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)
Try
    Dim pointF1 As New PointF(30, 10)
    e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1)
Finally
    font1.Dispose()
End Tr

 

 

GDI를 사용하여 한 줄의 텍스트를 그리려면

 

Dim font As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)
Try
    Dim point1 As New Point(30, 10)
    TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue)
Finally
    font.Dispose()
End Try

 

 

 

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

 

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

 

Rectangle TextFormatFlags 매개 변수를 사용하는 TextRender의 오버로드된 DrawText 메서드를 사용하여 사각형 안에 줄 바꿈된 텍스트를 그릴 수도 있다. Color Font도 사용해야 한다.

 

 

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

 

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

 

 

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

 

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

 

 

출처) http://msdn.microsoft.com/ko-kr/library/xwf9s90b(v=vs.90).aspx

 

 

 

 

그린 텍스트 맞추기

 

사용자 지정 그리기를 수행할 때 폼이나 컨트롤에 그려진 텍스트를 가운데에 맞춰야 하는 경우가 많다. 올바른 서식 개체를 만들고 적절한 서식 플래그를 설정하여 DrawString 도는 DrawText 메서드로 그린 텍스트를 손쉽게 맞출 수 있다.

 

 

GDI+를 사용하여 가운데에 맞춰진 텍스트를 그리려면

 

적절한 DrawString 메서드와 함게 StringFormat을 사용하여 가운데에 맞춰진 텍스트를 지정한다.

 

Dim text1 As String = "Use StringFormat and Rectangle objects to center text in a rectangle."
Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
Try
    Dim rect1 As New Rectangle(10, 10, 130, 140)
    ' Create a StringFormat object with the each line of text, and the block
    ' of text centered on the page.
    Dim stringFormat As New StringFormat()
    stringFormat.Alignment = StringAlignment.Center
    stringFormat.LineAlignment = StringAlignment.Center
    ' Draw the text and the surrounding rectangle.
    e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat)
    e.Graphics.DrawRectangle(Pens.Black, rect1)
Finally
    font1.Dispose()
End Try

 

 

GDI를 사용하여 가운데에 맞춰진 텍스트를 그리려면

 

TextFormatFlags 열거형을 사용하여 텍스크를 래핑하고 적절한 DrawText 메서드를 사용하여 텍스트를 가로와 세로 방향으로 가운데에 맞춘다.

 

Dim text2 As String = "Use TextFormatFlags and Rectangle objects to center text in a rectangle."

Dim font2 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
Try
    Dim rect2 As New Rectangle(150, 10, 130, 140)
    ' Create a TextFormatFlags with word wrapping, horizontal center and
    ' vertical center specified.
    Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or _
        TextFormatFlags.VerticalCenter Or TextFormatFlags.WordBreak
    ' Draw the text and the surrounding rectangle.
    TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags)
    e.Graphics.DrawRectangle(Pens.Black, rect2)
Finally
    font2.Dispose()
End Try

 

 

 

사용자 지정으로 그릴 텍스트의 사이즈를 알고 싶을 때는

 

TextRenderMeasureText 메서드를 사용하여 Size 개체를 얻을 수 있다.

 

Dim g As Graphics = Me.Panel1.CreateGraphics
Dim f As New Font("돋음", 10)

Dim size As Size = TextRenderer.MeasureText(g, TextBox1.Text, f, New Size(Integer.MaxValue, Integer.MaxValue), _ 

    TextFormatFlags.NoPadding)
TextRenderer.DrawText(g, TextBox1.Text, f, New Point(0, 0), Color.Black)
Console.WriteLine(size.ToString)