Freigeben über


Erstellen eines Grafikobjekts

Um Grafiken für beliebige Anzeigegeräte zu zeichnen, benötigen Sie ein Graphics-Objekt. Ein Graphics-Objekt ist mit einer Zeichenoberfläche verknüpft, normalerweise mit dem Clientbereich eines Form-Objekts. Verwenden Sie eines der folgenden Verfahren, um Grafikobjekte von einem Graphics-Objekt in ein Form-Objekt zeichnen zu lassen.

Verwenden der CreateGraphics-Methode

Verwenden Sie die Form.CreateGraphics-Methode, um ein Graphics-Objekt zu erstellen, das in ein Form-Objekt zeichnet.

Im folgenden Beispiel wird eine Unterklasse der Form-Klasse erstellt. Anschließend wird dessen CreateGraphics-Methode aufgerufen und mit Hilfe des resultierenden Graphics-Objekts ein Rechteck im Clientbereich des Formulars gezeichnet:

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

Überschreiben des OnPaint-Ereignishandlers

Die OnPaint-Methode einer Form-Klasse empfängt ein PaintEventArgs-Objekt als Parameter. Einer der Member dieses Objekts ist ein mit dem Formular verknüpftes Graphics-Objekt.

Im folgenden Beispiel wird die OnPaint-Methode einer Form-Klasse überschrieben und mit Hilfe des Graphics-Objekts aus ihrem PaintEventArgs-Parameter im Clientbereich des Formulars ein Rechteck gezeichnet:

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