路徑 (GDI+)

路徑是由結合線條、矩形和簡單曲線所形成。 回想一下 向量圖形的概 觀,下列基本建置組塊已證明最適合繪製圖片。

  • 線條
  • 矩形
  • 橢圓形
  • 多邊形
  • 基數曲線
  • Bézier 曲線

在 Windows GDI+中, GraphicsPath 物件可讓您將這些建置組塊的序列收集成單一單位。 然後,您可以透過呼叫Graphics類別的Graphics::D rawPath方法繪製整個線條、矩形、多邊形和曲線。 下圖顯示結合線條、弧線、Bézier 曲線和基數曲線所建立的路徑。

結合線條、弧線、Bezier 曲線和基數曲線的路徑圖例

GraphicsPath類別提供下列方法來建立要繪製的專案序列:AddLine、AddRectangleAddEllipseAddArcAddPolygonAddCurve (用於基數曲線) 和AddBezier。 每個方法都會多載;也就是說,每個方法都有數個不同參數清單的變化。 例如,AddLine 方法的一個變化會接收四個整數,而 AddLine 方法的另一個變化則會收到兩個 Point 物件。

將線條、矩形和 Bézier 曲線新增至路徑的方法具有複數隨附方法,可將數個專案新增至單一呼叫中的路徑: AddLinesAddRectanglesAddBeziers。 此外, AddCurve 方法也有隨附方法 AddClosedCurve,可將封閉曲線新增至路徑。

若要繪製路徑,您需要 Graphics 物件、 Pen 物件和 GraphicsPath 物件。 Graphics物件提供Graphics::D rawPath方法,而Pen物件會儲存路徑的屬性,例如線條寬度和色彩。 GraphicsPath物件會儲存組成路徑的線條、矩形和曲線序列。 Pen物件的位址和GraphicsPath物件會當做引數傳遞至Graphics::D rawPath方法。 下列範例會繪製包含線條、橢圓形和 Bézier 曲線的路徑。

myGraphicsPath.AddLine(0, 0, 30, 20);
myGraphicsPath.AddEllipse(20, 20, 20, 40);
myGraphicsPath.AddBezier(30, 60, 70, 60, 50, 30, 100, 10);
myGraphics.DrawPath(&myPen, &myGraphicsPath);

下圖顯示路徑。

由線條、橢圓形和 Bezier 曲線組成的路徑圖例

除了將線條、矩形和曲線新增至路徑之外,您還可以將路徑新增至路徑。 這可讓您結合現有的路徑,以形成大型的複雜路徑。 下列程式碼會將 graphicsPath1graphicsPath2 新增至 myGraphicsPathGraphicsPath::AddPath方法的第二個參數會指定新增的路徑是否連接到現有的路徑。

myGraphicsPath.AddPath(&graphicsPath1, FALSE);
myGraphicsPath.AddPath(&graphicsPath2, TRUE);

您可以新增至路徑的其他兩個專案:字串和 pies。 圓形圖是橢圓形內部的一部分。 下列範例會從弧形、基數曲線、字串和圓形圖建立路徑。

myGraphicsPath.AddArc(0, 0, 30, 20, -90, 180);
myGraphicsPath.AddCurve(myPointArray, 3);
myGraphicsPath.AddString(L"a string in a path", 18, &myFontFamily, 
   0, 24, myPointF, &myStringFormat);
myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110);
myGraphics.DrawPath(&myPen, &myGraphicsPath);

下圖顯示路徑。 請注意,路徑不需要連接;弧形、基數曲線、字串和圓形圖會分開。

由中斷連線線條組成的路徑圖例:弧線、基數曲線、字串和圓形圖