다음을 통해 공유


방법: 윤곽선이 있는 도형 그리기

업데이트: 2007년 11월

이 예제에서는 폼에서 윤곽선이 있는 타원과 사각형을 그립니다.

예제

Private Sub DrawEllipse()
    Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
    Dim formGraphics As System.Drawing.Graphics
    formGraphics = Me.CreateGraphics()
    formGraphics.DrawEllipse(myPen, New Rectangle(0, 0, 200, 300))
    myPen.Dispose()
    formGraphics.Dispose()
End Sub

Private Sub DrawRectangle()
    Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
    Dim formGraphics As System.Drawing.Graphics
    formGraphics = Me.CreateGraphics()
    formGraphics.DrawRectangle(myPen, New Rectangle(0, 0, 200, 300))
    myPen.Dispose()
    formGraphics.Dispose()
End Sub

private void DrawEllipse()
{
    System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
    System.Drawing.Graphics formGraphics;
    formGraphics = this.CreateGraphics();
    formGraphics.DrawEllipse(myPen, new Rectangle(0, 0, 200, 300));
    myPen.Dispose();
    formGraphics.Dispose();
}

private void DrawRectangle()
{
    System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
    System.Drawing.Graphics formGraphics;
    formGraphics = this.CreateGraphics();
    formGraphics.DrawRectangle(myPen, new Rectangle(0, 0, 200, 300));
    myPen.Dispose();
    formGraphics.Dispose();
}

private:
    void DrawEllipse()
    {
        System::Drawing::Pen^ myPen =
            gcnew System::Drawing::Pen(System::Drawing::Color::Red);
        System::Drawing::Graphics^ formGraphics;
        formGraphics = this->CreateGraphics();
        formGraphics->DrawEllipse(myPen, Rectangle(0, 0, 200, 300));
        delete myPen;
        delete formGraphics;
    }

private:
    void DrawRectangle()
    {
        System::Drawing::Pen^ myPen =
            gcnew System::Drawing::Pen(System::Drawing::Color::Red);
        System::Drawing::Graphics^ formGraphics;
        formGraphics = this->CreateGraphics();
        formGraphics->DrawRectangle(myPen, Rectangle(0, 0, 200, 300));
        delete myPen;
        delete formGraphics;
    }

코드 컴파일

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

강력한 프로그래밍

PenGraphics 개체와 같이 시스템 리소스를 소모하는 개체에 대해서는 항상 Dispose를 호출해야 합니다.

참고 항목

참조

DrawEllipse

OnPaint

DrawRectangle

기타 리소스

그래픽 프로그래밍 시작

펜을 사용하여 선과 도형 그리기

Windows Forms의 그래픽 및 그리기