Event handling
User interfaces on Windows are driven by events, also known as messages. This principle is very basic in its nature - something happens to dialog or control and Windows fires message so programmer can react to it appropriately.
ThinBASIC uses concept of callback functions. You can specify callback function for each dialog and control, and that function then recieves all the messages relevant to given GUI object.
For example, when you want to show MODAL dialog and receive its events in function "dialogEvents", you do it simply as following:
...
DIALOG SHOW MODAL hDlg, CALL dialogEvents
...
CALLBACK FUNCTION dialogEvents() AS LONG
...
END FUNCTION
For programmer comfort, ThinBASIC analyzes the messages of the dialog above and passes them as predefined variables to callback function (named dialog events in this case).
The predefined variables are the following:
- CBHndl - Callback dialog handle
- CBMsg - Callback dialog message
- CBWParam - Callback wParam
- CBLParam - Callback lParam
- CBCtl - Callback control
- CBCtlMsg - Callback control message
The messages received are standard Win32 messages, no wrappers. So you can find their description on Microsoft Developers Network.
The most used messages are:
- %WM_INITDIALOG - occurs right before dialog shows on the screen
- %WM_COMMAND - occurs when something happens to the dialog controls
- %WM_CLOSE - occurs when dialog is closing
From this you can intuitively guess the typical structure of dialog callback:
' -- Callback for dialog
CALLBACK FUNCTION dialogEvents()
' -- Test for messages
SELECT CASE CBMSG
CASE %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
CASE %WM_COMMAND
' -- You can handle controls here
'SELECT CASE CBCTL
'
' CASE ...
' IF CBCTLMSG = ... THEN
'
' END IF
'
'END SELECT
CASE %WM_CLOSE
' -- Put code to be executed before dialog end here
END SELECT
END FUNCTION
You are free to combine callbacks for whole dialog and particular controls. Even multiple controls can share the same callback.
Detailed information about messages dialog controls can receive you can check on yet mentioned Microsoft Developers Network, each control messages are also listed in ThinBASIC help file.
Little complete GUI sample is listed below:
USES "UI"
' -- ID numbers of controls
Begin Const
%btnClose = 1000
End Const
' -- Create dialog here
FUNCTION TBMAIN()
LOCAL hDlg AS DWORD
DIALOG New 0, "",-1,-1, 160, 120, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION OR %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
' -- Place controls here
CONTROL ADD BUTTON, hDlg, %btnClose, "Click to close", 95, 100, 60, 14, CALL btnCloseProc
DIALOG SHOW MODAL hDlg, CALL dialogEvents
END FUNCTION
' -- Callback for dialog
CALLBACK FUNCTION dialogEvents()
' -- Test for messages
SELECT CASE CBMSG
CASE %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
CASE %WM_COMMAND
' -- You can handle controls here
'SELECT CASE CBCTL
'
' CASE ...
' IF CBCTLMSG = ... THEN
'
' END IF
'
'END SELECT
CASE %WM_CLOSE
' -- Put code to be executed before dialog end here
END SELECT
END FUNCTION
' -- Callback for close button
CALLBACK FUNCTION btnCloseProc()
IF CBMSG = %WM_COMMAND THEN
IF CBCTLMSG = %BN_CLICKED THEN
' -- Closes the dialog
DIALOG END CBHNDL
END IF
END IF
END FUNCTION
|