Code:
' SIMPLE 'WRITE YOUR OWN' ENUM FUNCTION FOR PB/Win 7.0 ' Author: Michael Mattias Racine WI ' September 2002 ' Compiler: PB/Win v 7.0 ' ======================================================================== ' This demo shows how to write a simple enumeration function to process ' each element of a string array one at a time with an option to quit ' partway through. ' ======================================================================== ' The parameters for the callback function 'EnumElementsCallBackFunction' ' were designed specifically to allow the callback function to do 'what it ' has to do' and no more. #COMPILE EXE #INCLUDE "Win32API.INC" %USER_ABORT = 999 %ID_LABEL = 101 %ID_BUTTON = 102 %N_ELEMENTS = 6 ' number of elements in array ' Optional DECLARE for CALL DWORD: not needed in this demo because ' the function appears in the source code PRIOR to its use ' DECLARE FUNCTION EnumElementsCallBackFunction(BYVAL hDlg AS LONG, BYVAL hCtrl AS LONG, s AS STRING) AS LONG ' The function which is called for each array element when the enumeration occurs. ' Parameters needed: hDlg, control ID and text so we can CONTROL SET TEXT FUNCTION EnumElementsCallBackFunction(BYVAL hDlg AS LONG, BYVAL hCtrl AS LONG, s AS STRING) AS LONG LOCAL J AS LONG ' set the text of the label control fo this element CONTROL SET TEXT hDlg, hCtrl, S ' does user want to continue? J = MSGBOX ("Continue Enumeration?", %MB_YESNO OR %MB_TASKMODAL OR %MB_ICONQUESTION, "Yo! Response Required!") IF J = %IDYES THEN FUNCTION = %TRUE ' yes, continue the enumeration ELSE FUNCTION = %FALSE ' no, terminate now END IF END FUNCTION FUNCTION EnumArrayElements (BYVAL hDlg AS LONG, BYVAL hCtrl AS LONG, S() AS STRING, BYVAL dwCallbackAddress AS DWORD) AS LONG LOCAL I AS LONG, lContinue AS LONG ' for each element of the passed array, call the callback function ' with the defined parameters exactly once. FOR I = LBOUND(S,1) TO UBOUND (s,1) CALL DWORD dwCallBackAddress USING EnumElementsCallBackFunction (hDlg, hCtrl, S(I)) TO lContinue IF ISFALSE lContinue THEN FUNCTION = %USER_ABORT : EXIT FUNCTION END IF NEXT ' If we get this far, the user allowed the enumeration to continue ' until there was nothing left to do. FUNCTION = 0 END FUNCTION CALLBACK FUNCTION DialogProc LOCAL X() AS STRING, I AS LONG, umsg AS STRING IF CBMSG = %WM_COMMAND THEN IF CBCTL = %ID_BUTTON THEN REDIM X(%N_ELEMENTS) FOR I = 0 TO %N_ELEMENTS X(I) = "Array Element # " & STR$(I) NEXT I = EnumArrayElements(CBHNDL, %ID_LABEL, X(), CODEPTR(EnumElementsCallbackFunction)) IF I = %USER_ABORT THEN uMsg ="User terminated enumeration before completion" ELSE uMsg = "User Allowed enumeration to complete" END IF MSGBOX uMsg, %MB_OK OR %MB_ICONINFORMATION, "Enumeration Results" ELSEIF CBCTL = %IDCANCEL THEN DIALOG END CBHNDL, 0 END IF END IF END FUNCTION FUNCTION PBMAIN() AS LONG LOCAL hDlg AS LONG DIALOG NEW 0&, "Enum & Callback Demo", 10, 10, 120,100 TO hDlg CONTROL ADD LABEL ,hDlg, %ID_LABEL, "<null>", 10, 10, 60, 12 CONTROL ADD BUTTON ,hDlg, %ID_BUTTON, "Do it", 10, 30, 40,14 CONTROL ADD BUTTON, hDlg, %IDCANCEL, "Quit", 10, 50, 40,14 DIALOG SHOW MODAL hDlg, CALL DialogProc END FUNCTION ' ** END OF FILE **
------------------
Michael Mattias
Tal Systems Inc.
Racine WI USA
[email protected]
www.talsystems.com