Nasıl yapılır: Çizgiler, Eğriler ve Şekillerden Şekiller Oluşturma

Bir şekil oluşturmak için, yola temel öğeler eklemek için bir oluşturun ve ardından ve gibi GraphicsPathAddLine yöntemleri AddCurve çağırarak.

Örnek

Aşağıdaki kod örnekleri, şekillere sahip yollar oluşturmanızı sağlar:

  • İlk örnek, tek bir şekilde bir yol oluşturur. Şekilde tek bir yay yer almaktadır. Yay, varsayılan koordinat sisteminde saat yönünün tersi olan –180 derecelik bir tarama açısına sahiptir.

  • İkinci örnek, iki şekilden bir yol oluşturur. İlk şekil, ardından bir çizgi gelen bir yaydır. İkinci şekil, ardından bir eğri ve ardından bir çizgi gelen bir çizgidir. İlk şekil açık, ikinci şekil ise kapalıdır.

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)

Kod Derleniyor

Önceki örnekler, Windows Forms ile birlikte kullanım için tasarlanmıştır ve olay işleyicinin PaintEventArgse bir parametresi olan Paint parametresini gerektirir.

Ayrıca bkz.