Share via


Comment : dessiner une forme avec contour

Mise à jour : novembre 2007

Cet exemple dessine des ellipses et des rectangles avec contour dans un formulaire.

Exemple

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

Compilation du code

Vous ne pouvez pas appeler cette méthode dans le gestionnaire d'événements Load. Le contenu dessiné ne sera pas redessiné si le formulaire est redimensionné ou s'il est occulté par un autre formulaire. Pour que votre contenu soit automatiquement repeint, vous devez substituer la méthode OnPaint.

Programmation fiable

Veillez à toujours appeler Dispose sur les objets qui utilisent des ressources système, tels que les objets Pen et Graphics.

Voir aussi

Référence

DrawEllipse

OnPaint

DrawRectangle

Autres ressources

Mise en route de la programmation graphique

Utilisation d'un stylet pour dessiner des lignes et des formes

Graphiques et dessins dans les Windows Forms