• Welcome to Jose's Read Only Forum 2023.
 

Looking for Timer example

Started by Chris Chancellor, November 08, 2018, 12:38:02 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello All

i'm looking for a simple timer example code in O2

something similar to this below PB program, need to know how to declare timer
and set timer and what to do CASE %WM_TIMER


' Timer example.bas

#COMPILE EXE
#DIM ALL
#INCLUDE "Win32Api.INC"

'===========================
CALLBACK FUNCTION Dlg_CB
   SELECT CASE CBMSG
      CASE %WM_INITDIALOG
        SetTimer CBHNDL, 1, 2000, BYVAL %NULL
      ' Every 2 seconds

      CASE %WM_TIMER
         CONTROL SET TEXT CBHNDL, 201, "Time: " + TIME$

      CASE %WM_DESTROY
            KillTimer CBHNDL, 1
   END SELECT
END FUNCTION

'===========================
FUNCTION PBMAIN () AS LONG
    LOCAL HDLG AS LONG
    DIALOG NEW 0, "Test", 1, 1, 80, 30, %DS_CENTER OR _
        %WS_CAPTION OR %WS_SYSMENU, 0 TO HDLG
    CONTROL ADD LABEL, Hdlg, 201, "", 10, 10, 80, 14
    DIALOG SHOW MODAL HDLG, CALL Dlg_CB
END FUNCTION

Chris Chancellor

Partially converted this PB program to O2

but,  i encounter a problem, when i click the Close button  ,  the dialog closes but  the program remains in memory

if you use the Task Manager, you would find that the program still running in the memory

anyone can please help me in this ?   as how to exit the program cleanly


' Timer_example.o2bas

$ filename = "Timer_example.exe"
uses rtl64

#lookahead

              uses corewin
              uses dialogs
              uses user

% DS_CENTER   =0x0800

' https://www.oxygenbasic.org/forum/index.php?topic=810.msg6615#msg6615
Type SYSTEMTIME
     word wYear
     word wMonth
     word wDayOfWeek
     word wDay   
     word wHour   
     word wMinute
     word wSecond
     word wMilliseconds
End Type

Declare GetSystemTime LIB "KERNEL32.DLL" ( SYSTEMTIME *lpSystemTime )
Declare GetLocalTime  Lib "kernel32.dll" ( ByRef lpSystemTime As SYSTEMTIME )

SYSTEMTIME   myTime



'   Equates
% IDC_LABEL1       = 201

'  Handle for the Main Dialog
     sys hDlg

'===========================
'  Call back function
FUNCTION Dlg_CB(hDlg,uint uMsg, sys wParam, lParam)  as sys CALLBACK

   SELECT CASE uMsg
      CASE WM_INITDIALOG
        ' set the timer for 2 seconds timing
          SetTimer hDlg, 1, 2000,  NULL
     

      CASE WM_TIMER
       '     CONTROL SET TEXT hDlg, 201, "Time: " + TIME$
             '     mbox " new time " + str(GetSystemTime(mytime))
                 GetLocalTime   myTime
                  mbox " new time " + mytime.wHour + ":" + mytime.wMinute + ":" + mytime.wSecond,0


        CASE WM_COMMAND
                   select case loword(wParam)
                              case IDCANCEL
                                          KillTimer hDlg, 1
                                         EndDialog( hDlg, null )
                              end select


      CASE WM_Close
                  KillTimer hDlg, 1
                  EndDialog( hDlg, null )

      CASE WM_DESTROY
            KillTimer hDlg, 1

   END SELECT
END FUNCTION



'===========================
FUNCTION O2MAIN() as sys
sys lpdt
         MSG wMsg
         dyn::init(lpdt)
   
       Dialog( 1, 10,10,80,30, "Timer Display ", lpdt,
                                    WS_CAPTION OR WS_SYSMENU or DS_CENTER or WS_VISIBLE,
                                    8,"Arial" )

        LText( "", IDC_LABEL1, 10, 10, 80, 14)

      hDlg = CreateModelessDialog( 0, @Dlg_CB, 0, lpdt )
         
     
        while GetMessage( @wMsg, null, 0, 0 ) <> 0
              if IsDialogMessage( hDlg,  @wMsg ) = 0 then
                            TranslateMessage( @wMsg )
                            DispatchMessage( @wMsg )
               end if
          wend


END FUNCTION     


     
'-------------------------------------
'  Start of program
  O2Main                 


Chris Chancellor


i have replaced the

with  EndDialog( hDlg, null )  with  DestroyWindow( hDlg )

but the program still remains in memory !  upon exit

José Roca

EndDialog is only useful to end a modal dialog box, and you're using a modeless dialog box and a message pump. Therefore, you a have to use PostQuiteMessage 0 in WM_DESTROY.

Chris Chancellor

Thanxx a lot  Jose

works correctly
learn something today