WinAPI Tutorial #1
Posted: Fri Aug 02, 2002 6:24 am
Code updated for 5.20+
Restored from previous forum. Originally posted by Art Sentinel.
Hi! It recently dawned on me that PureBasic is capable of performing almost any
programming task you can think up, whether you are a fletchling amateur or a virtual
PB coding demigod. This amazing power is achieved through the skillful use of ASM
coding and the WinAPI. (Neither of which is even remotely as difficult as you would
first believe.)
I am just now teaching myself how to do useful things in Nasm and the API. People
here have been so patient and generous with me, I would like to share a little something
with those attempting to learn how to use the Windows API in their own PureBasic
code. (No this will not make your nose bleed and give you a nasty migraine.)
[lol]
There is a great beginner API tutorial written by a man named Karl Moore. The lesson
was directed for Visual Basic programmers. I know that many people wanting to learn
PureBasic will have little to no experience with Microsoft's interpreted Frankenstein,
so I converted all the coding tasks into PB so that you could much easier understand
what is going on.
.
Tips to Remember:
- Call APIFuntionName in VB is equivalent to "APIFunctionName_(parameters)" in PB
- All API constants are written with a hash sign first as if they were local PB constants.
(I.E. #WooHoo, #APIProgramming, #IsFun)
- If you do not have the Win32.hlp file yet download it here:
ftp://ftpc.borland.com/pub/delphi/techp ... /win32.zip
or here:
http://www.borland.com/devsupport/delphi/downloads/
Then stick it in your PureBasic install folder inside the 'Help' directory. Now, all
you need to do is place you text pointer within an API command while using the PB IDE, and press
F1. You will get a pleasant little surprise.
- Grab a copy of the API Guide and carry it like your new best friend everywhere you go.
(http://www.allapi.net/agnet/apiguide.shtml)
- To understand what data type is needed for a given parameter, read the above two downloadable
resources carefully. They will describe what the API is looking for.
- If you can basically understand what a line of Visual Basic code means translated to PureBasic,
you will be much more prepared to start learning from the vast number (1000's) of VB tutorials freely
available on the net. In VB most programmers have a convention of placing three letters before their
variables to help identify them for the type they are. Such as:
1. lngPizza As Long (VB) = Pizza.l (PB)
2. strSmellySocks As String (VB) = SmellySocks.s (Or SmellySocks$)
- If an API function wants a memory address as one of its arguments, use a PB pointer (@,*,?).
Else, expect your computer to explode.
- That brings up two more very important tips: Save often! And wear protective eye gear!
.
You will find the tutorial in two parts, starting with the following URL:
http://216.26.168.92/api/winapi/
Read it slowly and absorb what they say. It is very helpful. When you reach a programming task, simply
refer to the examples I designed for you below. I sincerely hope this will help someone!
Note: There are a couple final programming tasks you are asked to undertake during the conclusion of
part 2 in the API tutorial. I did not design examples of those, because by that point, it is a good idea
for you to try them out on your own.
Please send thank you emails to Karl Moore. He is the one who wrote the tutorials. All I am guilty
of doing is attempting to help you out by making things more clear in your PureBasic coder eyes.
After reading through the entire tutorial, I think you will agree with me that using the Win API within
PB is much easier than using it within VB. (Thank you Fred!)
.
.
==================
Translated Samples
==================
.
.
.
Have fun with PureBasic!
- Art Sentinel
http://www.artsentinel.net
--------------
Top Ten Reasons Not To Procrastinate:
Coming Soon...
Edited by - Art Sentinel on 02 August 2002 14:48:57
Restored from previous forum. Originally posted by Art Sentinel.
Hi! It recently dawned on me that PureBasic is capable of performing almost any
programming task you can think up, whether you are a fletchling amateur or a virtual
PB coding demigod. This amazing power is achieved through the skillful use of ASM
coding and the WinAPI. (Neither of which is even remotely as difficult as you would
first believe.)
I am just now teaching myself how to do useful things in Nasm and the API. People
here have been so patient and generous with me, I would like to share a little something
with those attempting to learn how to use the Windows API in their own PureBasic
code. (No this will not make your nose bleed and give you a nasty migraine.)

There is a great beginner API tutorial written by a man named Karl Moore. The lesson
was directed for Visual Basic programmers. I know that many people wanting to learn
PureBasic will have little to no experience with Microsoft's interpreted Frankenstein,
so I converted all the coding tasks into PB so that you could much easier understand
what is going on.

.
Tips to Remember:
- Call APIFuntionName in VB is equivalent to "APIFunctionName_(parameters)" in PB
- All API constants are written with a hash sign first as if they were local PB constants.
(I.E. #WooHoo, #APIProgramming, #IsFun)
- If you do not have the Win32.hlp file yet download it here:
ftp://ftpc.borland.com/pub/delphi/techp ... /win32.zip
or here:
http://www.borland.com/devsupport/delphi/downloads/
Then stick it in your PureBasic install folder inside the 'Help' directory. Now, all
you need to do is place you text pointer within an API command while using the PB IDE, and press
F1. You will get a pleasant little surprise.
- Grab a copy of the API Guide and carry it like your new best friend everywhere you go.

(http://www.allapi.net/agnet/apiguide.shtml)
- To understand what data type is needed for a given parameter, read the above two downloadable
resources carefully. They will describe what the API is looking for.
- If you can basically understand what a line of Visual Basic code means translated to PureBasic,
you will be much more prepared to start learning from the vast number (1000's) of VB tutorials freely
available on the net. In VB most programmers have a convention of placing three letters before their
variables to help identify them for the type they are. Such as:
1. lngPizza As Long (VB) = Pizza.l (PB)
2. strSmellySocks As String (VB) = SmellySocks.s (Or SmellySocks$)
- If an API function wants a memory address as one of its arguments, use a PB pointer (@,*,?).
Else, expect your computer to explode.
- That brings up two more very important tips: Save often! And wear protective eye gear!

.
You will find the tutorial in two parts, starting with the following URL:
http://216.26.168.92/api/winapi/
Read it slowly and absorb what they say. It is very helpful. When you reach a programming task, simply
refer to the examples I designed for you below. I sincerely hope this will help someone!
Note: There are a couple final programming tasks you are asked to undertake during the conclusion of
part 2 in the API tutorial. I did not design examples of those, because by that point, it is a good idea
for you to try them out on your own.
Please send thank you emails to Karl Moore. He is the one who wrote the tutorials. All I am guilty
of doing is attempting to help you out by making things more clear in your PureBasic coder eyes.

After reading through the entire tutorial, I think you will agree with me that using the Win API within
PB is much easier than using it within VB. (Thank you Fred!)
.
.
==================
Translated Samples
==================
.
.
Code: Select all
;====================
;GetTickCount API function test
;Art Sentinel July, 30, 2002
;For everyone at the PureBasic Forum
;====================
;ILYLCBD
;Declare variables
HowLong.f = GetTickCount_()
HowLongMill$ = StrF(HowLong , 1)
HowLongSec$ = StrF(HowLong/1000 , 1)
HowLongMin$ = StrF((HowLong/1000)/60 , 1)
HowLongHour$ = StrF(((HowLong/1000)/60)/60 , 1)
;Start Subliminal_Message{
;Hire Art Sentinel for all your web design and graphic art needs!
;End Subliminal_Message
;Display results
MessageRequester("API Test", HowLongMill$ + " milliseconds have passed since you turned your computer on.",#PB_MessageRequester_Ok)
MessageRequester("API Test", HowLongSec$ + " second have passed since you turned your computer on.",#PB_MessageRequester_Ok)
MessageRequester("API Test", HowLongMin$ + " minutes have passed since you turned your computer on.", #PB_MessageRequester_Ok)
MessageRequester("API Test", HowLongHour$ + " hours have passed since you turned your computer on.", #PB_MessageRequester_Ok)
;Finish up
End
Code: Select all
;====================
;MessageBox API Test
;Art Sentinel July, 30, 2002
;For everyone at the PureBasic Forum
;====================
;ILYLCBD
;Declare Variables
Title.s = "MessageBox API Test"
EvilText.s = "Crap.. Windows is evil.."
DoesText.s = "Does this work?"
YippeeTest.s = "Yippee!! This Works! :wink:"
NoPointerPsst$ = "Psst.. Try moving the main window now before you press 'OK'."
NoPointerDoh$ = "Doh! Something went wrong! "
NoPointerTitle$ = "MessageBox API Test"
;Open Window for hWnd to work
If OpenWindow(0, 10, 150, 300, 300, Title$)
;Find Identity of current window
hWnd = WindowID(0)
Else
MessageBox_(hWnd, @EvilText, @Title, #MB_OK | #MB_ICONERROR)
End
EndIf
;Call MessageBox API
If MessageBox_(hWnd, @DoesText, @Title, #MB_YESNO | #MB_ICONQUESTION) = #IDYES
MessageBox_(hWnd, @YippeeText, @Title, #MB_OK | #MB_ICONEXCLAMATION)
Else
;And to demonsytrate how PureBasic automatically uses the pointer to a string's address without the need for '@'...
MessageBox_(NULL, NoPointerDoh$ + NoPointerPsst$ , NoPointerTitle$, #MB_OK | #MB_ICONERROR)
EndIf
;Finish up
End
Code: Select all
;====================
;Menu Bitmap Image Test
;Art Sentinel July, 31, 2002
;For everyone at the PureBasic Forum
;====================
;ILYLCBD
;Declare variables
#MN_Open = 0
#MN_Exit = 2
#Exit_Icon = 2
Titles$ = "Menu Bitmap Image Test"
DefaultFile$ = "C:\"
Pattern$ = "Nothing Much file| *.nmf |Not Really File| *.nrf;*.not"
FileName$ = "Exit_Icon.bmp"
Debug #PB_Compiler_Home
;Load bitmap for the menu API test
If LoadImage(#Exit_Icon, #PB_Compiler_Home + "Examples/Sources/Data/File.bmp")
hBitmapUnchecked = ImageID(#Exit_Icon)
hBitmapChecked = ImageID(#Exit_Icon)
Else
MessageRequester("[sigh]", "Load Image command failed. Please make sure you placed a 13 x 13 pixel bitmap within the same directory as this .pb", #PB_MessageRequester_Ok)
End
EndIf
;Open window for menu
If OpenWindow(0,10, 150, 300, 300, Title$, #PB_Window_SystemMenu)
hWnd = WindowID(0)
Else
MessageRequester("Could not open PureBasic Window", "DOH!", #PB_MessageRequester_Ok)
EndIf
;Create menu
CreateMenu(0, hWnd)
MenuTitle("&File")
MenuItem(#MN_Open, "&Open")
MenuBar()
MenuItem(#MN_Exit, "&Exit")
;Call API function
Menu.l = GetMenu_(hWnd)
SubMenu.l = GetSubMenu_(Menu, 0)
MenuItemID = GetMenuItemID_(SubMenu, 2)
SetMenuItemBitmaps_(Menu, MenuItemID, 0, hBitmapUnchecked, hBitmapChecked)
;Main loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_Menu
Select EventMenu()
Case 0 ; Open
OpenFileRequester("Open What?", DefaultFile$, Pattern$, 0)
Case 2 ; Exit
Quit = 1
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
;Finish up
MessageRequester("Hillbilly Exit", "Ya all com'on back now, ya hear!", #PB_MessageRequester_Ok)
FreeImage(#Exit_Icon)
End
Code: Select all
;====================
;GetWindowsDirectory API Test
;Art Sentinel August, 01, 2002
;For everyone at the PureBasic Forum
;====================
;ILYLCBD
;Declare variables
Size.l = 255
WinDir$ = Space(Size)
Quite.b = 0
;Open window
If OpenWindow(0, 150, 150, 300, 300, "GetWindowsDirectory API Test", #PB_Window_SystemMenu)
WindowID = WindowID(0)
Else
MessageRequester("Could not open window.", ":(", #PB_MessageRequester_Ok)
EndIf
;Add command button
If CreateGadgetList(WindowID)
ButtonGadget(007, 120, 135, 60, 30, "Press me", #PB_Button_Default)
GadgetToolTip(007, "Press this button to learn the location of your Windows directory.")
Else
MessageRequester("Oops!", "My bad...", #PB_MessageRequester_Ok)
End
EndIf
;Main loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case 007
Gosub FindDirectory
MessageRequester("Warning:", "Now that you know where your Windows directory is, you will never be the same again. (Oh how I miss those carefree days of innocence..)", #PB_MessageRequester_Ok)
End
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
;Finish up
End
;Perform the API call and display results
FindDirectory:
GetWindowsDirectory_(@WinDir$, Size - 1)
WinDir$ = RTrim(WinDir$)
MessageRequester("And the answer to the million dollar question is..", WinDir$, #PB_MessageRequester_Ok)
Return
Have fun with PureBasic!
- Art Sentinel
http://www.artsentinel.net
--------------
Top Ten Reasons Not To Procrastinate:
Coming Soon...
Edited by - Art Sentinel on 02 August 2002 14:48:57