• Welcome to Jose's Read Only Forum 2023.
 

GDI+: Draw spheres

Started by José Roca, June 23, 2008, 06:01:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example draws spheres.


SUB GDIP_DrawSpheres (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL x AS LONG
   LOCAL y AS LONG
   LOCAL nSize AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath1 AS DWORD
   LOCAL pBrush1 AS DWORD
   LOCAL pPath2 AS DWORD
   LOCAL pBrush2 AS DWORD
   LOCAL MiddleColorToOpaque AS DWORD
   LOCAL BlackFullTranslucent AS DWORD

   hStatus = GdipCreateFromHDC(hDC, pGraphics)

   ' // Create a GraphicsPath object.
   hStatus = GdipCreatePath(%FillModeAlternate, pPath1)

   ' // Add an ellipse to the path.
   x = 100 : y = 50 : nSize = 220
   hStatus = GdipAddPathEllipseI(pPath1, x, y, nSize, nSize)

   ' // Create a path gradient based on the ellipse.
   hStatus = GdipCreatePathGradientFromPath(pPath1, pBrush1)

   ' // Set the middle color of the path.
   MiddleColorToOpaque = GDIP_ARGB_SetAlphaValue(%ARGB_MediumAquamarine, 0)
   hStatus = GdipSetPathGradientCenterColor(pBrush1, MiddleColorToOpaque)

   ' // Set the entire path boundary to Alpha Black using 50% translucency.
   BlackFullTranslucent = GDIP_ARGB_SetAlphaValue(%ARGB_Black, 128)
   hStatus = GdipSetPathGradientSurroundColorsWithCount(pBrush1, BlackFullTranslucent, 1)

   ' // Draw the ellipse, keeping the exact coordinates defined for the path,
   ' // and using antialising mode (+ 2 and - 4 are used to better achieve antialiasing)
   hStatus = GdipSetSmoothingMode(pGraphics, %SmoothingModeAntiAlias)
   hStatus = GdipFillEllipseI(pGraphics, pBrush1, x + 2, y + 2, nSize - 4, nSize - 4)

   ' // Create a second GraphicsPath object.
   hStatus = GdipCreatePath(%FillModeAlternate, pPath2)

   ' // Add an ellipse to the path
   x = 200 : y = 200 : nSize = 150
   hStatus = GdipAddPathEllipseI(pPath2, x, y, nSize, nSize)

   ' // Create a path gradient based on the ellipse.
   hStatus = GdipCreatePathGradientFromPath(pPath2, pBrush2)

   ' // Set the middle color of the path.
   MiddleColorToOpaque = GDIP_ARGB_SetAlphaValue(%ARGB_Yellow, 64)
   hStatus = GdipSetPathGradientCenterColor(pBrush2, MiddleColorToOpaque)

   ' // Set the entire path boundary to Alpha Black using 50% translucency
   BlackFullTranslucent = GDIP_ARGB_SetAlphaValue(%ARGB_Red, 128)
   CALL GdipSetPathGradientSurroundColorsWithCount(pBrush2, BlackFullTranslucent, 1)

   ' // Draw the ellipse, keeping the exact coords we defined for the path
   hStatus = GdipFillEllipseI(pGraphics, pBrush2, x + 2, y + 2, nSize - 4, nSize - 4)

  ' // Cleanup
   IF pPath1 THEN GdipDeletePath(pPath1)
   IF pPath2 THEN GdipDeletePath(pPath2)
   IF pBrush1 THEN GdipDeleteBrush(pBrush1)
   IF pBrush2 THEN GdipDeleteBrush(pBrush2)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB