The following example creates and draws a
path that has a line, a rectangle, an ellipse, and a curve. The code gets the
path's points and types by passing the address of a
PathData structure to the
GdipGetPathData function. Then the code draws each of the
path's data points.
C++
VOID GetPathDataExample(HDC hdc)
{
Graphics graphics(hdc);
// Create a path that has a line, a rectangle, an ellipse, and a curve.
GraphicsPath path;
PointF points[] = {
PointF(200, 200),
PointF(250, 240),
PointF(200, 300),
PointF(300, 310),
PointF(250, 350)};
path.AddLine(20, 100, 150, 200);
path.AddRectangle(Rect(40, 30, 80, 60));
path.AddEllipse(Rect(200, 30, 200, 100));
path.AddCurve(points, 5);
// Draw the path.
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
// Get the path data.
PathData pathData;
path.GetPathData(&pathData);
// Draw the path's data points.
SolidBrush brush(Color(255, 255, 0, 0));
for(INT j = 0; j < pathData.Count; ++j)
{
graphics.FillEllipse(
&brush,
pathData.Points[j].X - 3.0f,
pathData.Points[j].Y - 3.0f,
6.0f,
6.0f);
}
}
PowerBASIC
SUB GDIP_GetPathData (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPath AS DWORD
LOCAL pBluePen AS DWORD
LOCAL pBrush AS DWORD
LOCAL pData AS PathData
LOCAL i AS LONG
LOCAL count AS LONG
DIM pts(4) AS POINTF
DIM DataPoints(0) AS POINTF
DIM DataTypes(0) AS BYTE
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a path that has a line, a rectangle, an ellipse, and a curve.
hStatus = GdipCreatePath(%FillModeAlternate, pPath)
pts(0).x = 200 : pts(0).y = 200
pts(1).x = 250 : pts(1).y = 240
pts(2).x = 200 : pts(2).y = 300
pts(3).x = 300 : pts(3).y = 310
pts(4).x = 250 : pts(4).y = 350
hStatus = GdipAddPathLine(pPath, 20, 100, 150, 200)
hStatus = GdipAddPathRectangle(pPath, 40, 30, 80, 60)
hStatus = GdipAddPathEllipse(pPath, 200, 30, 200, 100)
hStatus = GdipAddPathCurve(pPath, pts(0), 5)
' // Draw the path
hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 255), 1, %UnitWorld, pBluePen)
hStatus = GdipDrawPath(pGraphics, pBluePen, pPath)
' // Get the path data.
' // We have to allocate the needed arrays.
' // GdipGetPathData fills them with the data.
hStatus = GdipGetPointCount(pPath, count)
IF count THEN
REDIM DataPoints(count - 1)
REDIM DataTypes(count - 1)
pData.Count = count
pData.Points = VARPTR(DataPoints(0))
pData.Types = VARPTR(DataTypes(0))
hStatus = GdipGetPathData(pPath, pData)
END IF
' // Create a solid brush.
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pBrush)
' // Draw the path's data points.
FOR i = 0 TO pData.Count - 1
hStatus = GdipFillEllipse(pGraphics, pBrush, pData.@Points[i].x - 3, pData.@Points[i].y - 3, 6, 6)
NEXT
' // Cleanup
IF pBrush THEN GdipDeleteBrush(pBrush)
IF pBluePen THEN GdipDeletePen(pBluePen)
IF pPath THEN GdipDeletePath(pPath)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipGetPathData.png)