如何:從直線、曲線和形狀建立圖形
若要建立圖形,請建構一個 GraphicsPath,然後呼叫方法 (例如 AddLine 和 AddCurve) 以將基本類型新增至路徑。
範例
以下程式碼範例會建立包含數字的路徑:
第一個範例會建立一條包含單一圖形的路徑。 該圖形由單一圓弧組成。圓弧的掃角為 –180 度,在預設座標系統中為逆時針方向。
第二個範例會建立一條包含兩個圖形的路徑。 第一個圖形是一條圓弧,後面接著一條直線。 第二個圖形是一條直線,後面接著一條曲線,後面再接著一條直線。 第一個圖形是保持開啟的,而第二個圖形是關閉的。
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)
編譯程式碼
前面的範例設計用於 Windows Forms,它們需要 PaintEventArgs e
,這是 Paint 事件處理常式的參數。