• Welcome to Jose's Read Only Forum 2023.
 

GDI+: Brushes Examples

Started by José Roca, November 04, 2011, 01:21:13 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
The IGdipBrush interface defines a Brush object. A Brush object is used to paint the interior of graphics shapes, such as rectangles, ellipses, pies, polygons, and paths.


José Roca

 
The following example creates a SolidBrush object, clones it, and then uses the clone to fill a rectangle.


' ########################################################################################
' Microsoft Windows
' File: CGDIP_CloneBrush.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 creates a SolidBrush object, clones it, and then uses the clone
' to fill a rectangle.
' ========================================================================================
SUB Example_CloneBrush (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

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

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

'   // Create a copy of solidBrush.
'   Brush *cloneBrush = solidBrush.Clone();
   LOCAL cloneBrush AS IGdipSolidBrush
   cloneBrush = solidBrush.Clone

'   // Use cloneBrush to fill a rectangle.
'   graphics.FillRectangle(cloneBrush, Rect(0, 0, 100, 100));
   graphics.FillRectangle(cloneBrush, 0, 0, 100, 100)

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, "Clone Brush", 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 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_CloneBrush(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
' ========================================================================================


Flat API version


' ########################################################################################
' Microsoft Windows
' File: GDIP_CloneBrush.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' 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 "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a SolidBrush object, clones it, and then uses the clone
' to fill a rectangle.
' ========================================================================================
SUB Example_CloneBrush (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a SolidBrush object.
'   SolidBrush solidBrush(Color(255, 255, 0, 0));
   LOCAL solidBrush AS DWORD
   LOCAL pixelColor AS DWORD
   pixelColor = GDIP_ARGB(255, 255, 0, 0)
   GdipCreateSolidFill(pixelColor, solidBrush)

'   // Create a copy of solidBrush.
'   Brush *cloneBrush = solidBrush.Clone();
   LOCAL cloneBrush AS DWORD
   GdipCloneBrush(solidBrush, cloneBrush)

'   // Use cloneBrush to fill a rectangle.
'   graphics.FillRectangle(cloneBrush, Rect(0, 0, 100, 100));
   GdipFillRectangleI(graphics, cloneBrush, 0, 0, 100, 100)

   ' // Cleanup
   GdipDeleteBrush(cloneBrush)
   GdipDeleteBrush(solidBrush)
   GdipDeleteGraphics(graphics)

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, "Clone Brush", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(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_CloneBrush(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
' ========================================================================================


José Roca

 
The following example creates a SolidBrush object, checks the type of the object, and then, if the type is BrushTypeSolidColor, uses the brush to fill a rectangle.


' ########################################################################################
' Microsoft Windows
' File: CGDIP_GetBrushType.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 creates a SolidBrush object, checks the type of the object, and
' then, if the type is BrushTypeSolidColor, uses the brush to fill a rectangle.
' ========================================================================================
SUB Example_GetType (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

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

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

'   // Get the type of solidBrush.
'   BrushType type = solidBrush.GetType();
   LOCAL brushType AS LONG
   brushType = solidBrush.GetType

'   // If the type of solidBrush is BrushTypeSolidColor, use it to fill a rectangle.
'   if (type == BrushTypeSolidColor)
'       graphics.FillRectangle(&solidBrush, Rect(0, 0, 100, 100));
   IF brushType = %BrushTypeSolidColor THEN
      graphics.FillRectangle(solidBrush, 0, 0, 100, 100)
   END IF

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, "Get brush type", 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 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_GetType(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
' ========================================================================================


Flat API version


' ########################################################################################
' Microsoft Windows
' File: GDIP_GetBrushType.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' 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 "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a SolidBrush object, checks the type of the object, and
' then, if the type is BrushTypeSolidColor, uses the brush to fill a rectangle.
' ========================================================================================
SUB Example_GetType (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a SolidBrush object.
'   SolidBrush solidBrush(Color(255, 0, 0, 255));
   LOCAL pixelColor AS DWORD
   pixelColor = GDIP_ARGB(255, 0, 0, 255)
   LOCAL solidBrush AS DWORD
   GdipCreateSolidFill(pixelColor, solidBrush)

'   // Get the type of solidBrush.
'   BrushType type = solidBrush.GetType();
   LOCAL brushType AS LONG
   GdipGetBrushType(solidBrush, brushType)

'   // If the type of solidBrush is BrushTypeSolidColor, use it to fill a rectangle.
'   if (type == BrushTypeSolidColor)
'       graphics.FillRectangle(&solidBrush, Rect(0, 0, 100, 100));
   IF brushType = %BrushTypeSolidColor THEN
      GdipFillRectangle(graphics, solidBrush, 0, 0, 100, 100)
   END IF

   ' // Cleanup
   GdipDeleteBrush(solidBrush)
   GdipDeleteGraphics(graphics)

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, "Get brush type", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(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_GetType(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
' ========================================================================================


José Roca

 
The following example creates a solid brush and uses it to fill a rectangle. The code gets the color of the solid brush and stores it. Then, the code creates a second solid brush using the stored color and paints a second rectangle with the second solid brush.


' ########################################################################################
' Microsoft Windows
' File: CGDIP_SolidBrushGetColor.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 creates a solid brush and uses it to fill a rectangle. The code
' gets the color of the solid brush and stores it. Then, the code creates a second solid
' brush using the stored color and paints a second rectangle with the second solid brush.
' ========================================================================================
SUB Example_GetColor (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

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

'   // Create a solid brush, and use it to fill a rectangle.
'   SolidBrush solidBrush(Color(255, 0, 0, 255));  // blue
'   graphics.FillRectangle(&solidBrush, 10, 10, 200, 100);
   LOCAL solidBrush AS IGdipSolidBrush
   solidBrush = pGdip.SolidBrush(pGdip.Color(255, 0, 0, 255))
   graphics.FillRectangle(solidBrush, 10, 10, 200, 100)

'   // Get the color of the solid brush.
'   Color color;
'   solidBrush.GetColor(&color);
   LOCAL colour AS DWORD
   solidBrush.GetColor(colour)

'   // Create a second solid brush with that same color.
'   SolidBrush solidBrush2(color);
   LOCAL solidBrush2 AS IGdipSolidBrush
   solidBrush2 = pGdip.SolidBrush(colour)

'   // Paint a second rectangle with the second solid brush.
'   graphics.FillRectangle(&solidBrush2, 220, 10, 200, 100);
   graphics.FillRectangle(solidBrush2, 220, 10, 200, 100)

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, "Get Brush Color", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 430, 250
   ' // Center the window
   pWindow.CenterWindow

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

   ' // Add a 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_GetColor(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
' ========================================================================================


Flat API version


' ########################################################################################
' Microsoft Windows
' File: GDIP_SolidBrushGetColor.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' 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 "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a solid brush and uses it to fill a rectangle. The code
' gets the color of the solid brush and stores it. Then, the code creates a second solid
' brush using the stored color and paints a second rectangle with the second solid brush.
' ========================================================================================
SUB Example_GetColor (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a solid brush, and use it to fill a rectangle.
'   SolidBrush solidBrush(Color(255, 0, 0, 255));  // blue
'   graphics.FillRectangle(&solidBrush, 10, 10, 200, 100);
   LOCAL solidBrush AS DWORD
   GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), solidBrush)
   GdipFillRectangle(graphics, solidBrush, 10, 10, 200, 100)

'   // Get the color of the solid brush.
'   Color color;
'   solidBrush.GetColor(&color);
   LOCAL colour AS DWORD
   GdipGetSolidFillColor(solidBrush, colour)

'   // Create a second solid brush with that same color.
'   SolidBrush solidBrush2(color);
   LOCAL solidBrush2 AS DWORD
   GdipCreateSolidFill(colour, solidBrush2)

'   // Paint a second rectangle with the second solid brush.
'   graphics.FillRectangle(&solidBrush2, 220, 10, 200, 100);
   GdipFillRectangle(graphics, solidBrush2, 220, 10, 200, 100)

   ' // Cleanup
   GdipDeleteBrush(solidBrush2)
   GdipDeleteBrush(solidBrush)
   GdipDeleteGraphics(graphics)

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, "Get Brush Color", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 430, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(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_GetColor(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
' ========================================================================================


José Roca

 
The following example creates a solid brush and uses it to fill a rectangle. The code uses SetColor to change the color of the solid brush and then paints a second rectangle the new color.


' ########################################################################################
' Microsoft Windows
' File: CGDIP_SolidBrushSetColor.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 creates a solid brush and uses it to fill a rectangle. The code
' uses SetColor to change the color of the solid brush and then paints a second rectangle
' the new color.
' ========================================================================================
SUB Example_SetColor (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

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

'   // Create a solid brush, and use it to fill a rectangle.
'   SolidBrush solidBrush(Color(255, 0, 0, 255));  // blue
'   graphics.FillRectangle(&solidBrush, 10, 10, 200, 100);
   LOCAL solidBrush AS IGdipSolidBrush
   solidBrush = pGdip.SolidBrush(pGdip.Color(255, 0, 0, 255))
   graphics.FillRectangle(solidBrush, 10, 10, 200, 100)

'   // Change the color of the brush, and fill another rectangle.
'   solidBrush.SetColor(Color(255, 255, 0, 0));    // red
'   graphics.FillRectangle(&solidBrush, 220, 10, 200, 100);
   solidBrush.SetColor(pGdip.Color(255, 255, 0, 0))   ' // red
   graphics.FillRectangle(solidBrush, 220, 10, 200, 100)

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, "Set Brush Color", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 430, 250
   ' // Center the window
   pWindow.CenterWindow

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

   ' // Add a 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_SetColor(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
' ========================================================================================


Flat API version


' ########################################################################################
' Microsoft Windows
' File: CGDIP_SolidBrushSetColor.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' 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 "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a solid brush and uses it to fill a rectangle. The code
' uses GdipSetSolidFillColor to change the color of the solid brush and then paints a
' second rectangle the new color.
' ========================================================================================
SUB Example_SetColor (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a solid brush, and use it to fill a rectangle.
'   SolidBrush solidBrush(Color(255, 0, 0, 255));  // blue
'   graphics.FillRectangle(&solidBrush, 10, 10, 200, 100);
   LOCAL solidBrush AS DWORD
   GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), solidBrush)
   GdipFillRectangle(graphics, solidBrush, 10, 10, 200, 100)

'   // Change the color of the brush, and fill another rectangle.
'   solidBrush.SetColor(Color(255, 255, 0, 0));    // red
'   graphics.FillRectangle(&solidBrush, 220, 10, 200, 100);
   GdipSetSolidFillColor(solidBrush, GDIP_ARGB(255, 255, 0, 0))
   GdipFillRectangle(graphics, solidBrush, 220, 10, 200, 100)

   ' // Cleanup
   GdipDeleteBrush(solidBrush)
   GdipDeleteGraphics(graphics)

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, "Set Brush Color", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 430, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(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_SetColor(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
' ========================================================================================