• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipGetPathPoints

Started by José Roca, June 22, 2008, 05:45:40 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates and draws a path that has a line, a rectangle, an ellipse, and a curve. The code calls the path's GdipGetPointCount function to determine the number of data points that are stored in the path. The code allocates a buffer large enough to receive the array of data points and passes the address of that buffer to the GdipGetPathPoints function. Finally, the code draws each of the path's data points.

C++


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

   // Create a path that has a line, a rectangle, an ellipse, and a curve.
   GraphicsPath path;
   
   PointF[] = {
      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 points.
   INT count = path.GetPointCount();
   PointF* dataPoints = new PointF[count]; 
   path.GetPathPoints(dataPoints, count);

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


PowerBASIC


SUB GDIP_GetPathPoints (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pPen AS DWORD
   LOCAL pBrush AS DWORD
   LOCAL i AS LONG
   LOCAL count AS LONG
   DIM   pts(4) AS POINTF
   DIM   DataPoints(0) AS POINTF

   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, pPen)
   hStatus = GdipDrawPath(pGraphics, pPen, pPath)

   ' // Get the path points
   hStatus = GdipGetPointCount(pPath, count)
   IF count THEN
      REDIM DataPoints(count - 1)
      hStatus = GdipGetPathPoints(pPath, DataPoints(0), count)
   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 count - 1
      hStatus = GdipFillEllipse(pGraphics, pBrush, DataPoints(i).x - 3, DataPoints(i).y - 3, 6, 6)
   NEXT

   ' // Cleanup
   IF pBrush THEN GdipDeleteBrush(pBrush)
   IF pPen THEN GdipDeletePen(pPen)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB