Share via


방법: Windows Form에서 텍스트 그리기

다음 코드 예제는 GraphicsDrawString 메서드를 사용하여 양식에 텍스트를 그리는 방법을 보여 줍니다. 또는 양식에 텍스트를 그리는 데 TextRenderer를 사용할 수 있습니다. 자세한 내용은 방법: GDI로 텍스트 그리기를 참조하세요.

예제

public:
    void DrawString()
    {
        System::Drawing::Graphics^ formGraphics = this->CreateGraphics();
        String^ drawString = "Sample Text";
        System::Drawing::Font^ drawFont =
            gcnew System::Drawing::Font("Arial", 16);
        System::Drawing::SolidBrush^ drawBrush = gcnew
            System::Drawing::SolidBrush(System::Drawing::Color::Black);
        float x = 150.0F;
        float y = 50.0F;
        System::Drawing::StringFormat^ drawFormat =
            gcnew System::Drawing::StringFormat();
        formGraphics->DrawString(drawString, drawFont, drawBrush, x,
            y, drawFormat);
        delete drawFont;
        delete drawBrush;
        delete formGraphics;
    }

public void DrawString()
{
    System.Drawing.Graphics formGraphics = this.CreateGraphics();
    string drawString = "Sample Text";
    System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
    System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
    float x = 150.0F;
    float y = 50.0F;
    System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
    formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
    drawFont.Dispose();
    drawBrush.Dispose();
    formGraphics.Dispose();
}

Public Sub DrawString()
    Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
    Dim drawString As String = "Sample Text"
    Dim drawFont As New System.Drawing.Font("Arial", 16)
    Dim drawBrush As New _
       System.Drawing.SolidBrush(System.Drawing.Color.Black)
    Dim x As Single = 150.0
    Dim y As Single = 50.0
    Dim drawFormat As New System.Drawing.StringFormat
    formGraphics.DrawString(drawString, drawFont, drawBrush, _
        x, y, drawFormat)
    drawFont.Dispose()
    drawBrush.Dispose()
    formGraphics.Dispose()
End Sub

코드 컴파일

Load 이벤트 처리기에서 DrawString 메서드를 호출할 수 없습니다. 양식의 크기가 조정되거나 다른 양식에 의해 가려진 경우 그려진 콘텐츠는 다시 그려지지 않습니다. 콘텐츠가 자동으로 다시 그려지도록 하려면 OnPaint 메서드를 재정의해야 합니다.

강력한 프로그래밍

다음 조건에서 예외가 발생합니다.

  • Arial 글꼴이 설치되어 있지 않습니다.

참고 항목