' Progress bar program that shows how to handle a modeless window using a thread. ' This is an effective way of handling all messages to the "modeless" windows without ' compromising speed. It does not uses DIALOG DOENVENTS. ' It uses Semen's solution shown at the end of the thread. ' PBForms 1.50 was used to build, but I got rid of whatever comments and unnecesary code to ' simplify the example below. ' This is a 100% working sample. ' It was based on this thread: ' http://www.powerbasic.com/support/forums/Archives/Archive-000003/HTML/20020731-4-001417.html #COMPILE EXE #DIM ALL '---------------------------------------------------------------------------- ' ** Includes ** '---------------------------------------------------------------------------- #INCLUDE "WIN32API.INC" #INCLUDE "COMMCTRL.INC" '---------------------------------------------------------------------------- GLOBAL hDlgProgress AS DWORD GLOBAL gAbort AS LONG '---------------------------------------------------------------------------- ' ** Constants ** '---------------------------------------------------------------------------- %IDCANCEL = 2 %IDD_DIALOG1 = 101 %IDC_MSCTLS_PROGRESS32_1 = 1001 %IDC_LABEL1 = 1002 '---------------------------------------------------------------------------- '---------------------------------------------------------------------------- ' ** Declarations ** '---------------------------------------------------------------------------- DECLARE CALLBACK FUNCTION ShowDIALOG1Proc() DECLARE FUNCTION SampleProgress(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, Actual AS LONG) AS LONG DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG '---------------------------------------------------------------------------- '---------------------------------------------------------------------------- ' ** Main Application Entry Point ** '---------------------------------------------------------------------------- FUNCTION PBMAIN() LOCAL n AS LONG LOCAL Total AS LONG LOCAL hThread AS DWORD gAbort& = 0 ' Substitute the statement: ' ShowDIALOG1 %HWND_DESKTOP ' by: THREAD CREATE ShowDIALOG1(%HWND_DESKTOP) TO hThread??? THREAD CLOSE hThread??? TO n& ' Process the data and show the progress Total& = 10000000 FOR n& = 1 TO Total& IF (n&*100)/Total& = INT((n&*100)/Total&) THEN 'We can use either a call to a sub or directly sending the message to the control: ' SampleProgress hDlgProgress, %IDC_MSCTLS_PROGRESS32_1,(n&*100)\Total& CONTROL SEND hDlgProgress, %IDC_MSCTLS_PROGRESS32_1, %PBM_SETPOS, (n&*100)\Total&, 0 CONTROL SET TEXT hDlgProgress, %IDC_LABEL1, STR$(INT((n&*100)/Total&)) END IF ' If program has been stopped then quit inmediately IF gAbort& THEN EXIT FOR NEXT ' Close "Modeless" window DIALOG END hDlgProgress END FUNCTION '---------------------------------------------------------------------------- ' ** CallBacks ** '---------------------------------------------------------------------------- CALLBACK FUNCTION ShowDIALOG1Proc() SELECT CASE AS LONG CBMSG CASE %WM_COMMAND ' Process control notifications SELECT CASE AS LONG CBCTL CASE %IDCANCEL IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN ' Set abort flag on gAbort& = 1 DIALOG END CBHNDL, 0 END IF END SELECT END SELECT END FUNCTION '---------------------------------------------------------------------------- ' ** Sample Code ** '---------------------------------------------------------------------------- FUNCTION SampleProgress(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, Actual AS LONG) AS LONG CONTROL SEND hDlg, lID, %PBM_SETRANGE, 0, MAKDWD(0,100) CONTROL SEND hDlg, lID, %PBM_SETPOS, Actual&, 0 END FUNCTION '---------------------------------------------------------------------------- ' ** Dialogs ** '---------------------------------------------------------------------------- FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG LOCAL lRslt AS LONG DIALOG NEW hParent, "Dialog1", 70, 70, 201, 121, TO hDlgProgress CONTROL ADD "msctls_progress32", hDlgProgress, %IDC_MSCTLS_PROGRESS32_1, _ "", 48, 40, 100, 10, %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER CONTROL ADD BUTTON, hDlgProgress, %IDCANCEL, "&Cancel", 80, 80, 50, 15 CONTROL ADD LABEL, hDlgProgress, %IDC_LABEL1, "", 58, 58, 100, 10 SampleProgress hDlgProgress, %IDC_MSCTLS_PROGRESS32_1, 0 ' Although the Dialog is MODAL, its behaviour is Modeless DIALOG SHOW MODAL hDlgProgress, CALL ShowDIALOG1Proc TO lRslt FUNCTION = lRslt END FUNCTION '----------------------------------------------------------------------------