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
' ========================================================================================