Procedura: creare una forma con contornoHow to: Draw an Outlined Shape

In questo esempio disegna ellissi e rettangoli in un form.This example draws outlined ellipses and rectangles on a form.

EsempioExample

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

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 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

Compilazione del codiceCompiling the Code

Non è possibile chiamare questo metodo Load gestore dell'evento.You cannot call this method in the Load event handler. Se il form viene ridimensionato o nascosto da un altro formato, il contenuto disegnato non verrà ridisegnato.The drawn content will not be redrawn if the form is resized or obscured by another form. Per ridisegnare automaticamente il contenuto è necessario eseguire l'override di OnPaint metodo.To make your content automatically repaint, you should override the OnPaint method.

Programmazione efficienteRobust Programming

È necessario chiamare sempre Dispose in tutti gli oggetti che utilizzano le risorse di sistema, ad esempio Pen e Graphics oggetti.You should always call Dispose on any objects that consume system resources, such as Pen and Graphics objects.

Vedere ancheSee Also

DrawEllipse
OnPaint
DrawRectangle
Introduzione alla programmazione graficaGetting Started with Graphics Programming
Uso di un oggetto Pen per creare linee e formeUsing a Pen to Draw Lines and Shapes
Grafica e disegno in Windows FormGraphics and Drawing in Windows Forms