• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipDrawBeziers

Started by José Roca, June 23, 2008, 07:19:54 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example draws a pair of Bézier curves.

C++


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

   // Define a Pen object and an array of PointF objects.
   Pen greenPen(Color(255, 0, 255, 0), 3);

   PointF startPoint(100.0f, 100.0f);
   PointF ctrlPoint1(200.0f, 50.0f);
   PointF ctrlPoint2(400.0f, 10.0f);
   PointF endPoint1(500.0f, 100.0f);
   PointF ctrlPoint3(600.0f, 200.0f);
   PointF ctrlPoint4(700.0f, 400.0f);
   PointF endPoint2(500.0f, 500.0f);

   PointF curvePoints[7] = {
      startPoint,
      ctrlPoint1,
      ctrlPoint2,
      endPoint1,
      ctrlPoint3,
      ctrlPoint4,
      endPoint2};

   // Draw the Bezier curves.
   graphics.DrawBeziers(&greenPen, curvePoints, 7);

   // Draw the control and end points.
   SolidBrush redBrush(Color(255, 255, 0, 0));
   graphics.FillEllipse(&redBrush, Rect(100 - 5, 100 - 5, 10, 10));
   graphics.FillEllipse(&redBrush, Rect(500 - 5, 100 - 5, 10, 10));
   graphics.FillEllipse(&redBrush, Rect(500 - 5, 500 - 5, 10, 10));
   SolidBrush blueBrush(Color(255, 0, 0, 255));
   graphics.FillEllipse(&blueBrush, Rect(200 - 5, 50 - 5, 10, 10));
   graphics.FillEllipse(&blueBrush, Rect(400 - 5, 10 - 5, 10, 10));
   graphics.FillEllipse(&blueBrush, Rect(600 - 5, 200 - 5, 10, 10));
   graphics.FillEllipse(&blueBrush, Rect(700 - 5, 400 - 5, 10, 10));
}


PowerBASIC


SUB GDIP_DrawBeziers (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pGreenPen AS DWORD
   LOCAL pRedBrush AS DWORD
   LOCAL pBlueBrush AS DWORD
   LOCAL startPoint AS POINTF
   LOCAL ctrlPoint1 AS POINTF
   LOCAL ctrlPoint2 AS POINTF
   LOCAL endPoint1 AS POINTF
   LOCAL ctrlPoint3 AS POINTF
   LOCAL ctrlPoint4 AS POINTF
   LOCAL endPoint2 AS POINTF
   DIM   curvePoints(6) AS POINTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create a green Pen
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 255, 0), 1.0!, %UnitWorld, pGreenPen)

   startPoint.x = 100.0 : startPoint.y = 100.0
   ctrlPoint1.x = 200.0 : ctrlPoint1.y = 50.0
   ctrlPoint2.x = 400.0 : ctrlPoint2.y = 10.0
   endPoint1.x = 500.0 : endPoint1.y = 100.0
   ctrlPoint3.x = 600.0 : ctrlPoint3.y = 200.0
   ctrlPoint4.x = 700.0 : ctrlPoint4.y = 400.0
   endPoint1.x = 500.0 : endPoint1.y = 500.0

   curvePoints(0) = startPoint
   curvePoints(1) = ctrlPoint1
   curvePoints(2) = ctrlPoint2
   curvePoints(3) = endPoint1
   curvePoints(4) = ctrlPoint3
   curvePoints(5) = ctrlPoint4
   curvePoints(6) = endPoint2

   ' // Draw the Bézier curves
   hStatus = GdipDrawBeziers(pGraphics, pGreenPen, curvePoints(0), 7)

  ' // Create the brushes
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pRedBrush)
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), pBlueBrush)

   ' //Draw the end points and control points.
   hStatus = GdipFillEllipse(pGraphics, pRedBrush, 100 - 5, 100 - 5, 10, 10)
   hStatus = GdipFillEllipse(pGraphics, pRedBrush, 500 - 5, 100 - 5, 10, 10)
   hStatus = GdipFillEllipse(pGraphics, pRedBrush, 500 - 5, 500 - 5, 10, 10)
   hStatus = GdipFillEllipse(pGraphics, pBlueBrush, 200 - 5, 50 - 5, 10, 10)
   hStatus = GdipFillEllipse(pGraphics, pBlueBrush, 400 - 5, 10 - 5, 10, 10)
   hStatus = GdipFillEllipse(pGraphics, pBlueBrush, 600 - 5, 200 - 5, 10, 10)
   hStatus = GdipFillEllipse(pGraphics, pBlueBrush, 700 - 5, 400 - 5, 10, 10)

   ' // Cleanup
   IF pBlueBrush THEN GdipDeleteBrush(pBlueBrush)
   IF pRedBrush THEN GdipDeleteBrush(pRedBrush)
   IF pGreenPen THEN GdipDeletePen(pGreenPen)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB