閱讀英文

共用方式為


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

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

範例

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

編譯程式碼

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

穩固程式設計

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

另請參閱