방법: 그린 텍스트 맞추기

사용자 지정 그리기를 수행할 때 그리는 텍스트를 양식이나 컨트롤의 가운데에 배치하는 것이 좋습니다. 올바른 서식 개체를 만들고 적절한 서식 플래그를 설정하여 DrawString 또는 DrawText 메서드로 그린 텍스트를 쉽게 맞출 수 있습니다.

GDI+(DrawString)로 가운데 맞춤 텍스트를 그리려면 다음을 수행합니다.

  1. 적절한 DrawString 메서드와 함께 StringFormat을 사용하여 가운데 맞춤 텍스트를 지정합니다.

    string text1 = "Use StringFormat and Rectangle objects to"
        + " center text in a rectangle.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect1 = 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.
        StringFormat stringFormat = 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);
    }
    
    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(DrawText)를 사용하여 가운데 맞춤 텍스트를 그리려면 다음을 수행합니다.

  1. 적절한 DrawText 메서드를 사용하여 텍스트를 세로 및 가로 가운데에 배치하고 래핑을 위해 TextFormatFlags 열거를 사용합니다.

    string text2 = "Use TextFormatFlags and Rectangle objects to"
     + " center text in a rectangle.";
    
    using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect2 = new Rectangle(150, 10, 130, 140);
    
        // Create a TextFormatFlags with word wrapping, horizontal center and
        // vertical center specified.
        TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
            TextFormatFlags.VerticalCenter | 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);
    }
    
    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
    

코드 컴파일

앞의 코드 예제는 Windows Forms와 함께 사용하도록 설계되었으며 PaintEventHandler의 매개변수인 PaintEventArgse가 필요합니다.

참고 항목