Procedimiento para dibujar un rectángulo con relleno en un formulario Windows Forms

En este ejemplo se dibuja un rectángulo con relleno en un formulario.

Ejemplo

System::Drawing::SolidBrush^ myBrush =
    gcnew System::Drawing::SolidBrush(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->FillRectangle(myBrush, Rectangle(0, 0, 200, 300));
delete myBrush;
delete formGraphics;
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.FillRectangle(myBrush, New Rectangle(0, 0, 200, 300))
myBrush.Dispose()
formGraphics.Dispose()

Compilar el código

No se puede llamar a este método en el controlador de eventos Load. El contenido dibujado no se volverá a dibujar si se cambia el tamaño del formulario o si otro formulario lo oculta. Para que el contenido se vuelva a pintar automáticamente, debe invalidar el método OnPaint.

Programación sólida

Siempre debe llamar a Dispose en los objetos que consumen recursos del sistema, como los objetos Brush y Graphics.

Consulte también