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

  1. /*-----------------------------------------
  2.    DIGCLOCK.C -- Digital Clock Program
  3.                  (c) Charles Petzold, 1996
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <time.h>
  8.  
  9. #define ID_TIMER    1
  10.  
  11. #define YEAR  (datetime->tm_year % 100)
  12. #define MONTH (datetime->tm_mon  + 1)
  13. #define MDAY  (datetime->tm_mday)
  14. #define WDAY  (datetime->tm_wday)
  15. #define HOUR  (datetime->tm_hour)
  16. #define MIN   (datetime->tm_min)
  17. #define SEC   (datetime->tm_sec)
  18.  
  19. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  20. void SizeTheWindow (int *, int *, int *, int *) ;
  21.  
  22. char  sDate[2], sTime[2], sAMPM[2][5] ;
  23. int   iDate, iTime ;
  24.  
  25. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                     PSTR szCmdLine, int iCmdShow)
  27.      {
  28.      static char szAppName[] = "DigClock" ;
  29.      HWND        hwnd ;
  30.      MSG         msg ;
  31.      int         xStart, yStart, xClient, yClient ;
  32.      WNDCLASSEX  wndclass ;
  33.  
  34.      wndclass.cbSize        = sizeof (wndclass) ;
  35.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  36.      wndclass.lpfnWndProc   = WndProc ;
  37.      wndclass.cbClsExtra    = 0 ;
  38.      wndclass.cbWndExtra    = 0 ;
  39.      wndclass.hInstance     = hInstance ;
  40.      wndclass.hIcon         = NULL ;
  41.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  42.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  43.      wndclass.lpszMenuName  = NULL ;
  44.      wndclass.lpszClassName = szAppName ;
  45.      wndclass.hIconSm       = NULL ;
  46.  
  47.      RegisterClassEx (&wndclass) ;
  48.  
  49.      SizeTheWindow (&xStart, &yStart, &xClient, &yClient) ;
  50.  
  51.      hwnd = CreateWindow (szAppName, szAppName,
  52.                           WS_POPUP | WS_DLGFRAME | WS_SYSMENU,
  53.                           xStart,  yStart,
  54.                           xClient, yClient,
  55.                           NULL, NULL, hInstance, NULL) ;
  56.  
  57.      if (!SetTimer (hwnd, ID_TIMER, 1000, NULL))
  58.           {
  59.           MessageBox (hwnd, "Too many clocks or timers!", szAppName,
  60.                       MB_ICONEXCLAMATION | MB_OK) ;
  61.           return FALSE ;
  62.           }
  63.  
  64.      ShowWindow (hwnd, SW_SHOWNOACTIVATE) ; 
  65.      UpdateWindow (hwnd) ;
  66.  
  67.      while (GetMessage (&msg, NULL, 0, 0))
  68.           {
  69.           TranslateMessage (&msg) ;
  70.           DispatchMessage (&msg) ;
  71.           }
  72.      return msg.wParam ;
  73.      }
  74.  
  75. void SizeTheWindow (int *pxStart,  int *pyStart,
  76.                     int *pxClient, int *pyClient)
  77.      {
  78.      HDC        hdc ;
  79.      TEXTMETRIC tm ;
  80.  
  81.      hdc = CreateIC ("DISPLAY", NULL, NULL, NULL) ;
  82.      GetTextMetrics (hdc, &tm) ;
  83.      DeleteDC (hdc) ;
  84.  
  85.      *pxClient = 2 * GetSystemMetrics (SM_CXDLGFRAME) + 16*tm.tmAveCharWidth ;
  86.      *pxStart  =     GetSystemMetrics (SM_CXSCREEN)   - *pxClient ;
  87.      *pyClient = 2 * GetSystemMetrics (SM_CYDLGFRAME) + 2*tm.tmHeight ;
  88.      *pyStart  =     0 ;
  89.      }
  90.  
  91. void SetInternational (void)
  92.      {
  93.      static char cName [] = "intl" ;
  94.  
  95.      iDate = GetProfileInt (cName, "iDate", 0) ;
  96.      iTime = GetProfileInt (cName, "iTime", 0) ;
  97.  
  98.      GetProfileString (cName, "sDate",  "/", sDate,    2) ;
  99.      GetProfileString (cName, "sTime",  ":", sTime,    2) ;
  100.      GetProfileString (cName, "s1159", "AM", sAMPM[0], 5) ;
  101.      GetProfileString (cName, "s2359", "PM", sAMPM[1], 5) ;
  102.      }
  103.  
  104. void WndPaint (HWND hwnd, HDC hdc)
  105.      {
  106.      static char szWday[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat" ;
  107.      char        cBuffer[40] ;
  108.      int         iLength ;
  109.      RECT        rect ;
  110.      struct tm  *datetime ;
  111.      time_t      lTime ;
  112.  
  113.      time (&lTime) ;
  114.      datetime = localtime (&lTime) ;
  115.      
  116.      iLength = wsprintf (cBuffer, "  %s  %d%s%02d%s%02d  \r\n",
  117.                (PSTR) szWday + 4 * WDAY,
  118.                iDate == 1 ? MDAY  : iDate == 2 ? YEAR  : MONTH, (PSTR) sDate,
  119.                iDate == 1 ? MONTH : iDate == 2 ? MONTH : MDAY,  (PSTR) sDate,
  120.                iDate == 1 ? YEAR  : iDate == 2 ? MDAY  : YEAR) ;
  121.  
  122.      if (iTime == 1)
  123.           iLength += wsprintf (cBuffer + iLength, "  %02d%s%02d%s%02d  ",
  124.                                HOUR, (PSTR) sTime, MIN, (PSTR) sTime, SEC) ;
  125.      else
  126.           iLength += wsprintf (cBuffer + iLength, "  %d%s%02d%s%02d %s  ",
  127.                                (HOUR % 12) ? (HOUR % 12) : 12,
  128.                                (PSTR) sTime, MIN, (PSTR) sTime, SEC,
  129.                                (PSTR) sAMPM [HOUR / 12]) ;
  130.  
  131.      GetClientRect (hwnd, &rect) ;
  132.      DrawText (hdc, cBuffer, -1, &rect, DT_CENTER | DT_NOCLIP) ;
  133.      }
  134.  
  135. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  136.      {
  137.      HDC         hdc ;
  138.      PAINTSTRUCT ps ;
  139.  
  140.      switch (iMsg)
  141.           {
  142.           case WM_CREATE :
  143.                SetInternational () ;
  144.                return 0 ;
  145.  
  146.           case WM_TIMER :
  147.                InvalidateRect (hwnd, NULL, FALSE) ;
  148.                return 0 ;
  149.  
  150.           case WM_PAINT :
  151.                hdc = BeginPaint (hwnd, &ps) ;
  152.                WndPaint (hwnd, hdc) ;
  153.                EndPaint (hwnd, &ps) ;
  154.                return 0 ;
  155.  
  156.           case WM_WININICHANGE :
  157.                SetInternational () ;
  158.                InvalidateRect (hwnd, NULL, TRUE) ;
  159.                return 0 ;
  160.  
  161.           case WM_DESTROY :
  162.                KillTimer (hwnd, ID_TIMER) ;
  163.                PostQuitMessage (0) ;
  164.                return 0 ;
  165.           }
  166.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  167.      }
  168.