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 FlattenExample(HDC hdc)
{
Graphics graphics(hdc);
// Begin example.
Point pts[] = {Point(20,50),
Point(40,70),
Point(60,10),
Point(80,50)};
GraphicsPath path;
path.AddCurve(pts, 4);
path.AddEllipse(20, 100, 150, 80);
path.AddBezier(20, 200, 20, 250, 50, 210, 100, 260);
// Draw the path before flattening.
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
path.Flatten(NULL, 8.0f);
// Draw the flattened path.
pen.SetColor(Color(255, 0, 255, 0));
graphics.DrawPath(&pen, &path);
// Get the path data from the flattened path.
PathData pathData;
path.GetPathData(&pathData);
// Draw the data points of the flattened path.
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_FlattenPath (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPath AS DWORD
LOCAL pRedPen AS DWORD
LOCAL pBluePen AS DWORD
LOCAL pBrush AS DWORD
LOCAL pData AS PathData
LOCAL i AS LONG
LOCAL count AS LONG
DIM pts(3) AS POINTF
DIM DataPoints(0) AS POINTF
DIM DataTypes(0) AS BYTE
hStatus = GdipCreateFromHDC(hdc, pGraphics)
hStatus = GdipCreatePath(%FillModeAlternate, pPath)
pts(0).x = 20 : pts(0).y = 50
pts(1).x = 40 : pts(1).y = 70
pts(2).x = 60 : pts(2).y = 10
pts(3).x = 80 : pts(3).y = 50
hStatus = GdipAddPathCurve(pPath, pts(0), 4)
hStatus = GdipAddPathEllipse(pPath, 20, 100, 150, 80)
hStatus = GdipAddPathBezier(pPath, 20, 200, 20, 250, 50, 210, 100, 260)
' // Draw the path before flattening.
hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitWorld, pRedPen)
hStatus = GdipDrawPath(pGraphics, pRedPen, pPath)
' // Flatten the path.
hStatus = GdipFlattenPath(pPath, %NULL, 8)
' // Draw the flattened path.
hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 255), 1, %UnitWorld, pBluePen)
hStatus = GdipDrawPath(pGraphics, pBluePen, pPath)
' // Create a solid brush.
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pBrush)
' // Get the path data from the flattened path.
' // 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
' // Draw the data points of the flattened path.
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 pRedPen THEN GdipDeletePen(pRedPen)
IF pBluePen THEN GdipDeletePen(pBluePen)
IF pPath THEN GdipDeletePath(pPath)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipFlattenPath.png)