다음을 통해 공유


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

업데이트: 2007년 11월

다음 코드 예제에서는 GraphicsDrawString 메서드를 사용하여 폼에 텍스트를 그리는 방법을 보여 줍니다. 또는 TextRenderer를 사용하여 폼에 텍스트를 그릴 수도 있습니다. 자세한 내용은 방법: GDI를 사용하여 텍스트 그리기를 참조하십시오.

예제

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

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:
    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;
    }

코드 컴파일

Load 이벤트 처리기에서 DrawString 메서드를 호출할 수 없습니다. 그려진 내용은 폼 크기가 조정되거나 다른 폼에 의해 가려지더라도 새로 그려지지 않습니다. 내용이 자동으로 다시 그려지려면 OnPaint 메서드를 재정의해야 합니다.

강력한 프로그래밍

다음 조건에서 예외가 발생할 수 있습니다.

  • Arial 글꼴이 설치되지 않은 경우

참고 항목

작업

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

참조

DrawString

DrawText

FormatFlags

StringFormatFlags

TextFormatFlags

OnPaint

기타 리소스

그래픽 프로그래밍 시작