home *** CD-ROM | disk | FTP | other *** search
/ TestDrive Super Store 2.3 / TESTDRIVE_2.ISO / realizer / samples / refch27 / ccsample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-30  |  7.7 KB  |  337 lines

  1. /************************************************
  2.  *
  3.  *  Realizer Reference Manual Sample Program
  4.  *
  5.  *  Chapter 27: CCSample.C
  6.  *
  7.  *  Copyright ⌐ 1991-1992 Computer Associates International, Inc.
  8.  *  All rights reserved.
  9.  *
  10.  ************************************************/
  11.  
  12.  
  13. #include <windows.h> 
  14. #include "CustCtrl.h" 
  15.  
  16. HANDLE    hLibInstance; 
  17.  
  18. /* 
  19.  * Constants for the countdown timer 
  20.  */ 
  21.  
  22. #define TIMER_CLASS    "CountDownTimer" 
  23.  
  24. #define TIMER_FONT    0        /* 2 bytes, handle to font */ 
  25. #define TIMER_DOWNCT    2    /* 2 bytes, current count (65535 max) */ 
  26. #define TIMER_ON        4    /* 2 bytes, non-zero if on */ 
  27.                             /*  bit 0: set if timer is running */ 
  28.                             /*  bit 1: toggles display at 0 */ 
  29. #define TIMER_ENDTICK    6    /* 4 bytes, GetTickCount value at which */ 
  30.                             /*   TIMER_DOWNCT should hit zero */ 
  31. #define TIMER_EXTRA    10 
  32.  
  33.  
  34. /* 
  35.  * Constants for the size message generator 
  36.  */ 
  37.  
  38. #define SIZEMSG_CLASS    "SizeMsg" 
  39. #define SIZEMSG_EXTRA    0 
  40.  
  41.  
  42. /* 
  43.  * Notify Parent - notifies the custom control's form 
  44.  */ 
  45.  
  46. void NotifyParent (HWND hWnd) 
  47.     SendMessage( 
  48.         GetParent(hWnd),            /* this is the form */ 
  49.         WM_COMMAND, 
  50.         GetWindowWord(hWnd, GWW_ID),    /* this is hWnd's item num */ 
  51.         MAKELONG((int)hWnd, BN_CLICKED)    /* looks like a click */ 
  52.     ); 
  53.  
  54.  
  55. /* 
  56.  * FormatSecs - formats the display for the timer 
  57.  */ 
  58.  
  59. void FormatSecs(HWND hWnd, LPSTR buf) 
  60.     WORD    wOn, wSecs; 
  61.     int        len; 
  62.  
  63.     wOn = GetWindowWord(hWnd, TIMER_ON); 
  64.     wSecs = GetWindowWord(hWnd, TIMER_DOWNCT); 
  65.  
  66.     if (wSecs > 9999) 
  67.         wsprintf(buf, "****"); 
  68.     else { 
  69.         len = wsprintf(buf, "%d", wSecs); 
  70.         if ((wOn & 2) != 0) 
  71.             buf[len - 1] = '_'; 
  72.     } 
  73.  
  74.  
  75. /* 
  76.  * TimerServer - Window procedure for the countdown timer 
  77.  */ 
  78.  
  79. extern LONG FAR PASCAL 
  80. TimerServer(HWND hWnd, unsigned msg, WORD wP, LONG lP) 
  81.     PAINTSTRUCT    ps; 
  82.     RECT        cr; 
  83.     HFONT        hFont; 
  84.     HDC        hDC; 
  85.     HBRUSH    hBgndBrush; 
  86.     LPLONG    lpMods; 
  87.     LONG        lTick, lNewCount; 
  88.     WORD        wOn; 
  89.     int        height, wCount; 
  90.     char        buf[10]; 
  91.  
  92.     switch (msg) { 
  93.  
  94.     case WM_DESTROY: 
  95.  
  96.         /* If the timer is running, kill it */ 
  97.         if (GetWindowWord(hWnd, TIMER_ON)) 
  98.             KillTimer(hWnd, 1); 
  99.         break; 
  100.  
  101.     case WM_TIMER: 
  102.  
  103.         /* If the timer is still running... */ 
  104.         if (wOn = GetWindowWord(hWnd, TIMER_ON)) { 
  105.  
  106.             /* Force the display to be updated */ 
  107.             InvalidateRect(hWnd, NULL, TRUE); 
  108.  
  109.             if (wCount = GetWindowWord(hWnd, TIMER_DOWNCT)) { 
  110.  
  111.                 /* Adjust the count to the actual time */ 
  112.                 lTick = GetWindowLong(hWnd, TIMER_ENDTICK); 
  113.                 lTick = (lTick - GetTickCount() + 500) / 1000; 
  114.                 if (HIWORD(lTick)) 
  115.                     wCount = 0; 
  116.                 else 
  117.                     wCount = LOWORD(lTick); 
  118.                 SetWindowWord(hWnd, TIMER_DOWNCT, wCount); 
  119.  
  120.             } else { 
  121.  
  122.                 /* The timer is at zero. Toggle the display */ 
  123.                 SetWindowWord(hWnd, TIMER_ON, wOn ^ 2); 
  124.  
  125.             } 
  126.  
  127.             /* If the timer has hit zero, notify the form */ 
  128.             if (wCount == 0) 
  129.                 NotifyParent(hWnd); 
  130.         } 
  131.         break; 
  132.  
  133.     case WM_SETFONT: 
  134.  
  135.         /* Store the font handle in TIMER_FONT */ 
  136.         if (wP) { 
  137.             SetWindowWord(hWnd, TIMER_FONT, wP); 
  138.             InvalidateRect(hWnd, NULL, TRUE); 
  139.         } 
  140.         break; 
  141.  
  142.     case CCM_SETNUM: 
  143.  
  144.         /* Get the modifer list */ 
  145.         lpMods = (LPLONG)lP; 
  146.  
  147.         /* If the new count is out of range, return 0 */ 
  148.         lNewCount = lpMods[0]; 
  149.         if (lNewCount < 0 || lNewCount > 65535) 
  150.             return (0); 
  151.  
  152.         /* Store the new count and force a display update */ 
  153.         SetWindowWord(hWnd, TIMER_DOWNCT, LOWORD(lNewCount)); 
  154.         InvalidateRect(hWnd, NULL, TRUE); 
  155.  
  156.         /* Resynchronize the timer */ 
  157.         KillTimer(hWnd, 1);                     
  158.         if (lNewCount == 0) 
  159.             SetWindowWord(hWnd, TIMER_ON, 0); 
  160.         else { 
  161.             SetWindowWord(hWnd, TIMER_ON, 1); 
  162.             SetTimer(hWnd, 1, 1000, NULL); 
  163.             SetWindowLong(hWnd, TIMER_ENDTICK,  
  164.                     lNewCount * 1000 + GetTickCount()); 
  165.         } 
  166.         return (1);             
  167.  
  168.     case CCM_GETNUM: 
  169.  
  170.         /* Return the value of the timer, -1 if it's off */ 
  171.         if (GetWindowWord(hWnd, TIMER_ON)) 
  172.             return (GetWindowWord(hWnd, TIMER_DOWNCT)); 
  173.         else 
  174.             return (-1L); 
  175.  
  176.     case CCM_QDEFAULTSIZE: 
  177.  
  178.         /* Calculate the size of "9999" and return that size */ 
  179.         hDC = GetDC(hWnd); 
  180.         if (hFont = GetWindowWord(hWnd, TIMER_FONT)) 
  181.             SelectObject(hDC, hFont); 
  182.         cr.left = cr.top = 0; 
  183.         cr.right = cr.bottom = 100; 
  184.         height = DrawText(hDC, "9999", -1, &cr,  
  185.                             DT_SINGLELINE | DT_CALCRECT); 
  186.         ReleaseDC(hWnd, hDC); 
  187.         return (MAKELONG(cr.right - cr.left + 2, height + 2)); 
  188.  
  189.     case CCM_GETFLAGS: 
  190.     case CCM_FORMSIZED: 
  191.     case CCM_RESETCONTENT: 
  192.         break; 
  193.  
  194.     case WM_ERASEBKGND: 
  195.  
  196.         /* let WM_PAINT do it */ 
  197.         return (1L); 
  198.  
  199.     case WM_PAINT: 
  200.  
  201.         /* Always do the whole display */ 
  202.         InvalidateRect(hWnd, NULL, TRUE);     
  203.  
  204.         BeginPaint(hWnd, &ps); 
  205.  
  206.         /* Color the background */ 
  207.         hBgndBrush = (HBRUSH)SendMessage(GetParent(hWnd), 
  208.             WM_CTLCOLOR, ps.hdc, MAKELONG(hWnd, CTLCOLOR_STATIC)); 
  209.         FillRect(ps.hdc, &ps.rcPaint, hBgndBrush); 
  210.  
  211.         /* Display the timer count */ 
  212.         FormatSecs(hWnd, buf); 
  213.         GetClientRect(hWnd, &cr); 
  214.         if (hFont = GetWindowWord(hWnd, TIMER_FONT)) 
  215.             SelectObject(ps.hdc, hFont); 
  216.         SetBkMode(ps.hdc, TRANSPARENT); 
  217.         DrawText(ps.hdc, buf, -1, &cr,  
  218.                     DT_SINGLELINE | DT_RIGHT | DT_VCENTER); 
  219.         EndPaint(hWnd, &ps); 
  220.         break; 
  221.  
  222.     default: 
  223.         return (DefWindowProc(hWnd, msg, wP, lP)); 
  224.     } 
  225.  
  226.     return (0L); 
  227.  
  228.  
  229. /* 
  230.  * SizeMsgServer - Window procedure for the 
  231.  *   size message generator 
  232.  */ 
  233.  
  234.  
  235. extern LONG FAR PASCAL 
  236. SizeMsgServer(HWND hWnd, unsigned msg, WORD wP, LONG lP) 
  237.     switch (msg) { 
  238.  
  239.         case CCM_QDEFAULTSIZE: 
  240.             return(0L); 
  241.  
  242.         case CCM_GETFLAGS: 
  243.             return(0L); 
  244.  
  245.         case CCM_FORMSIZED: 
  246.             NotifyParent(hWnd); 
  247.             break; 
  248.  
  249.         default: 
  250.             return(DefWindowProc(hWnd, msg, wP, lP)); 
  251.         } 
  252.  
  253.     return (0L); 
  254.  
  255.  
  256. /* 
  257.  * LibMain(hInstance, wDataSegment, wHeapSize, lpszCmdLine) 
  258.  * 
  259.  *    hInstance      library instance handle 
  260.  *    wDataSegment   library data segment 
  261.  *    wHeapSize      default heap size 
  262.  *    lpszCmdLine    command line arguments 
  263.  * 
  264.  * LibMain is called by LibEntry, which is called by Windows  
  265.  * when the DLL is loaded.  The LibEntry routine is provided 
  266.  * in the LIBENTRY.OBJ in the SDK Link Libraries disk. The  
  267.  * source, LIBENTRY.ASM, is also provided.   
  268.  * 
  269.  * LibEntry initializes the DLL's heap, if a HEAPSIZE value is 
  270.  * specified in the DLL's DEF file.  LibEntry then calls 
  271.  * LibMain.  The LibMain function below satisfies that call. 
  272.  * 
  273.  * LibMain registers the custom control window classes. 
  274.  * 
  275.  */ 
  276.   
  277. int FAR PASCAL LibMain( 
  278.    HANDLE      hInstance, 
  279.    WORD        wDataSegment, 
  280.    WORD        wHeapSize, 
  281.    LPSTR       lpszCmdLine) 
  282.     WNDCLASS        wc; 
  283.  
  284.     if (hLibInstance) return (TRUE); 
  285.  
  286.     /* define class attributes */ 
  287.     wc.style =            CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; 
  288.     wc.lpfnWndProc =        TimerServer; 
  289.     wc.cbClsExtra =        0; 
  290.     wc.cbWndExtra =        TIMER_EXTRA; 
  291.     wc.hInstance =        hInstance; 
  292.     wc.hIcon =            NULL; 
  293.     wc.hCursor =        LoadCursor(NULL, IDC_ARROW); 
  294.     wc.hbrBackground =    (HBRUSH)(COLOR_WINDOW + 1); 
  295.     wc.lpszMenuName =        NULL; 
  296.     wc.lpszClassName =    TIMER_CLASS; 
  297.  
  298.     if (!RegisterClass(&wc)) return (FALSE); 
  299.      
  300.     /* 
  301.      * all the parameters for the size message class 
  302.      * are the same, except for these three 
  303.      */ 
  304.       
  305.     wc.lpfnWndProc =        SizeMsgServer; 
  306.     wc.cbWndExtra =        SIZEMSG_EXTRA; 
  307.     wc.lpszClassName =     SIZEMSG_CLASS;     
  308.      
  309.     if (!RegisterClass(&wc)) return (FALSE); 
  310.          
  311.     hLibInstance = hInstance; 
  312.     return (TRUE); 
  313.  
  314.  
  315. /* 
  316.  * WEP 
  317.  * 
  318.  * Performs cleanup tasks when the DLL is unloaded.  WEP is 
  319.  * called automatically by Windows when the DLL is unloaded. 
  320.  */ 
  321.  
  322. VOID FAR PASCAL WEP(int bSystemExit) 
  323.     return; 
  324.  
  325.