Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Working with win 32 timers.

Working with win 32 timers.

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Suresh H

    Hello All, I have a working timer in MFC can anyone tell me how to change it to Win 32. MFC Timer Code. // Declarations #define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime; //Start Timer time(&lStartTime); SetTimer(s->m_hWnd,ELAPSED_TIMER, 1000, NULL); //Kill Timer KillTimer(s->m_hWnd, ELAPSED_TIMER); //Timer Function void CSDlg::OnTimer(UINT nIDEvent) { if(nIDEvent == ELAPSED_TIMER) { time(&lStopTime); cteElapsedTime = CTimeSpan(lStopTime-lStartTime); CString csElapsedTime; csElapsedTime.Format("%02d:%02d:%02d", cteElapsedTime.GetHours(), cteElapsedTime.GetMinutes(), cteElapsedTime.GetSeconds()); if(IsWindowVisible()) { m_TIME.SetWindowText(csElapsedTime); } } CDialog::OnTimer(nIDEvent); } How to change it to win 32 application ?? what are the changes which I have to make in the below code???? int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. INT_PTR mRet = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), GetDesktopWindow(), MainDlgProc); return 0; } BOOL CALLBACK MainDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: { SetTimer(hwnd,IDT_TIMER, 1000,(TIMERPROC) NULL); } break; case WM_CLOSE: EndDialog(hwnd, 0); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_EXIT: EndDialog(hwnd, 0); break; case IDC_Start: //Starts the timer on button click break; case IDC_End: //Ends the timer on button click KillTimer(hwnd, IDT_TIMER); break; } default: return FALSE; } return TRUE; } Thanking you, Suresh HC

    D Offline
    D Offline
    Don Box
    wrote on last edited by
    #3

    Handle WM_TIMER message. Rest r fine.

    Come online at:- jubinc@skype

    1 Reply Last reply
    0
    • P prasad_som

      Suresh H wrote:

      BOOL CALLBACK MainDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) {

      You need to add one more case here,

      case WM_TIMER:
      if((UINT)wParam== ELAPSED_TIMER)
      {
      //do your stuff
      }

      Prasad Notifier using ATL | Operator new[],delete[][^]

      S Offline
      S Offline
      Suresh H
      wrote on last edited by
      #4

      Hi Prasad, Thanks for the response. In the declaration only I am getting error. But it is working in MFC. Hw to do it in Win32 ??? #define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime; error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers fatal error C1004: unexpected end of file found

      P 1 Reply Last reply
      0
      • S Suresh H

        Hi Prasad, Thanks for the response. In the declaration only I am getting error. But it is working in MFC. Hw to do it in Win32 ??? #define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime; error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers fatal error C1004: unexpected end of file found

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #5

        Suresh H wrote:

        error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers

        Its MFC stuff. Use difftime instead. See its MSDN example.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        S 1 Reply Last reply
        0
        • P prasad_som

          Suresh H wrote:

          error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers

          Its MFC stuff. Use difftime instead. See its MSDN example.

          Prasad Notifier using ATL | Operator new[],delete[][^]

          S Offline
          S Offline
          Suresh H
          wrote on last edited by
          #6

          Hi Prasad, No much information in MSDN. i got this link for win32 timers. http://msdn2.microsoft.com/en-us/library/ms632592.aspx

          P 1 Reply Last reply
          0
          • S Suresh H

            Hi Prasad, No much information in MSDN. i got this link for win32 timers. http://msdn2.microsoft.com/en-us/library/ms632592.aspx

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #7

            I was taking about difftime[^] example from MSDN.

            Prasad Notifier using ATL | Operator new[],delete[][^]

            S 1 Reply Last reply
            0
            • P prasad_som

              I was taking about difftime[^] example from MSDN.

              Prasad Notifier using ATL | Operator new[],delete[][^]

              S Offline
              S Offline
              Suresh H
              wrote on last edited by
              #8

              Prasad example gives the time diffrence only. int main( void ) { time_t start, finish; long loop; double result, elapsed_time; printf( "Multiplying 2 floating point numbers 500 million times...\n" ); time( &start ); for( loop = 0; loop < 500000000; loop++ ) result = 3.63 * 5.27; time( &finish ); elapsed_time = difftime( finish, start ); printf( "\nProgram takes %6.0f seconds.\n", elapsed_time ); }

              P 1 Reply Last reply
              0
              • S Suresh H

                Prasad example gives the time diffrence only. int main( void ) { time_t start, finish; long loop; double result, elapsed_time; printf( "Multiplying 2 floating point numbers 500 million times...\n" ); time( &start ); for( loop = 0; loop < 500000000; loop++ ) result = 3.63 * 5.27; time( &finish ); elapsed_time = difftime( finish, start ); printf( "\nProgram takes %6.0f seconds.\n", elapsed_time ); }

                P Offline
                P Offline
                prasad_som
                wrote on last edited by
                #9

                You need to use one of time management[^] function. difftime is used as substitue for CTimeSpan.

                Prasad Notifier using ATL | Operator new[],delete[][^]

                1 Reply Last reply
                0
                • S Suresh H

                  Hello All, I have a working timer in MFC can anyone tell me how to change it to Win 32. MFC Timer Code. // Declarations #define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime; //Start Timer time(&lStartTime); SetTimer(s->m_hWnd,ELAPSED_TIMER, 1000, NULL); //Kill Timer KillTimer(s->m_hWnd, ELAPSED_TIMER); //Timer Function void CSDlg::OnTimer(UINT nIDEvent) { if(nIDEvent == ELAPSED_TIMER) { time(&lStopTime); cteElapsedTime = CTimeSpan(lStopTime-lStartTime); CString csElapsedTime; csElapsedTime.Format("%02d:%02d:%02d", cteElapsedTime.GetHours(), cteElapsedTime.GetMinutes(), cteElapsedTime.GetSeconds()); if(IsWindowVisible()) { m_TIME.SetWindowText(csElapsedTime); } } CDialog::OnTimer(nIDEvent); } How to change it to win 32 application ?? what are the changes which I have to make in the below code???? int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. INT_PTR mRet = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), GetDesktopWindow(), MainDlgProc); return 0; } BOOL CALLBACK MainDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: { SetTimer(hwnd,IDT_TIMER, 1000,(TIMERPROC) NULL); } break; case WM_CLOSE: EndDialog(hwnd, 0); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_EXIT: EndDialog(hwnd, 0); break; case IDC_Start: //Starts the timer on button click break; case IDC_End: //Ends the timer on button click KillTimer(hwnd, IDT_TIMER); break; } default: return FALSE; } return TRUE; } Thanking you, Suresh HC

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #10

                  And see Timers tutorial[^]


                  WhiteSky


                  S 1 Reply Last reply
                  0
                  • H Hamid Taebi

                    And see Timers tutorial[^]


                    WhiteSky


                    S Offline
                    S Offline
                    Suresh H
                    wrote on last edited by
                    #11

                    Thank you white sky. link is very usefull.:)

                    H 1 Reply Last reply
                    0
                    • S Suresh H

                      Thank you white sky. link is very usefull.:)

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #12

                      You're welcome ;)


                      WhiteSky


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups