• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipPathIterNextSubpath

Started by José Roca, June 23, 2008, 01:45:42 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a GraphicsPath object and adds five figures to the path. The code passes the address of that GraphicsPath object to the GdipCreatePathIter function to create an iterator that is associated with the path. The code calls the GdipPathIterNextSubpath function three times to obtain the starting index and the ending index of the path's third figure. Then the code calls the GdipPathIterCopyData function to retrieve the third figure's data points.

C++


VOID NextSubpathExample2(HDC hdc)
{
   Graphics graphics(hdc);

   // Create a graphics path with five figures (subpaths).
   GraphicsPath path;

   path.AddRectangle(Rect(20, 20, 60, 30));   // Subpath count is 1.

   path.AddLine(100, 20, 160, 50);            // Subpath count is 2.
   path.AddArc(180, 20, 60, 30, 0.0f, 180.0f);

   path.AddRectangle(Rect(260, 20, 60, 30));  // Subpath count is 3.

   path.AddLine(340, 20, 400, 50);            // Subpath count is 4.
   path.AddArc(340, 20, 60, 30, 0.0f, 180.0f);
   path.CloseFigure();
 
   path.AddRectangle(Rect(420, 20, 60, 30));  // Subpath count is 5.

   // Create an iterator, and associate it with the path.
   GraphicsPathIterator iterator(&path);

   // Call NextSubpath three times to get the starting and ending
   // indices for the third figure.
   INT start;
   INT end;
   BOOL isClosed;
   INT count;
   count = iterator.NextSubpath(&start, &end, &isClosed);
   count = iterator.NextSubpath(&start, &end, &isClosed);
   count = iterator.NextSubpath(&start, &end, &isClosed);

   // Get the third figure's data points.
   PointF* points = new PointF[count];
   BYTE* types = new BYTE[count];
   iterator.CopyData(points, types, start, end);

   // Draw the third figure's data points.
   SolidBrush brush(Color(255, 255, 0, 0));
   for(INT j = 0; j < count; ++j)
      graphics.FillEllipse(
         &brush,
         points[j].X - 3.0f,
         points[j].Y - 3.0f,
         6.0f,
         6.0f);

   delete points;
   delete types;
}


PowerBASIC


SUB GDIP_PathIterNextSubpath (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pIterator AS DWORD
   LOCAL pBrush AS DWORD
   LOCAL count AS LONG
   LOCAL resultCount AS LONG
   LOCAL startIndex AS LONG
   LOCAL endIndex AS LONG
   LOCAL isClosed AS LONG
   LOCAL i AS LONG
   DIM   pPoints(0) AS POINTF
   DIM   pTypes(0) AS BYTE

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create a graphics path with five figures (subpaths).
   hStatus = GdipCreatePath(%FillModeAlternate, pPath)

   hStatus = GdipAddPathRectangle(pPath, 20, 20, 60, 30)       ' // Subpath count is 1.

   hStatus = GdipAddPathLine(pPath, 100, 20, 160, 50)          ' // Subpath count is 2.
   hStatus = GdipAddPathArc(pPath, 180, 20, 60, 30, 0, 180)

   hStatus = GdipAddPathRectangle(pPath, 260, 20, 60, 30)      ' // Subpath count is 3.

   hStatus = GdipAddPathLine(pPath, 340, 20, 400, 50)          ' // Subpath count is 4.
   hStatus = GdipAddPathArc(pPath, 340, 20, 60, 30, 0, 180)
   hStatus = GdipClosePathFigure(pPath)

   hStatus = GdipAddPathRectangle(pPath, 420, 20, 60, 30)      ' // Subpath count is 5.

   ' // Create an iterator, and associate it with the path.
   hStatus = GdipCreatePathIter(pIterator, pPath)

   ' // Call NextSubpath three times to get the starting and ending indices for the third figure.
   hStatus = GdipPathIterNextSubpath(pIterator, count, startIndex, endIndex, isClosed)
   hStatus = GdipPathIterNextSubpath(pIterator, count, startIndex, endIndex, isClosed)
   hStatus = GdipPathIterNextSubpath(pIterator, count, startIndex, endIndex, isClosed)

   ' // Get the third figure's data points.
   IF count THEN
      REDIM pPoints(count - 1)
      REDIM pTypes(count - 1)
      hStatus = GdipPathIterCopyData(pIterator, resultCount, pPoints(0), pTypes(0), startIndex, endIndex)
      ' // Draw the third figure's data points.
      hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pBrush)
      FOR i = 0 TO count - 1
         hStatus = GdipFillEllipse(pGraphics, pBrush, pPoints(i).x - 3, pPoints(i).y - 3, 6, 6)
      NEXT
   END IF

   ' // Cleanup
   IF pBrush THEN GdipDeleteBrush(pBrush)
   IF pIterator THEN GdipDeletePathIter(pIterator)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB