Share via


如何:在 Windows Form 上繪製實心矩形

本範例會在表單上繪製填滿的矩形。

範例

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

編譯程式碼

您無法在 Load 事件處理常式中呼叫這個方法。 如果表單調整大小或被另一個表單遮蔽,則繪製的內容將不會重新繪製。 若要讓內容自動重繪,您應該覆寫 OnPaint 方法。

穩固程式設計

您應該一律在任何取用系統資源的物件上呼叫 Dispose ,例如 BrushGraphics 物件。

另請參閱