home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap05 / beeper1.c next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.0 KB  |  95 lines

  1. /*-----------------------------------------
  2.    BEEPER1.C  -- Timer Demo Program No. 1
  3.                  (c) Charles Petzold, 1990
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #define ID_TIMER    1
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10.  
  11. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  12.                     LPSTR lpszCmdLine, int nCmdShow)
  13.      {
  14.      static char szAppName[] = "Beeper1" ;
  15.      HWND        hwnd ;
  16.      MSG         msg ;
  17.      WNDCLASS    wndclass ;
  18.  
  19.      if (!hPrevInstance) 
  20.           {
  21.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.           wndclass.lpfnWndProc   = WndProc ;
  23.           wndclass.cbClsExtra    = 0 ;
  24.           wndclass.cbWndExtra    = 0 ;
  25.           wndclass.hInstance     = hInstance ;
  26.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  29.           wndclass.lpszMenuName  = NULL ;
  30.           wndclass.lpszClassName = szAppName ;
  31.  
  32.           RegisterClass (&wndclass) ;
  33.           }
  34.  
  35.      hwnd = CreateWindow (szAppName, "Beeper1 Timer Demo",
  36.                           WS_OVERLAPPEDWINDOW,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           NULL, NULL, hInstance, NULL) ;
  40.  
  41.      while (!SetTimer (hwnd, ID_TIMER, 1000, NULL))
  42.           if (IDCANCEL == MessageBox (hwnd,
  43.                               "Too many clocks or timers!", szAppName,
  44.                               MB_ICONEXCLAMATION | MB_RETRYCANCEL))
  45.                return FALSE ;
  46.  
  47.      ShowWindow (hwnd, nCmdShow) ;
  48.      UpdateWindow (hwnd) ;
  49.  
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.           {
  52.           TranslateMessage (&msg) ;
  53.           DispatchMessage (&msg) ;
  54.           }
  55.      return msg.wParam ;
  56.      }
  57.  
  58. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  59.      {
  60.      static BOOL fFlipFlop = FALSE ;
  61.      HBRUSH      hBrush ;
  62.      HDC         hdc ;
  63.      PAINTSTRUCT ps ;
  64.      RECT        rc ;
  65.  
  66.      switch (message)
  67.           {
  68.           case WM_TIMER:
  69.                MessageBeep (0) ;
  70.  
  71.                fFlipFlop = !fFlipFlop ;
  72.                InvalidateRect (hwnd, NULL, FALSE) ;
  73.  
  74.                return 0 ;
  75.  
  76.           case WM_PAINT:
  77.                hdc = BeginPaint (hwnd, &ps) ;
  78.  
  79.                GetClientRect (hwnd, &rc) ;
  80.  
  81.                hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) :
  82.                                                       RGB(0,0,255)) ;
  83.                FillRect (hdc, &rc, hBrush) ;
  84.                EndPaint (hwnd, &ps) ;
  85.                DeleteObject (hBrush) ;
  86.                return 0 ;
  87.  
  88.           case WM_DESTROY:
  89.                KillTimer (hwnd, ID_TIMER) ;
  90.                PostQuitMessage (0) ;
  91.                return 0 ;
  92.           }
  93.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  94.      }
  95.