• Welcome to Jose's Read Only Forum 2023.
 

Source Code -- Example Timer program

Started by Chris Chancellor, November 08, 2018, 08:49:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello All

this is an example usage of a timer program in O2
O2 is a very good language

Thanxx to Jose and Charles




' 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

    sys hLabText1=GetDlgItem(hDlg, IDC_LABEL1)
    string  TimeStr

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

      CASE WM_TIMER
           '    when time changes (every 2 secs)
                GetLocalTime   myTime
          '     mbox " Current time " + mytime.wHour + ":" + mytime.wMinute + ":" + mytime.wSecond,0
               TimeStr =  " Current time " + mytime.wHour + ":" + mytime.wMinute + ":" + mytime.wSecond 
               SetWindowText (hLabText1, TimeStr)


        CASE WM_COMMAND
                   select case loword(wParam)
                              case IDCANCEL
                                          KillTimer hDlg, 1
                                          PostQuitMessage 0
                                          DestroyWindow( hDlg )
                              end select


      CASE WM_Close
                  KillTimer hDlg, 1
                  PostQuitMessage 0   
                  DestroyWindow( hDlg )


      CASE WM_DESTROY
            KillTimer hDlg, 1
            PostQuitMessage 0

   END SELECT
END FUNCTION



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

        LText( "", IDC_LABEL1, 10, 10, 85, 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                 



José Roca


      CASE WM_Close
                  KillTimer hDlg, 1
                  PostQuitMessage 0   
                  DestroyWindow( hDlg )


      CASE WM_DESTROY
            KillTimer hDlg, 1
            PostQuitMessage 0


Killing the timer and posting PostQuitMessage twice? All this code is unneeded:


      CASE WM_Close
                  KillTimer hDlg, 1
                  PostQuitMessage 0   
                  DestroyWindow( hDlg )

Chris Chancellor

Thanxx a lot Jose

i have modified the code as below


' Timer_example.o2bas

$ filename = "Timer_example.exe"
uses rtl64

#lookahead

              uses corewin
              uses dialogs
              uses user

% DS_CENTER   =0x0800


' the following codes is related to getting date and time
' 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



'   Identifiers
% 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

    sys hLabText1=GetDlgItem(hDlg, IDC_LABEL1)
    string  TimeStr

   SELECT CASE uMsg
      CASE WM_INITDIALOG
        ' set the timer for 2 seconds timing
                SetTimer hDlg, 1, 2000,  NULL
                 GetLocalTime   myTime
                TimeStr =  " Current time " + mytime.wHour + ":" + mytime.wMinute + ":" + mytime.wSecond 
               SetWindowText (hLabText1, TimeStr)

      CASE WM_TIMER
           '    when time changes (every 2 secs)
                GetLocalTime   myTime
          '     mbox " Current time " + mytime.wHour + ":" + mytime.wMinute + ":" + mytime.wSecond,0
               TimeStr =  " Current time " + mytime.wHour + ":" + mytime.wMinute + ":" + mytime.wSecond 
               SetWindowText (hLabText1, TimeStr)


        CASE WM_COMMAND
                   select case loword(wParam)
                              case IDCANCEL
                                          KillTimer hDlg, 1
                                          PostQuitMessage 0
                                          DestroyWindow( hDlg )
                              end select


 


      CASE WM_DESTROY
            KillTimer hDlg, 1
            PostQuitMessage 0

   END SELECT
END FUNCTION



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

        LText( "", IDC_LABEL1, 10, 10, 85, 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