共用方式為


建立圖形物件

若要在任何顯示裝置上繪製圖形,需要有 Graphics 物件。Graphics 物件和繪圖表面關聯,通常是 Form 物件的工作區 (Client Area)。請使用下列其中一種技巧來取得要繪製到 Form 中的 Graphics 物件。

使用 CreateGraphics 方法

Form.CreateGraphics 方法可用來建立繪製到 Form 物件中的 Graphics 物件。

下列範例會建立 Form 類別的子類別 (Subclass),稱為它的 CreateGraphics 方法,並使用產生的 Graphics 物件在表單的工作區中繪製矩形:

Imports System
Imports System.Windows.Forms
Imports System.Drawing

'Create a Class that inherits from System.Windows.Forms.Form. 
Class myForm 
   Inherits Form 
   
   'Override myForm's OnClick event. 
   Protected Overrides Sub OnClick(ByVal e As EventArgs) 

      'Use the CreateGraphics method to create a Graphics object. 
       Dim formGraphics As Graphics
       formGraphics = Me.CreateGraphics

      'Create a red brush. 
      Dim redBrush As new SolidBrush(Color.Red)

      'Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100)
   End Sub 'OnClick

   Public Shared Sub Main() 
      Application.Run(new myForm())
   End Sub 'Main

End Class

[C#]
using System;
using System.Windows.Forms;
using System.Drawing;

//Create a Class that inherits from System.Windows.Forms.Form. 
class myForm : Form {
   
   //Override myForm's OnClick event. 
  protected override void OnClick(EventArgs e) {

      //Use the CreateGraphics method to create a Graphics object. 
      Graphics formGraphics = this.CreateGraphics();

      //Create a red brush. 
      SolidBrush redBrush = new SolidBrush(Color.Red);

      //Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100);
   }

   public static void Main() {
      Application.Run(new myForm());
   }
}

覆寫 OnPaint 事件處理常式

Form 類別的 OnPaint 方法會收到 PaintEventArgs 物件當成參數。這個物件的其中一個成員是和表單關聯的 Graphics 物件。

下列範例會覆寫 Form 類別的 OnPaint 方法,並使用其 PaintEventArgs 參數的 Graphics 物件在表單的工作區中繪製矩形:

Imports System.Windows.Forms
Imports System.Drawing

'Create a Class that inherits from System.Windows.Forms.Form. 
Class myForm 
   Inherits Form 
   
   'Override myForm's OnPaint event handler. 
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 

      'Use the Graphics object from the PaintEventArgs object. 
       Dim formGraphics As Graphics
       formGraphics = e.Graphics

      'Create a red brush. 
      Dim redBrush As new SolidBrush(Color.Red)

      'Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100)
   End Sub 'OnClick

   Public Shared Sub Main() 
      Application.Run(new myForm())
   End Sub 'Main

End Class

[C#]
using System;
using System.Windows.Forms;
using System.Drawing;

//Create a Class that inherits from System.Windows.Forms.Form. 
class myForm : Form {
   
   //Override myForm's OnPaint event. 
  protected override void OnPaint(PaintEventArgs e) {

      //Get the Graphics object from the PaintEventArgs object. 
      Graphics formGraphics = e.CreateGraphics();

      //Create a red brush. 
      SolidBrush redBrush = new SolidBrush(Color.Red);

      //Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100);
   }

   public static void Main() {
      Application.Run(new myForm());
   }
}