Results 1 to 1 of 1

Thread: PB/WIN - GDI+ flat API - translucent sphere

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    Blue planet
    Posts
    15

    PB/WIN - GDI+ flat API - translucent sphere

    This is a PowerBASIC example, but it could be easily translated to either C or VB.
    source code + exe could be downloaded from there:
    http://www.zapsolution.com/preview/gpsphere.zip
    (size of the ZIP file: 15880 bytes)
    (size of GPSPHERE.EXE 30720 bytes)
    No runtime required, except that the GDIPLUS.DLL must be already installed.
    Note: The GDIPLUS.DLL works with any Windows version except 95 and NT3.

    VB Code:
    1. ' gpSPHERE GDIPLUS Flat API example
    2. '
    3. ' Most GDIPLUS examples and features have been translated to PB/WIN
    4. ' in the Patrice Terrier's "GDI+ helper" graphic toolkit.
    5. '
    6. ' You can download a complete demo version of the GDIPLUS features at:
    7. ' [URL]http://www.zapsolution.com/preview/WinLIFT.exe[/URL]
    8. ' The name of the main demo is PICTURE.EXE
    9. '
    10. ' Note: the "GDI+ helper" graphic toolkit do not require the use of WinLIFT
    11. ' however because WinLIFT is itself a graphic package some of its graphic's
    12. ' features are also explained in the PICTURE.EXE demo source code that is
    13. ' provided to registered users of the "GDI+ helper" graphic toolkit.
    14. '
    15. ' This demo draws two spheres using the GDI+ AntiAlias mode.
    16. ' it is written in pure SDK code to reach the majority of the programming community.
    17. ' The GDIPLUS.INC could be find at:
    18. ' [URL]http://www.powerbasic.com/support/forums/Forum7/HTML/001616.html[/URL]
    19. '
    20. ' The GDIPLUS.DLL could be find there:
    21. ' [URL]http://www.zapsolution.com/preview/gdiplus.exe[/URL]
    22. '
    23. #COMPILE EXE "gpSPHERE.exe"
    24. #INCLUDE "win32api.inc"
    25. #INCLUDE "gdiplus.inc"
    26.  
    27. ' GDIPLUS Init (load the GDIPLUS.DLL)
    28. FUNCTION gdihStart&()
    29.  ' Load the GDI+ Dll
    30.    DIM GpInput AS GdiplusStartupInput
    31.    GpInput.GdiplusVersion = 1
    32.    IF GdiplusStartup(hGDIplus&, GpInput) = 0 THEN FUNCTION = hGDIplus&
    33. END FUNCTION
    34.  
    35. ' GDIPLUS unload (unload the GDIPLUS.DLL)
    36. SUB gdihEnd(BYVAL hGDIplus&)
    37.   ' Unload the GDI+ Dll
    38.     IF hGDIplus& THEN CALL GdiplusShutdown(hGDIplus&)
    39. END SUB
    40.  
    41. ' Helper function
    42. ' Use this in lieu of the Color class constructor
    43. FUNCTION gdihColorSetAlpha(BYVAL lColor AS LONG, BYVAL Alpha AS BYTE) AS LONG
    44.     DIM bytestruct AS COLORBYTES
    45.     DIM result AS COLORLONG
    46.     result.longval = lColor
    47.     LSET bytestruct = result
    48.     bytestruct.AlphaByte = Alpha
    49.     LSET result = bytestruct
    50.     FUNCTION = result.longval
    51. END FUNCTION
    52.  
    53. SUB DrawSphere(BYVAL hDC&)
    54.  
    55.   ' First Sphere coordinates
    56.     x& = 100: y& = 100: Size& = 200
    57.  
    58.     CALL GdipCreateFromHDC(hDC&, graphics&)
    59.  
    60.   ' BRUSH EFFECT
    61.   ' Create a GraphicsPath object
    62.     CALL GdipCreatePath(%FillModeAlternate, path1&)
    63.  
    64.   ' Add an ellipse to the path
    65.     CALL GdipAddPathEllipseI(path1&, x&, y&, Size&, Size&)
    66.  
    67.   ' Create a path gradient based on the ellipse
    68.     CALL GdipCreatePathGradientFromPath(path1&, brush1&)
    69.  
    70.   ' Set the middle color of the path
    71.     MiddleColorToOpaque& = gdihColorSetAlpha(%ColorsMediumAquamarine, 0)
    72.     CALL GdipSetPathGradientCenterColor(brush1&, MiddleColorToOpaque&)
    73.  
    74.   ' Set the entire path boundary to Alpha Black using 50% translucency
    75.     BlackFullTranslucent& = gdihColorSetAlpha(%ColorsBlack, 128)
    76.     CALL GdipSetPathGradientSurroundColorsWithCount(brush1&, BlackFullTranslucent&, 1)
    77.  
    78.   ' Draw the ellipse, keeping the exact coords we defined for the path
    79.   ' I want to use AntiAlias drawing mode
    80.     CALL GdipSetSmoothingMode(graphics&, %SmoothingModeAntiAlias)
    81.     CALL GdipFillEllipseI(graphics&, brush1&, x& + 2, y& + 2, Size& - 4, Size& - 4)
    82.   ' + 2 and - 4 are used to better achieve the AntiAlias
    83.  
    84.   ' Second Sphere coordinates
    85.     x& = 200: y& = 200: Size& = 150
    86.  
    87.   ' Create a GraphicsPath object
    88.     CALL GdipCreatePath(%FillModeAlternate, path2&)
    89.  
    90.   ' Add an ellipse to the path
    91.     CALL GdipAddPathEllipseI(path2&, x&, y&, Size&, Size&)
    92.  
    93.   ' Create a path gradient based on the ellipse
    94.     CALL GdipCreatePathGradientFromPath(path2&, brush2&)
    95.  
    96.   ' Set the middle color of the path
    97.     MiddleColorToOpaque& = gdihColorSetAlpha(%ColorsYellow, 64)
    98.     CALL GdipSetPathGradientCenterColor(brush2&, MiddleColorToOpaque&)
    99.  
    100.   ' Set the entire path boundary to Alpha Black using 50% translucency
    101.     BlackFullTranslucent& = gdihColorSetAlpha(%ColorsRed, 128)
    102.     CALL GdipSetPathGradientSurroundColorsWithCount(brush2&, BlackFullTranslucent&, 1)
    103.  
    104.   ' Draw the ellipse, keeping the exact coords we defined for the path
    105.     CALL GdipFillEllipseI(graphics&, brush2&, x& + 2, y& + 2, Size& - 4, Size& - 4)
    106.   ' + 2 and - 4 are used to better achieve the AntiAlias
    107.  
    108.   ' Cleanup
    109.     CALL GdipDeletePath(path1&)
    110.     CALL GdipDeletePath(path2&)
    111.     CALL GdipDeleteBrush(brush1&)
    112.     CALL GdipDeleteBrush(brush2&)
    113.     CALL GdipDeleteGraphics(graphics&)
    114.  
    115. END SUB
    116.  
    117. FUNCTION WndProc(BYVAL hWnd& , BYVAL Msg&, BYVAL wParam&, BYVAL lParam&) AS LONG
    118.  
    119.     LOCAL ps AS PAINTSTRUCT
    120.  
    121.     SELECT CASE Msg&
    122.     CASE %WM_PAINT
    123.         hDC& = BeginPaint(hWnd&, ps)
    124.         CALL DrawSphere(hDC&)
    125.         CALL EndPaint(hWnd&, ps)
    126.  
    127.     CASE %WM_DESTROY
    128.         CALL PostQuitMessage(0)
    129.     END SELECT
    130.  
    131.     FUNCTION = DefWindowProc(hWnd&, Msg&, wParam&, lParam&)
    132.  
    133. END FUNCTION
    134.  
    135. FUNCTION WinMain (BYVAL hInstance     AS LONG, _
    136.                   BYVAL hPrevInstance AS LONG, _
    137.                   BYVAL lpCmdLine     AS ASCIIZ PTR, _
    138.                   BYVAL iCmdShow      AS LONG) AS LONG
    139.  
    140.     LOCAL msg AS tagMSG
    141.     LOCAL wc AS WNDCLASSEX
    142.     LOCAL szClassName AS ASCIIZ * 128
    143.     szClassName = "GDI+ demo"
    144.  
    145.   ' LOAD the GDI+ Engine
    146.     hGDIplus& = gdihStart
    147.     IF hGDIplus& THEN
    148.        IF ISFALSE(hPrevInstance&) THEN
    149.           wc.cbSize        = SIZEOF(wc)
    150.           wc.style         = %CS_HREDRAW OR %CS_VREDRAW
    151.           wc.lpfnWndProc   = CODEPTR(WndProc)
    152.           wc.cbClsExtra    = 0
    153.           wc.cbWndExtra    = 0
    154.           wc.hInstance     = hInstance&
    155.           wc.hIcon         = LoadIcon(%NULL, BYVAL %IDI_APPLICATION)
    156.           wc.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
    157.           wc.hbrBackground = GetStockObject(%WHITE_BRUSH)
    158.           wc.lpszMenuName  = %NULL
    159.           wc.lpszClassName = VARPTR(szClassName)
    160.           wc.hIconSm       = LoadIcon(%NULL, BYVAL %IDI_APPLICATION)
    161.           CALL RegisterClassEx(wc)
    162.        END IF
    163.        hWnd& = CreateWindowEx(0, _
    164.                               szClassName, _          ' window class name
    165.                               szClassName, _          ' window caption
    166.                               %WS_OVERLAPPEDWINDOW, _ ' window style
    167.                               %CW_USEDEFAULT, _       ' initial x position
    168.                               %CW_USEDEFAULT, _       ' initial y position
    169.                               %CW_USEDEFAULT, _       ' initial x size
    170.                               %CW_USEDEFAULT, _       ' initial y size
    171.                               %NULL, _                ' parent window handle
    172.                               %NULL, _                ' window menu handle
    173.                               hInstance&, _           ' program instance handle
    174.                               BYVAL %NULL)            ' creation parameters
    175.        IF hWnd& THEN                              
    176.           CALL ShowWindow(hWnd&, %SW_SHOW)
    177.           CALL UpdateWindow(hWnd&)
    178.  
    179.           DO WHILE GetMessage(msg, %NULL, 0, 0)
    180.               TranslateMessage msg
    181.               DispatchMessage msg
    182.           LOOP
    183.  
    184.           FUNCTION = msg.wParam
    185.        END IF
    186.  
    187.      ' UNLOAD the GDI+ Engine
    188.        CALL gdihEnd(hGDIplus&)
    189.  
    190.     END IF
    191.  
    192. END FUNCTION
    Last edited by ZAP; Feb 22nd, 2003 at 03:01 AM.
    Patrice Terrier

    pterrier@zapsolution.com
    http://www.zapsolution.com
    Toolkit: WinLIFT (Skin Engine), GDI+ helper (Graphic package), dvBTree (Index manager)
    Multimedia: ZAP Image Solution, ZAP Media Browser, ZAP Slide Show, ZAP Audio Player, ZAP Picture Browser.
    ArtWork on demand.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width