Date: 02-16-2022
Return to Index
created by gbSnippets
'Hello World is a phrase used often in sample programs, displayed so the program is seen
'to have accomplished something - confirmation that it worked.
'Primary Code:
'Credit: PowerBASIC Inc, distributed sample applications
'This snippet puts together all of the preceding snippets, creating a Window the SDK way!
0. WinMain
1. Define
2. Register
3. Show
4. Message Loop
5. Message Response
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim ALL
%USEMACROS = 1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr" ' source of window icon
Function WinMain (ByVal hInst As Dword, ByVal hPrevInstance As Dword, ByVal lpCmdLine As ASCIIZ PTR, ByVal iCmdShow As Long) As Long
Local Msg AS tagMsg, myWin AS WndClassEx, szAppName As AsciiZ * 80, hWnd AS Dword
' Setup and register a window class for the main window. CODEPTR points to message handler function.
szAppName = "HelloSDK"
myWin.cbSize = SizeOf(myWin)
myWin.Style = %CS_HREDRAW Or %CS_VREDRAW
myWin.lpfnWndProc = CodePTR(WndProc)
myWin.cbClsExtra = 0
myWin.cbWndExtra = 0
myWin.hInstance = hInst
myWin.hIcon = LoadIcon(hInst, "aainfo") 'icon from PBR file
myWin.hCursor = LoadCursor(%NULL, ByVal %IDC_ARROW)
myWin.hbrBackground = %NULL ' No class background, we do it ourselves
myWin.lpszMenuName = %NULL
myWin.lpszClassName = VarPTR(szAppName)
myWin.hIconSm = LoadIcon(hInst, ByVal %IDI_APPLICATION)
RegisterClassEx myWin
' Create a window using the registered class ....parent window handle, window menu handle, program instance handle, creation parameters
hWnd = CreateWindow(szAppName, "SDK", %WS_OVERLAPPEDWINDOW, 500,400,300,150,%Null, %Null, hInst, ByVal %Null)
If hWnd = 0 Then MsgBox "Unable to create window" : Exit Function 'exit on failure to create window
ShowWindow hWnd, iCmdShow : UpdateWindow hWnd 'Display the window on the screen
' Main message loop:
Do While GetMessage(Msg, %NULL, 0, 0) 'PostQuitMessage() exits from loop
TranslateMessage Msg : DispatchMessage Msg 'GetMessage receives message when window has focus
Loop
Function = Msg.wParam
End Function
Sub DrawGradient (ByVal hDC AS Dword)
Local rectFill AS RECT, rectClient AS RECT, fStep AS SINGLE, hBrush AS Dword, lOnBand As Long
GetClientRect WindowFromDC(hDC), rectClient
fStep = rectClient.nbottom / 200
For lOnBand = 0 TO 199
SetRect rectFill, 0, lOnBand * fStep, rectClient.nright + 1, (lOnBand + 1) * fStep
hBrush = CreateSolidBrush(Rgb(0, 0, 255 - lOnBand))
Fillrect hDC, rectFill, hBrush
DeleteObject hBrush
Next
End Sub
Function WndProc (ByVal hWnd AS Dword, ByVal wMsg AS Dword, ByVal wParam AS Dword, ByVal lParam As Long) EXPORT As Long
' WndProc is the message handler for all windows creating using the HelloWin class name.
Local hDC AS Dword, pPaint AS PAINTSTRUCT, tRect AS RECT
Select Case wMsg
Case %WM_CREATE
Case %WM_PAINT
hDC = BeginPaint(hWnd, pPaint)
GetClientRect hWnd, tRect
SetBkMode hDC, %TRANSPARENT
SetTextColor hDC, %WHITE
DrawText hDC, "SDK is the Way!", -1, tRect, %DT_SINGLELINE Or %DT_CENTER Or %DT_VCENTER
EndPaint hWnd, pPaint
Function = 1 : Exit Function
Case %WM_ERASEBKGND
hDC = wParam
DrawGradient hDC ' Pass the DC of the region to repaint
Function = 1 : Exit Function
Case %WM_DESTROY
PostQuitMessage 0
Exit Function
End Select
Function = DefWindowProc(hWnd, wMsg, wParam, lParam) 'if not handled above, pass to Windows default message handler.
End Function
'gbs_00334
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm