First, start with a Modeless Dialog Box

Now, a lot of programmers new to windows want to place thier code
inside of the PBMAIN() function block, let's try it:

Now, as you run it, you will see the labels not completly drawn until the end
this is because in order for windows to support multi-tasking it needs to process
many messages (or commands) coming from different programs at the same time.
Windows does this by putting messages on a queue (a circular list) and processes
them in any order they are recieved, how-ever some messages must have priority
over others, so windows uses a procedure that manages the messages.
This procedure, (or callback function) is called an event handler.
let's try a proper version that uses the callback function:

SIMPLE !!!
Also note that we didn't need to use a Modeless Dialog to process the messages
The Modal Dialog worked fine.

Code:
'EXAMPLE 5: A BASIC MODELESS DIALOG - DDT style '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #COMPILE EXE #INCLUDE "WIN32API.INC" '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CALLBACK FUNCTION My_DlgProc() AS LONG SELECT CASE CBMSG '----------- start message handler ---------- END SELECT '----------- end message handler ---------- END FUNCTION ' (My_DlgProc) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FUNCTION PBMAIN DIM hDlg AS LONG, Count AS LONG DIALOG NEW %HWND_DESKTOP, "A BASIC MODELESS DIALOG - DDT style",100,100,250,100, _ %WS_OVERLAPPEDWINDOW, TO hDlg& DIALOG SHOW MODELESS hDlg&, CALL My_DlgProc&() DO '----------- start message loop ---------- DIALOG DOEVENTS TO Count& LOOP WHILE Count& '----------- end message loop ---------- END FUNCTION ' (PBMAIN) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
inside of the PBMAIN() function block, let's try it:

Code:
#COMPILE EXE #INCLUDE "WIN32API.INC" '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FUNCTION PBMAIN DIM hDlg as LONG DIALOG NEW %HWND_DESKTOP, "Demo-ing improper placement of code",100,100,250,100, _ %WS_OVERLAPPEDWINDOW, TO hDlg CONTROL ADD LABEL, hDlg, 101,"This is a label",10,10,170,11, %SS_SUNKEN or %SS_CENTER CONTROL ADD LABEL, hDlg, 102,"This is a label",10,30,170,11, %SS_SUNKEN or %SS_CENTER DIALOG SHOW MODELESS hDlg&, 'a demonstration of improperly attempting to use code 'outside of an Event Handler CONTROL SET TEXT hDlg,102, "This is a label" SLEEP 1500 CONTROL SET TEXT hDlg,102, "Changing the text " SLEEP 1500 CONTROL SET TEXT hDlg,102, "This is the last change" '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DO '----------- start message loop ---------- DIALOG DOEVENTS TO Count& LOOP WHILE Count& '----------- end message loop ---------- END FUNCTION ' (PBMAIN)
this is because in order for windows to support multi-tasking it needs to process
many messages (or commands) coming from different programs at the same time.
Windows does this by putting messages on a queue (a circular list) and processes
them in any order they are recieved, how-ever some messages must have priority
over others, so windows uses a procedure that manages the messages.
This procedure, (or callback function) is called an event handler.
let's try a proper version that uses the callback function:

Code:
#COMPILE EXE #INCLUDE "WIN32API.INC" '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CALLBACK FUNCTION My_DlgProc STATIC My_TextFlg as LONG SELECT CASE CBMSG '----------- start message handling ---------- CASE %WM_INITDIALOG SetTimer CBHNDL, 1, 1000, BYVAL 0 FUNCTION=%TRUE CASE %WM_TIMER SELECT CASE My_TextFlg CASE 0 CONTROL SET TEXT CBHNDL,102, "This is a label" INCR My_TextFlg CASE 1 CONTROL SET TEXT CBHNDL,102, "Changing the text " BEEP INCR My_TextFlg CASE 2 CONTROL SET TEXT CBHNDL,102, "This is the last change" My_TextFlg = 0 END SELECT CASE %WM_DESTROY KillTimer CBHNDL, 1 END SELECT '----------- end message handling ---------- END FUNCTION '(My_DlgProc) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FUNCTION PBMAIN DIM hDlg as LONG DIALOG NEW %HWND_DESKTOP, "Demo: using the Event Handler",100,100,250,100, _ %WS_OVERLAPPEDWINDOW, TO hDlg CONTROL ADD LABEL, hDlg, 101,"This is a label",10,10,170,11, %SS_SUNKEN or %SS_CENTER CONTROL ADD LABEL, hDlg, 102,"This is a label",10,30,170,11, %SS_SUNKEN or %SS_CENTER DIALOG SHOW MODAL hDlg, CALL My_DlgProc&() END FUNCTION ' (PBMAIN)
Also note that we didn't need to use a Modeless Dialog to process the messages
The Modal Dialog worked fine.

Comment