HOW TO:從直線、曲線和形狀建立圖形
若要建立圖形,請建構 GraphicsPath,然後呼叫方法 (例如 AddLine 和 AddCurve),將基本圖形加入至路徑。
範例
下列程式碼範例會建立含有圖形的路徑:
第一個範例會建立包含單一圖形的路徑。 圖形只包含單一弧形。 這個弧形的散出角度為 -180 度,也就是預設座標系統的逆時針方向。
第二個範例會建立包含兩個圖形的路徑。 第一個圖形是弧形,後面跟著一條直線。 第二個圖形是一條直線,後面跟著一條曲線再加上一條直線。 第一個圖形的左邊是開放的,第二個圖形是封閉的。
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)
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);
' 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)
// 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);
編譯程式碼
上述範例是專為與 Windows Form 搭配使用而設計的,而且它們需要 PaintEventArgs e (即 Paint 事件處理常式的參數)。