Freigeben über


Vorgehensweise: Erstellen von Figuren aus Linien, Kurven und Formen

Erstellen Sie zum Erstellen einer Figur eine GraphicsPath-Klasse, und rufen Sie anschließend Methoden wie AddLine und AddCurve auf, um Primitiven zum Pfad hinzuzufügen.

Beispiel

Die folgenden Codebeispiele erstellen Pfade mit Figuren:

  • Im ersten Beispiel wird ein Pfad mit einer einzelne Figur erstellt. Die Figur besteht aus einem einzelnen Bogen. Der Bogen hat einen Mittelpunktswinkel von -180 Grad (entgegen dem Uhrzeigersinn im Standardkoordinatensystem).

  • Im zweiten Beispiel wird ein Pfad mit zwei Figuren erstellt. Die erste Figur ist ein Bogen gefolgt von einer Linie. Die zweite Figur ist eine Linie gefolgt von einer Kurve gefolgt von einer Linie. Die erste Figur ist offen, die zweite Figur geschlossen.

GraphicsPath path = new GraphicsPath();
path.AddArc(175, 50, 50, 50, 0, -180);
e.Graphics.DrawPath(new Pen(Color.FromArgb(128, 255, 0, 0), 4), path);
Dim path As New GraphicsPath()
path.AddArc(175, 50, 50, 50, 0, -180)
e.Graphics.DrawPath(New Pen(Color.FromArgb(128, 255, 0, 0), 4), path)

     // Create an array of points for the curve in the second figure.
     Point[] points = {
new Point(40, 60),
new Point(50, 70),
new Point(30, 90)};

     GraphicsPath path = new GraphicsPath();

     path.StartFigure(); // Start the first figure.
     path.AddArc(175, 50, 50, 50, 0, -180);
     path.AddLine(100, 0, 250, 20);
     // First figure is not closed.

     path.StartFigure(); // Start the second figure.
     path.AddLine(50, 20, 5, 90);
     path.AddCurve(points, 3);
     path.AddLine(50, 150, 150, 180);
     path.CloseFigure(); // Second figure is closed.

     e.Graphics.DrawPath(new Pen(Color.FromArgb(255, 255, 0, 0), 2), path);
' Create an array of points for the curve in the second figure.
Dim points As Point() = { _
   New Point(40, 60), _
   New Point(50, 70), _
   New Point(30, 90)}

Dim path As New GraphicsPath()

path.StartFigure() ' Start the first figure.
path.AddArc(175, 50, 50, 50, 0, -180)
path.AddLine(100, 0, 250, 20)
' First figure is not closed.

path.StartFigure() ' Start the second figure.
path.AddLine(50, 20, 5, 90)
path.AddCurve(points, 3)
path.AddLine(50, 150, 150, 180)
path.CloseFigure() ' Second figure is closed.
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 2), path)

Kompilieren des Codes

Die obigen Beispiele sind für die Verwendung in Windows Forms konzipiert und erfordern die PaintEventArgse-Klasse, die ein Parameter des Paint-Ereignishandlers ist.

Weitere Informationen