Author Topic: GDI+: Graphics Examples  (Read 14161 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: DrawPolygon Example
« Reply #15 on: November 04, 2011, 03:42:45 AM »
 
The following example draws a polygon, defined by an array of points.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_DrawPolygon.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example draws a polygon, defined by an array of points.
' ========================================================================================
SUB Example_DrawPolygon (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Pen object.
'   Pen blackPen(Color(255, 0, 0, 0), 3);
   LOCAL blackPen AS IGdipPen
   blackPen = pGdip.Pen(pGdip.Color(255, 0, 0, 0), 3)

'   // Create an array of PointF objects that define the polygon.
'   PointF point1(100.0f, 100.0f);
'   PointF point2(200.0f, 130.0f);
'   PointF point3(150.0f, 200.0f);
'   PointF point4(50.0f, 200.0f);
'   PointF point5(0.0f, 130.0f);
'   PointF points[5] = {point1, point2, point3, point4, point5};
'   PointF* pPoints = points;
   LOCAL point1, point2, point3, point4, point5 AS PointF
   point1 = pGdip.PointF(100, 100)
   point2 = pGdip.PointF(200, 130)
   point3 = pGdip.PointF(150, 200)
   point4 = pGdip.PointF(50, 200)
   point5 = pGdip.PointF(0, 130)
   DIM pts(4) AS PointF
   pts(0) = point1
   pts(1) = point2
   pts(2) = point3
   pts(3) = point4
   pts(4) = point5

'   // Draw the polygon.
'   graphics.DrawPolygon(&blackPen, pPoints, 5);
   graphics.DrawPolygon(blackPen, pts(0), 5)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Draw polygon", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_DrawPolygon(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: DrawRectangle Example
« Reply #16 on: November 04, 2011, 03:43:32 AM »
 
The following example draws a rectangle.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_DrawRectangle.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example draws a rectangle.
' ========================================================================================
SUB Example_DrawRectangle (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Pen object.
'   Pen blackPen(Color(255, 0, 0, 0), 3);
   LOCAL blackPen AS IGdipPen
   blackPen = pGdip.Pen(pGdip.Color(255, 0, 0, 0), 3)

'   // Define the rectangle.
'   REAL x = 0.0f;
'   REAL y = 0.0f;
'   REAL width = 200.0f;
'   REAL height = 200.0f;
   LOCAL x, y, nWidth, nHeight AS SINGLE
   x = 0
   y = 0
   nWidth = 200
   nHeight = 200

'   // Draw the rectangle.
'   graphics.DrawRectangle(&blackPen, x, y, width, height);
   graphics.DrawRectangle(blackPen, x, y, nWidth, nHeight)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Draw rectangle", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_DrawRectangle(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: DrawRectangles Example
« Reply #17 on: November 04, 2011, 03:44:33 AM »
 
The following example draws a group of rectangles.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_DrawRectangles.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example draws a group of rectangles.
' ========================================================================================
SUB Example_DrawRectangles (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Pen object.
'   Pen blackPen(Color(255, 0, 0, 0), 3);
   LOCAL blackPen AS IGdipPen
   blackPen = pGdip.Pen(pGdip.Color(255, 0, 0, 0), 3)

'   // Create an array of RectF objects.
'   RectF rect1(0.0f, 0.0f, 100.0f, 200.0f);
'   RectF rect2(100.0f, 200.0f, 250.0f, 50.0f);
'   RectF rect3(300.0f, 0.0f, 50.0f, 100.0f);
'   RectF rects[] = {rect1, rect2, rect3};
'   RectF* pRects = rects;
   LOCAL rect1, rect2, rect3 AS RectF
   rect1 = pGdip.RectF(0, 0, 100, 200)
   rect2 = pGdip.RectF(100, 200, 250, 50)
   rect3 = pGdip.RectF(300, 0, 50, 100)
   DIM rects(2) AS RectF
   rects(0) = rect1
   rects(1) = rect2
   rects(2) = rect3

'   // Draw the rectangles.
'   graphics.DrawRectangles(&blackPen, pRects, 3);
   graphics.DrawRectangles(blackPen, rects(0), 3)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Draw rectangle", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 252
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_DrawRectangles(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: ExcludeClip Example
« Reply #18 on: November 04, 2011, 03:45:37 AM »
 
The following example uses a rectangle to update a clipping region and then draws a rectangle that demonstrates the updated clipping region.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_ExcludeClip.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example uses a rectangle to update a clipping region and then draws a
' rectangle that demonstrates the updated clipping region.
' ========================================================================================
SUB Example_ExcludeClip (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a RectF object, and set the clipping region to its exclusion.
'   RectF excludeRect(125.0f, 50.0f, 150.0f, 150.0f);
'   graphics.ExcludeClip(excludeRect);
   LOCAL excludeRect AS RectF
   excludeRect = pGdip.RectF(125, 50, 150, 150)
   graphics.ExcludeClipRect(excludeRect)

'   // Fill a rectangle to demonstrate the clipping region.
'   graphics.FillRectangle(&SolidBrush((255, 0, 0, 255)), 0, 0, 400, 250);
   LOCAL solidBrush AS IGdipSolidBrush
   graphics.FillRectangle(pGdip.SolidBrushARGB(255, 0, 0, 255), 0, 0, 400, 250)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Exclude clip", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_ExcludeClip(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: FillClosedCurve Example
« Reply #19 on: November 04, 2011, 03:46:31 AM »
 
The following example fills a closed cardinal spline.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_FillClosedCurve.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example fills a closed cardinal spline.
' ========================================================================================
SUB Example_FillClosedCurve (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a SolidBrush object.
'   SolidBrush blackBrush(Color(255, 0, 0, 0));
   LOCAL blackBrush AS IGdipSolidBrush
   blackBrush = pGdip.SolidBrush(pGdip.Color(255, 0, 0, 0))

'   // Create an array of PointF objects.
'   PointF point1(100.0f, 100.0f);
'   PointF point2(200.0f, 50.0f);
'   PointF point3(250.0f, 200.0f);
'   PointF point4(50.0f, 150.0f);
'   PointF points[4] = {point1, point2, point3, point4};
   LOCAL point1, point2, point3, point4 AS PointF
   point1 = pGdip.PointF(100, 100)
   point2 = pGdip.PointF(200, 50)
   point3 = pGdip.PointF(250, 200)
   point4 = pGdip.PointF(50, 150)
   DIM pts(3) AS PointF
   pts(0) = point1
   pts(1) = point2
   pts(2) = point3
   pts(3) = point4

'   //Fill the curve.
'   graphics.FillClosedCurve(&blackBrush, points, 4);
   graphics.FillClosedCurve(blackBrush, pts(0), 4)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "FillClosedCurve", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_FillClosedCurve(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: FillEllipse Example
« Reply #20 on: November 04, 2011, 03:47:24 AM »
 
The following example fills an ellipse that is defined by coordinates and dimensions.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_FillEllipse.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example fills an ellipse that is defined by coordinates and dimensions.
' ========================================================================================
SUB Example_FillEllipse (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a SolidBrush object.
'   SolidBrush blackBrush(Color(255, 0, 0, 0));
   LOCAL blackBrush AS IGdipSolidBrush
   blackBrush = pGdip.SolidBrush(pGdip.Color(255, 0, 0, 0))

'   // Initialize the variables that define the ellipse.
'   REAL x = 0.0f;
'   REAL y = 0.0f;
'   REAL width = 200.1f;
'   REAL height = 100.4f;
   LOCAL x, y, nWidth, nHeight AS SINGLE
   nWidth = 200.1!
   nHeight = 100.4!

'   // Fill the ellipse.
'   graphics.FillEllipse(&blackBrush, x, y, width, height);
   graphics.FillEllipse(blackBrush, x, y, nWidth, nHeight)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "FillClosedCurve", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_FillEllipse(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
GDI+: FillPath Example
« Reply #21 on: November 04, 2011, 03:48:17 AM »
 
The following example fills a path.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_FillPath.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example fills a path.
' ========================================================================================
SUB Example_FillPath (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a GraphicsPath and add an ellipse.
'   GraphicsPath ellipsePath;
'   ellipsePath.AddEllipse(Rect(0, 0, 200, 100));
   LOCAL ellipsePath AS IGdipGraphicsPath
   ellipsePath = pGdip.GraphicsPath
   ellipsePath.AddEllipse(0, 0, 200, 100)

'   // Create a SolidBrush object.
'   SolidBrush blackBrush(Color(255, 0, 0, 0));
   LOCAL blackBrush AS IGdipSolidBrush
   blackBrush = pGdip.SolidBrush(pGdip.Color(255, 0, 0, 0))

'   // Draw ellipsePath.
'   graphics.FillPath(&blackBrush, &ellipsePath);
   graphics.FillPath(blackBrush, ellipsePath)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "FillClosedCurve", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_FillPath(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================