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

  1. /*-----------------------------------------
  2.    DIGCLOCK.C -- Digital Clock Program
  3.                  (c) Charles Petzold, 1990
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <time.h>
  8. #define ID_TIMER    1
  9.  
  10. #define YEAR  (datetime->tm_year % 100)
  11. #define MONTH (datetime->tm_mon  + 1)
  12. #define MDAY  (datetime->tm_mday)
  13. #define WDAY  (datetime->tm_wday)
  14. #define HOUR  (datetime->tm_hour)
  15. #define MIN   (datetime->tm_min)
  16. #define SEC   (datetime->tm_sec)
  17.  
  18. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  19. void SizeTheWindow (short *, short *, short *, short *) ;
  20.  
  21. char  sDate [2], sTime [2], sAMPM [2][5] ;
  22. int   iDate, iTime ;
  23.  
  24. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  25.                     LPSTR lpszCmdLine, int nCmdShow)
  26.      {
  27.      static char szAppName[] = "DigClock" ;
  28.      HWND        hwnd;
  29.      MSG         msg;
  30.      short       xStart, yStart, xClient, yClient ;
  31.      WNDCLASS    wndclass ;
  32.  
  33.      if (!hPrevInstance)
  34.           {
  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 = GetStockObject (WHITE_BRUSH) ;
  43.           wndclass.lpszMenuName  = NULL ;
  44.           wndclass.lpszClassName = szAppName ;
  45.  
  46.       RegisterClass (&wndclass) ;
  47.           }
  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 (short *pxStart,  short *pyStart,
  76.                     short *pxClient, short *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  =     GetSystemMetrics (SM_CYSCREEN)   - *pyClient ;
  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.      long        lTime ;
  109.      RECT        rect ;
  110.      short       nLength ;
  111.      struct tm   *datetime ;
  112.  
  113.      time (&lTime) ;
  114.      datetime = localtime (&lTime) ;
  115.      
  116.      nLength = wsprintf (cBuffer, "  %s  %d%s%02d%s%02d  \r\n",
  117.                (LPSTR) szWday + 4 * WDAY,
  118.                iDate == 1 ? MDAY  : iDate == 2 ? YEAR  : MONTH, (LPSTR) sDate,
  119.                iDate == 1 ? MONTH : iDate == 2 ? MONTH : MDAY,  (LPSTR) sDate,
  120.                iDate == 1 ? YEAR  : iDate == 2 ? MDAY  : YEAR) ;
  121.  
  122.      if (iTime == 1)
  123.           nLength += wsprintf (cBuffer + nLength, "  %02d%s%02d%s%02d  ",
  124.                                HOUR, (LPSTR) sTime, MIN, (LPSTR) sTime, SEC) ;
  125.      else
  126.           nLength += wsprintf (cBuffer + nLength, "  %d%s%02d%s%02d %s  ",
  127.                                (HOUR % 12) ? (HOUR % 12) : 12,
  128.                                (LPSTR) sTime, MIN, (LPSTR) sTime, SEC,
  129.                                (LPSTR) sAMPM [HOUR / 12]) ;
  130.  
  131.      GetClientRect (hwnd, &rect) ;
  132.      DrawText (hdc, cBuffer, -1, &rect, DT_CENTER | DT_NOCLIP) ;
  133.      }
  134.  
  135. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  136.      {
  137.      HDC         hdc ;
  138.      PAINTSTRUCT ps;
  139.  
  140.      switch (message)
  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, message, wParam, lParam) ;
  167.      }
  168.