Use with GFA Basic 32
Using the Windows API with gb32 example 4
We will begin this example page with a brief discussion
on the difference with some common Gfa Basic native
variables and the difference of these from other languages.
By now you may have noticed that Gfa basic may use postfix
symbols to some often used variables. The postfixes are
a type of namenclature, or naming convention of these
variables in gfa basic code listings. The postfixes assist
the programmer in longer listings by identifying exactly
what type of variable is being used. The main postfixes are:
Variable postfix storage size example
Boolean ? 1 Bit a?
Byte | 1 Byte a|
Word (Short) & 2 Bytes a&
Integer (unsigned) % 4 Bytes a%
Float ! 4 bytes a!
String $ byte length a$
of the char
string used
Gfa Basic 32 also supports some special variables for convenience
that are important in that they are often used in programming:
These are not all of them but here is an explanation of 3 of them:
_X the x-axis size of the current window in pixels.
_Y the y-axis size of the current window in pixels.
_maxInt the maximum size of an unsigned Integer variable.
The tilde character ~ stands for the same thing that it does in
the C language, a void return after calling a C type function.
This then is to discard or ignore a return value from a C type
of a function. This type of C function is used in calls to the
Windows operating system built in API (application programmer
interface).
Gfa Basic has over 1000 built in api functions that do no need
a statement to declare the function prototype as in Visual
Basic. The Dll and function prototype will need to be declared
for the others.
This is shown in the first example, DrawFileIcon. In this example
"Procedure init_WinDll_Libraries" contains the function prototypes
in a way simular to Visual Basic. The next procedure, init_WinSysTypes
uses the Gfa Basic equivilent of a C language data structure or
"Struct" and here it is the windows o/s stucture SHFILEINFO. Finally,
"Procedure init_vb_type_constants" declares and assigns some values
to 'constant' type system variables simular to Visual Basic. Constants
are variables whose values do not change in the running of the program.
Also note in the DrawFileIcon the windows event handler subrutine,
'frm1_Paint'. Without this if we move part of the window off of the
screen or another window is moved on top of frm1, the graphic will be
lost, as windows still needs to be reminded to repaint the form.
Here is the code for some Windows API examples:
Click here to begin downloading drawiconfile.zip
This is pasteable to the GFA Basic 32 editor:
listing 1: DrawFileIcon.
'Display file icon
'for free use with gfa basic
'Allan Shura neotreksoftware.com
'draws a copy of the icon associated with any file on to a form.
@init_vb_type_constants
@init_WinDll_Libraries
@init_WinSysTypes
Form frm1 'create a form 1 in code as in formwindow
frm1.Show
frm1.BackColor = RGB(0, 160, 160)
'use the built in Gfa Basic Windows api with a void ~
'return so our new form gets display priority on top
'of the other windows
~SetForegroundWindow(frm1.hWnd)
AutoRedraw = True
Global hImage As Long, udtFI As SHFILEINFO
@DrawIconFromFile
Do : Sleep : Until Me Is Nothing //listen for windows messages
Procedure init_WinDll_Libraries
'functions accessed from windows dynamic link libraries
Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hdcDst As Long, ByVal x As Long, ByVal y As Long, ByVal fStyle As Long) As Long
Declare Function ImageList_DrawEx Lib "comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hdcDst As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal rgbBk As Long, ByVal rgbFg As Long, ByVal fStyle As Long) As Long
Return
Procedure init_WinSysTypes
'windows system c types
Type SHFILEINFO
hIcon As Long 'icon
iIcon As Long 'icon index
dwAttributes As Long 'flags
szDisplayName As String * MAX_PATH ' path
szTypeName As String * 80
End Type
Return
Procedure init_vb_type_constants
'visual basic or windows constants used
Global Const MAX_PATH = 260
Global Const SHGFI_DISPLAYNAME = &H200
Global Const SHGFI_EXETYPE = &H2000
Global Const SHGFI_LARGEICON = &H0
Global Const SHGFI_SHELLICONSIZE = &H4
Global Const SHGFI_SMALLICON = &H1
Global Const SHGFI_SYSICONINDEX = &H4000
Global Const SHGFI_TYPENAME = &H400
Global Const ILD_BLEND50 = &H4
Global Const ILD_BLEND25 = &H2
Global Const ILD_TRANSPARENT = &H1
Global Const CLR_NONE = &HFFFFFFFF
Global Const CLR_DEFAULT = &HFF000000
Return
Procedure DrawIconFromFile
'get the shell icon associated for the file type
hImage = SHGetFileInfo(CurDir() + "\test.bmp", 0, udtFI, Len(udtFI), SHGFI_SYSICONINDEX Or SHGFI_LARGEICON)
'normal
ImageList_Draw(hImage, udtFI.iIcon, frm1.hDC, 0, 0, ILD_TRANSPARENT)
'blend 50
ImageList_DrawEx(hImage, udtFI.iIcon, frm1.hDC, 32, 0, 32, 32, CLR_NONE, CLR_DEFAULT, ILD_BLEND50)
Return
Sub frm1_Paint
'Auto redraw doesn't do graphic draws, redraw the form
'and when paint event message is sent to the application
'from Windows
@DrawIconFromFile
'remind windows not to switch threads
'before the job is done!
DoEvents
'without this the Icons will be lost
'by moving another window over frm1!
'and the event delayed or missed in
'the windows message queue!
EndSub
We will now move on to the implementation of a couple
of common API utility functions:
Click here to begin downloading gb32_example_4_api.zip
listing 2: Rounded Forms API.
'use with frm1 and add a frm2 in the form editor
'or download the code
Global x1 As Long = 10
Global y1 As Long = 10
Global y3 As Long = 10
Global x3 As Long = 10
LoadForm frm1
frm1.BackColor = RGB(0, 0, 255) 'blue
LoadForm frm2
frm2.BackColor = RGB(0, 255, 0) 'green
'We do not need to declare these functions as in vb since they are
'already built into GB32 with over 1000 other api
'the ~ (tilde) character means a void return upon calling
'the api function as in C
'note that 2 addtional API CreateRoundRectRgn(),CreateEllipticRgn()
'are used as parameters within SetWindowRgn()
~SetWindowRgn(frm1.hWnd, CreateRoundRectRgn(x1, y1, (frm1.Width / 15) - 5, (frm1.Height / 15) - 5, x3, y3), True)
~SetWindowRgn(frm2.hWnd, CreateEllipticRgn(x1 + 15, y1, (frm2.Width / 15) - 20, (frm2.Height / 15) - 15), True)
MsgBox "Close program with Round Rectangle and Elliptical forms."
End 'terminates the program in the IDE
listing 3: GetFreeDiskSpace.
'GB32 API GetDiskFreeSpace
Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
Global b%
Global Sector As Long, Bytes As Long, FreeC As Long, TotalC As Long, Total As Int64, Freeb As Int64
GetDiskFreeSpace("C:\", Sector, Bytes, FreeC, TotalC)
LoadForm frm1
~SetForegroundWindow(frm1.hWnd)
Me.AutoRedraw = True
Me. Print " Path: C:\"
Me. Print " Sectors per Cluster:" + Str$(Sector)
Me. Print " Bytes per sector:" + Str$(Bytes)
Me. Print " Number Of Free Clusters:" + Str$(FreeC)
Me. Print " Total Number Of Clusters:" + Str$(TotalC)
Total = TotalC * Sector * Bytes //long to Int64
Me. Print " Total number of bytes in path:" + Str$(Total)
Freeb = FreeC * Sector * Bytes
Me. Print " Free bytes:" + Str$(Freeb)
Do
Sleep
Until Me Is Nothing
listing 4: IsNetworkAlive.
Const NETWORK_ALIVE_AOL = &H4
Const NETWORK_ALIVE_LAN = &H1
Const NETWORK_ALIVE_WAN = &H2
Declare Function IsNetworkAlive Lib "SENSAPI.DLL" (ByRef lpdwFlags As Long) As Long
Dim Ret As Long
If IsNetworkAlive(Ret) = 0 Then
MsgBox "The local system is not connected to a network!"
Else
MsgBox "The local system is connected to a " + Iif(Ret = NETWORK_ALIVE_AOL, "AOL", Iif(Ret = NETWORK_ALIVE_LAN, "LAN", "WAN")) + " network!"
End If