Author Topic: Source Code -- Example Timer program  (Read 84 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Source Code -- Example Timer program
« on: November 08, 2018, 08:49:08 PM »
Hello All

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

Thanxx to Jose and Charles



Code: [Select]
' 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                 

« Last Edit: November 09, 2018, 05:14:44 AM by Chris Chancellor »

Offline Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
    • Josť Roca Software
Re: Example Timer program
« Reply #1 on: November 08, 2018, 09:27:08 PM »
Code: [Select]
      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:

Code: [Select]
      CASE WM_Close
                  KillTimer hDlg, 1
                  PostQuitMessage 0   
                  DestroyWindow( hDlg )

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Example Timer program
« Reply #2 on: November 09, 2018, 03:06:25 AM »
Thanxx a lot Jose

i have modified the code as below

Code: [Select]
' 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