Sdílet prostřednictvím


Postupy: Kreslení vyplněného obdélníku ve formuláři Windows

Tento příklad nakreslí vyplněný obdélník ve formuláři.

Příklad

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

Kompilace kódu

Tuto metodu nelze volat při zpracování události Load. Nakreslený obsah nebude překreslen, pokud je formulář změněn nebo zakryt jiným formulářem. Pokud chcete, aby se obsah automaticky překreslil, měli byste přepsat metodu OnPaint.

Robustní programování

Vždy byste měli volat Dispose u všech objektů, které spotřebovávají systémové prostředky, jako jsou Brush a Graphics objekty.

Viz také