home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP07 / BEEPER1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.0 KB  |  95 lines

  1. /*-----------------------------------------
  2.    BEEPER1.C  -- Timer Demo Program No. 1
  3.                  (c) Charles Petzold, 1996
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER    1
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14.      {
  15.      static char szAppName[] = "Beeper1" ;
  16.      HWND        hwnd ;
  17.      MSG         msg ;
  18.      WNDCLASSEX  wndclass ;
  19.  
  20.      wndclass.cbSize        = sizeof (wndclass) ;
  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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  32.  
  33.      RegisterClassEx (&wndclass) ;
  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, iCmdShow) ;
  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. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  59.      {
  60.      static BOOL fFlipFlop = FALSE ;
  61.      HBRUSH      hBrush ;
  62.      HDC         hdc ;
  63.      PAINTSTRUCT ps ;
  64.      RECT        rc ;
  65.  
  66.      switch (iMsg)
  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, iMsg, wParam, lParam) ;
  94.      }
  95.