閱讀英文

共用方式為


如何:繪製外框形狀

本範例會在表單上繪製外框橢圓形和矩形。

範例

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

編譯程式碼

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

穩固程式設計

您應該針對您所建立的任何會取用系統資源的物件一律呼叫 Dispose。 在上一個範例中,會建立 PenGraphics 物件,然後清除。

另請參閱