GDI+ Skeletons
Most of the posted GDI+ examples use a fixed skeleton for the SDK style programming, DDT and DDT with the use of the graphic control (introduced in PBWIn 8.0).
The SDK and DDT skeletons call the procedure containing the GDI+ code in the %WM_PAINT message:
CASE %WM_PAINT
hDC = BeginPaint(hWnd, ps)
GDIP_DrawLine hDC ' --> The name of the procedure changes with each example
EndPaint(hWnd, ps)Since the PB/WIN DDT graphic control features persistence, the procedure isn't called in %WM_PAINT, but after the control is created:
' Add a graphic control
CONTROL ADD GRAPHIC, hDlg, %IDC_GRAPHIC,"", 0, 0, 420, 250
' Select the drawing target
GRAPHIC ATTACH hDlg, %IDC_GRAPHIC
' Set the foreground and background color
GRAPHIC COLOR %BLACK, %WHITE
' Clear the entire selected graphic window
GRAPHIC CLEAR
' Retrieve the handle of the device context
GRAPHIC GET DC TO hdc
' Draw the graphics
GDIP_DrawLine hdcHere is the same example using the three different skeletons:
SDK Skeleton:
' ========================================================================================
' GdipDrawLine
' ========================================================================================
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
' ========================================================================================
' The following example draws a line.
' ========================================================================================
SUB GDIP_DrawLine (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPen AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a Pen
hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitPixel, pPen)
' // Draw the line
GdipDrawLineI pGraphics, pPen, 0, 0, 200, 100
' // Cleanup
IF pPen THEN GdipDeletePen(pPen)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
LOCAL hr AS LONG
LOCAL hWndMain AS DWORD
LOCAL hCtl AS DWORD
LOCAL hFont AS DWORD
LOCAL wcex AS WndClassEx
LOCAL szClassName AS ASCIIZ * 80
LOCAL rc AS RECT
LOCAL szCaption AS ASCIIZ * 255
LOCAL nLeft AS LONG
LOCAL nTop AS LONG
LOCAL nWidth AS LONG
LOCAL nHeight AS LONG
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput
' Initialize GDI+
StartupInput.GdiplusVersion = 1
hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hr THEN
MSGBOX "Error initializing GDI+"
EXIT FUNCTION
END IF
hFont = GetStockObject(%ANSI_VAR_FONT)
' Register the window class
szClassName = "MyClassName"
wcex.cbSize = SIZEOF(wcex)
wcex.style = %CS_HREDRAW OR %CS_VREDRAW
wcex.lpfnWndProc = CODEPTR(WndProc)
wcex.cbClsExtra = 0
wcex.cbWndExtra = 0
wcex.hInstance = hInstance
wcex.hCursor = LoadCursor (%NULL, BYVAL %IDC_ARROW)
wcex.hbrBackground = GetStockObject(%WHITE_BRUSH)
wcex.lpszMenuName = %NULL
wcex.lpszClassName = VARPTR(szClassName)
wcex.hIcon = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Sample, if resource icon: LoadIcon(hInst, "APPICON")
wcex.hIconSm = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Remember to set small icon too..
RegisterClassEx wcex
' Window caption
szCaption = "GdipDrawLine"
' Retrieve the nSize of the working area
SystemParametersInfo %SPI_GETWORKAREA, 0, BYVAL VARPTR(rc), 0
' Calculate the position and nSize of the window
nWidth = (((rc.nRight - rc.nLeft)) + 2) * 0.55 ' 55% of the client screen width
nHeight = (((rc.nBottom - rc.nTop)) + 2) * 0.50 ' 50% of the client screen height
nLeft = ((rc.nRight - rc.nLeft) \ 2) - nWidth \ 2
nTop = ((rc.nBottom - rc.nTop) \ 2) - (nHeight \ 2)
' Create a window using the registered class
hWndMain = CreateWindowEx(%WS_EX_CONTROLPARENT, _ ' extended style
szClassName, _ ' window class name
szCaption, _ ' window caption
%WS_OVERLAPPEDWINDOW OR _
%WS_CLIPCHILDREN, _ ' window style
nLeft, _ ' initial x position
nTop, _ ' initial y position
nWidth, _ ' initial x nSize
nHeight, _ ' initial y nSize
%NULL, _ ' parent window handle
0, _ ' window menu handle
hInstance, _ ' program instance handle
BYVAL %NULL) ' creation parameters
' Show the window
ShowWindow hWndMain, nCmdShow
UpdateWindow hWndMain
' Message handler loop
LOCAL Msg AS tagMsg
WHILE GetMessage(Msg, %NULL, 0, 0)
IF ISFALSE IsDialogMessage(hWndMain, Msg) THEN
TranslateMessage Msg
DispatchMessage Msg
END IF
WEND
' Shutdown GDI+
GdiplusShutdown token
FUNCTION = msg.wParam
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main Window procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
LOCAL hDC AS DWORD
LOCAL ps AS PAINTSTRUCT
SELECT CASE wMsg
CASE %WM_COMMAND
SELECT CASE LOWRD(wParam)
CASE %IDCANCEL
IF HIWRD(wParam) = %BN_CLICKED THEN
SendMessage hWnd, %WM_CLOSE, 0, 0
FUNCTION = 0
EXIT FUNCTION
END IF
END SELECT
CASE %WM_SYSCOMMAND
' Capture this message and send a WM_CLOSE message
IF (wParam AND &HFFF0) = %SC_CLOSE THEN
SendMessage hWnd, %WM_CLOSE, 0, 0
EXIT FUNCTION
END IF
CASE %WM_PAINT
hDC = BeginPaint(hWnd, ps)
GDIP_DrawLine hDC
EndPaint(hWnd, ps)
CASE %WM_DESTROY
PostQuitMessage 0
EXIT FUNCTION
END SELECT
FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
END FUNCTION
' ========================================================================================
DDT Skeleton:
' ========================================================================================
' GdipDrawLine
' ========================================================================================
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
' ========================================================================================
' The following example draws a line.
' ========================================================================================
SUB GDIP_DrawLine (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPen AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a Pen
hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitPixel, pPen)
' // Draw the line
GdipDrawLineI pGraphics, pPen, 0, 0, 200, 100
' // Cleanup
IF pPen THEN GdipDeletePen(pPen)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
LOCAL hr AS LONG
LOCAL hDlg AS DWORD
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput
' Initialize GDI+
StartupInput.GdiplusVersion = 1
hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hr THEN
MSGBOX "Error initializing GDI+"
EXIT FUNCTION
END IF
DIALOG NEW 0, "GdipDrawLine", , , 280, 170, %WS_OVERLAPPED OR %WS_THICKFRAME OR %WS_SYSMENU OR _
%WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_VISIBLE OR %DS_CENTER TO hDlg
DIALOG SET COLOR hDlg, %BLACK, %WHITE
DIALOG SHOW MODAL hDlg, CALL DlgProc
' Shutdown GDI+
GdiplusShutdown token
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main Dialog procedure
' ========================================================================================
CALLBACK FUNCTION DlgProc() AS LONG
LOCAL hDC AS DWORD
LOCAL ps AS PAINTSTRUCT
SELECT CASE CBMSG
CASE %WM_COMMAND
SELECT CASE CBCTL
CASE %IDCANCEL
IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL, 0
END SELECT
CASE %WM_PAINT
hDC = BeginPaint(CBHNDL, ps)
GDIP_DrawLine hDC
EndPaint(CBHNDL, ps)
END SELECT
END FUNCTION
' ========================================================================================
DDT Skeleton using the graphic control:
' ========================================================================================
' GdipDrawLine
' ========================================================================================
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
%IDC_GRAPHIC = 101
' ========================================================================================
' The following example draws a line.
' ========================================================================================
SUB GDIP_DrawLine (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPen AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a Pen
hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitPixel, pPen)
' // Draw the line
GdipDrawLineI pGraphics, pPen, 0, 0, 200, 100
' // Cleanup
IF pPen THEN GdipDeletePen(pPen)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
LOCAL hr AS LONG
LOCAL hDlg AS DWORD
LOCAL hdc AS DWORD
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput
' Initialize GDI+
StartupInput.GdiplusVersion = 1
hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hr THEN
MSGBOX "Error initializing GDI+"
EXIT FUNCTION
END IF
' Create a new dialog
DIALOG NEW PIXELS, 0, "GdipDrawLine", , , 420, 250, %WS_SYSMENU TO hDlg
' Add a graphic control
CONTROL ADD GRAPHIC, hDlg, %IDC_GRAPHIC,"", 0, 0, 420, 250
' Select the drawing target
GRAPHIC ATTACH hDlg, %IDC_GRAPHIC
' Set the foreground and background color
GRAPHIC COLOR %BLACK, %WHITE
' Clear the entire selected graphic window
GRAPHIC CLEAR
' Retrieve the handle of the device context
GRAPHIC GET DC TO hdc
' Draw the graphics
GDIP_DrawLine hdc
DIALOG SHOW MODAL hDlg, CALL DlgProc
' Shutdown GDI+
GdiplusShutdown token
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main Dialog procedure
' ========================================================================================
CALLBACK FUNCTION DlgProc() AS LONG
SELECT CASE CBMSG
CASE %WM_COMMAND
SELECT CASE CBCTL
CASE %IDCANCEL
IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL, 0
END SELECT
END SELECT
END FUNCTION
' ========================================================================================