home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / TIMERS / TIMERS.C_ / TIMERS.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  8.8 KB  |  307 lines

  1. #if 0
  2.  
  3. ABSTRACT START
  4.  
  5. TITLE
  6.         Windows Timer Sample Application
  7. EXE
  8.         timers.exe
  9. AUTHOR
  10.         Bob Gunderson - Microsoft Corp.
  11. CONTENT
  12.         This sample application demonstrates how to create and terminate
  13.         application timers and how to change the rate of an existing timer.
  14.  
  15.         On menu commands, it will create a timer that can either dispatch a
  16.         WM_TIMER message to a window procedure or call a callback function.
  17.         The timer can be set to run at one of three speeds.
  18.  
  19.         The sample also demonstrates calling the Toolhelp.dll timer services.
  20. TAG
  21.         1.3.2
  22.         2.1
  23.         2.2
  24.         3.1.1.21
  25.         3.1.15
  26.  
  27. ABSTRACT END
  28.  
  29. #endif
  30.  
  31.  
  32. #include "windows.h"
  33. #include "timers.h"
  34. #include "toolhelp.h"
  35.  
  36. //---------------------------------------------------------------------------
  37. // Global Variables...
  38. //---------------------------------------------------------------------------
  39.  
  40. HANDLE  hInstance;              // Global instance handle for application
  41. int     TimerType = cmdStop;    // Current timer type
  42. int     TimerSpeed = cmdSlow;   // Current timer speed
  43. int     hwndKill, idKill;       // Values to use on KillTimer
  44. int     Millisec = 5000;        // Current timer speed in milliseconds
  45. FARPROC fpTimerCallback;        // MakeProcInstance of callback
  46. HWND    hwndMain;               // Main hwnd.  Needed in callback
  47. int     counter = 0;            // Counter to display when timer goes off
  48. char    szText[80];             // Temp storage to build strings
  49. TIMERINFO  ti;                  // Structure to pass to Toolhelp.dll
  50.  
  51. //---------------------------------------------------------------------------
  52. // Function declarations
  53. //---------------------------------------------------------------------------
  54.  
  55. LONG FAR PASCAL __export TimerTestWndProc(HWND, UINT, WPARAM, LPARAM);
  56. void StartTimer(HWND, int, int);
  57. WORD FAR PASCAL __export TimerCallbackProc(HWND, WORD, int, DWORD);
  58.  
  59. //---------------------------------------------------------------------------
  60. // WinMain
  61. //---------------------------------------------------------------------------
  62.  
  63. int PASCAL WinMain(HANDLE hInst, HANDLE hInstPrev, LPSTR lpstrCmdLine, int cmdShow)
  64. {
  65.     MSG msgMain;
  66.     WNDCLASS wc;
  67.  
  68.     // Set the global instance variable
  69.     hInstance = hInst;
  70.  
  71.     // Register the window class if this is the first instance.
  72.     if (hInstPrev == NULL)
  73.     {
  74.         wc.lpszMenuName     = "TimerMenu";
  75.         wc.lpszClassName    = "TimerTestApp";
  76.         wc.hInstance        = hInst;
  77.         wc.hIcon            = LoadIcon(hInst, "TimerIcon");
  78.         wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  79.         wc.hbrBackground    = COLOR_WINDOW + 1;
  80.         wc.style            = 0;
  81.         wc.lpfnWndProc      = TimerTestWndProc;
  82.         wc.cbClsExtra       = 0;
  83.         wc.cbWndExtra       = 0;
  84.  
  85.         if (!RegisterClass(&wc))
  86.             return(0);
  87.     }
  88.  
  89.     // Create the main window
  90.     if ((hwndMain = CreateWindow("TimerTestApp",
  91.                                  "Sample Timer Application",
  92.                                  WS_OVERLAPPEDWINDOW,
  93.                                  CW_USEDEFAULT, 0,
  94.                                  CW_USEDEFAULT, CW_USEDEFAULT,
  95.                                  NULL, NULL, hInst, NULL)) == NULL)
  96.         return(0);
  97.  
  98.         // Need to call MakeProcInstance because multiple instances of this
  99.         // application can exist.
  100.     fpTimerCallback = MakeProcInstance(TimerCallbackProc, hInst);
  101.  
  102.         // Show the window and make sure it is updated.
  103.     ShowWindow(hwndMain, cmdShow);
  104.     UpdateWindow(hwndMain);
  105.  
  106.     // Main message "pump"
  107.     while (GetMessage((LPMSG) &msgMain, NULL, 0, 0))
  108.     {
  109.        TranslateMessage((LPMSG) &msgMain);
  110.        DispatchMessage((LPMSG) &msgMain);
  111.     }
  112.  
  113.         // Even though Windows destroys timers created by this app, destroying
  114.         // the things the application creates is good practice.
  115.     if (TimerType != cmdStop)
  116.         KillTimer(hwndKill, idKill);
  117.  
  118.     return(0);
  119. }
  120.  
  121.  
  122.  
  123. //---------------------------------------------------------------------------
  124. // TimerTestWndProc
  125. //
  126. // Window procedure for the sample applications window.
  127. //
  128. //---------------------------------------------------------------------------
  129.  
  130. LONG FAR PASCAL __export TimerTestWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  131. {
  132.     HMENU       hMenu;
  133.     HDC         hdc;
  134.  
  135.     switch(msg) {
  136.  
  137.         //
  138.         // Handle menu selections
  139.         //
  140.         case WM_COMMAND:
  141.             switch (wParam) {
  142.  
  143.                 case cmdStop:
  144.                 case cmdToHwnd:
  145.                 case cmdCallProc:
  146.                     //
  147.                     // Change the checkmark to the proper item
  148.                     //
  149.                     hMenu = GetMenu(hwnd);
  150.                     CheckMenuItem(hMenu, TimerType, MF_UNCHECKED | MF_BYCOMMAND);
  151.                     CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
  152.                     //
  153.                     // Kill any existing timer.  Also clear the text displayed
  154.                     // by a previous timer going off.
  155.                     //
  156.                     KillTimer(hwndKill, idKill);
  157.                     idKill = 0;
  158.                     counter = 0;
  159.                     InvalidateRect(hwnd, NULL, TRUE);
  160.                     UpdateWindow(hwnd);
  161.                     //
  162.                     // Set the new timer type and start it up.
  163.                     //
  164.                     TimerType = wParam;
  165.                     if (wParam != cmdStop)
  166.                         StartTimer(hwnd, TimerType, TimerSpeed);
  167.                     break;
  168.  
  169.                 //
  170.                 // Set the timer speed
  171.                 //
  172.                 case cmdFast:
  173.                     Millisec = 250;
  174.                     goto Common;
  175.                 case cmdMedium:
  176.                     Millisec = 1000;
  177.                     goto Common;
  178.                 case cmdSlow:
  179.                     Millisec = 5000;
  180. Common:
  181.                     //
  182.                     // Change the checkmark to the proper item
  183.                     //
  184.                     hMenu = GetMenu(hwnd);
  185.                     CheckMenuItem(hMenu, TimerSpeed, MF_UNCHECKED | MF_BYCOMMAND);
  186.                     CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
  187.                     //
  188.                     // Reset the timer.  No need to kill the existing one and
  189.                                         // start a new one.  Windows resets an existing timer
  190.                     // with a new speed if the hWnd and ID are identical to
  191.                     // an existing timer.
  192.                     //
  193.                     TimerSpeed = wParam;
  194.                     if (TimerType != cmdStop)
  195.                         StartTimer(hwnd, TimerType, TimerSpeed);
  196.                     break;
  197.  
  198.             }
  199.             break;
  200.  
  201.         case WM_TIMER:
  202.             //
  203.             // Timer set to dispatch a WM_TIMER message has gone off.
  204.             // Display a little message and beep the speaker.
  205.             //
  206.             MessageBeep(0);
  207.             hdc = GetDC(hwnd);
  208.             wsprintf((LPSTR)&szText, "WM_TIMER sent to hwnd - %d    ", counter++);
  209.             TextOut(hdc, 10, 10,(LPSTR)&szText, lstrlen((LPSTR)&szText));
  210.             //
  211.             // Now call Toolhelp to get the system timer numbers
  212.             //
  213.             ti.dwSize = sizeof(TIMERINFO);
  214.             if (!TimerCount((TIMERINFO FAR *)&ti))
  215.             {
  216.                 ReleaseDC(hwnd, hdc);
  217.                 break;
  218.             }
  219.             wsprintf((LPSTR)&szText, "System: %lu, this VM: %lu    ", ti.dwmsSinceStart, ti.dwmsThisVM);
  220.             TextOut(hdc, 10, 40, (LPSTR)&szText, lstrlen((LPSTR)&szText));
  221.             ReleaseDC(hwnd, hdc);
  222.             break;
  223.  
  224.         case WM_DESTROY:
  225.             PostQuitMessage(0);
  226.             break;
  227.  
  228.         default:
  229.             return(DefWindowProc(hwnd, msg, wParam, lParam));
  230.     }
  231.  
  232.     return(0);
  233. }
  234.  
  235. //---------------------------------------------------------------------------
  236. // StartTimer
  237. //
  238. // Starts a timer to either dispatch a WM_TIMER message or call a callback.
  239. // Care is taken to save the proper hWnd and timer ID for use when killing
  240. // the timer.
  241. //---------------------------------------------------------------------------
  242.  
  243. void StartTimer(HWND hwnd, int Type, int Speed)
  244. {
  245.     switch (Type) {
  246.  
  247.         //
  248.         // Create a timer to dispatch a WM_TIMER message.
  249.         //
  250.         case cmdToHwnd:
  251.             if (SetTimer(hwnd, 1, Millisec, NULL) == NULL)
  252.             {
  253.                 MessageBox(hwnd, "Couldn't create timer.", "Notice!", MB_OK);
  254.                 return;
  255.             }
  256.             hwndKill = hwnd;
  257.             idKill = 1;
  258.             break;
  259.  
  260.         //
  261.                 // Create a timer to call a callback function.  If we are
  262.                 // changing only the timer rate, be sure that the ID parameter is
  263.         // the idKill value returned from the initial SetTimer call.
  264.         //
  265.         case cmdCallProc:
  266.             if ((idKill = SetTimer(NULL, (idKill ? idKill : 1), Millisec, fpTimerCallback)) == NULL)
  267.             {
  268.                 MessageBox(hwnd, "Couldn't create timer.", "Notice!", MB_OK);
  269.                 return;
  270.             }
  271.             hwndKill = NULL;
  272.             break;
  273.  
  274.     }
  275. }
  276.  
  277. //---------------------------------------------------------------------------
  278. // TimerCallbackProc
  279. //
  280. // Callback function that is called when a timer setup to call the callback
  281. // goes off.  Displays a short message and beeps the speaker.
  282. //---------------------------------------------------------------------------
  283.  
  284. WORD FAR PASCAL __export TimerCallbackProc(HWND hwnd, WORD msg, int idEvent, DWORD dwtime)
  285.  
  286. {
  287.     HDC hdc;
  288.  
  289.     MessageBeep(0);
  290.     hdc = GetDC(hwndMain);
  291.     wsprintf((LPSTR)&szText, "Callback function called - %d    ", counter++);
  292.     TextOut(hdc, 10, 10, (LPSTR)&szText, lstrlen((LPSTR)&szText));
  293.     //
  294.     // Now call Toolhelp to get the system timer numbers
  295.     //
  296.     ti.dwSize = sizeof(TIMERINFO);
  297.     if (!TimerCount((TIMERINFO FAR *)&ti))
  298.     {
  299.         ReleaseDC(hwnd, hdc);
  300.         return(0);
  301.     }
  302.     wsprintf((LPSTR)&szText, "System: %lu, this VM: %lu    ", ti.dwmsSinceStart, ti.dwmsThisVM);
  303.     TextOut(hdc, 10, 40, (LPSTR)&szText, lstrlen((LPSTR)&szText));
  304.     ReleaseDC(hwndMain, hdc);
  305.     return(0);
  306. }
  307.