home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / PMFREMEM.ZIP / FREEMEM.C next >
C/C++ Source or Header  |  1990-08-09  |  3KB  |  132 lines

  1. /*
  2.     -----------------------------------
  3.       FREEMEM.c - Free memory display
  4.     -----------------------------------
  5.             From Programming the OS/2 Presentation Manager
  6.                     by Charles Petzold
  7. */
  8.  
  9. #define INCL_WIN
  10. #define INCL_GPI
  11. #define INCL_DOS
  12. #include <os2.h>
  13. #include <string.h>
  14.  
  15. #define ID_TIMER 1
  16.  
  17. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  18. VOID    SizeTheWindow (HWND) ;
  19.  
  20. int main (void)
  21. {
  22.     static CHAR szClientClass[] = "FreeMem" ;
  23.     static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU |
  24.                                 FCF_BORDER     | FCF_TASKLIST ;
  25.     HAB     hab;
  26.     HMQ     hmq;
  27.     HWND    hwndFrame, hwndClient ;
  28.     QMSG    qmsg;
  29.  
  30.     hab=WinInitialize (0) ;
  31.     hmq = WinCreateMsgQueue (hab, 0) ;
  32.  
  33.     WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0 ) ;
  34.     hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  35.                                     &flFrameFlags, szClientClass, NULL,
  36.                                     0L, NULL, 0, &hwndClient ) ;
  37.     SizeTheWindow (hwndFrame) ;
  38.  
  39.     if (WinStartTimer (hab, hwndClient, ID_TIMER, 1000)) {
  40.         while (WinGetMsg (hab, &qmsg, NULL, 0,0))
  41.             WinDispatchMsg (hab, &qmsg) ;
  42.         WinStopTimer (hab, hwndClient, ID_TIMER) ;
  43.     } else
  44.         WinMessageBox (HWND_DESKTOP, hwndClient,
  45.                         "Too many clocks or timers",
  46.                         szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;
  47.  
  48.     WinDestroyWindow (hwndFrame) ;
  49.     WinDestroyMsgQueue (hmq) ;
  50.     WinTerminate (hab) ;
  51.     return 0;
  52. }
  53. VOID SizeTheWindow (HWND hwndFrame)
  54. {
  55.     static CHAR szText [] = "1,234,567,890 bytes" ;
  56.     HPS         hps;
  57.     POINTL        aptl[TXTBOX_COUNT];
  58.     RECTL        rcl;
  59.  
  60.     hps = WinGetPS (hwndFrame) ;
  61.     GpiQueryTextBox (hps, sizeof szText - 1L, szText, TXTBOX_COUNT,aptl) ;
  62.     WinReleasePS (hps) ;
  63.  
  64.     rcl.yBottom = 0 ;
  65.     rcl.yTop    = 3 * (aptl[TXTBOX_TOPLEFT].y -
  66.                        aptl[TXTBOX_BOTTOMLEFT].y) / 2 ;
  67.     rcl.xLeft    = 0 ;
  68.     rcl.xRight    = (sizeof szText + 1L) * (aptl[TXTBOX_BOTTOMRIGHT].x -
  69.                    aptl[TXTBOX_BOTTOMLEFT].x) / (sizeof szText - 1L) ;
  70.  
  71.     WinCalcFrameRect (hwndFrame, &rcl, FALSE) ;
  72.     WinSetWindowPos (hwndFrame, NULL, (SHORT) rcl.xLeft, (SHORT) rcl.yBottom,
  73.                      (SHORT) (rcl.xRight - rcl.xLeft),
  74.                      (SHORT) (rcl.yTop - rcl.yBottom), SWP_SIZE | SWP_MOVE) ;
  75. }
  76. VOID FormatNumber (CHAR *pchResult, ULONG ulValue)
  77. {
  78.     BOOL    fDisplay = FALSE;
  79.     SHORT    sDigit;
  80.     ULONG    ulQuotient, ulDivisor = 1000000000L ;
  81.  
  82.     for (sDigit = 0 ; sDigit < 10 ; sDigit++) {
  83.         ulQuotient = ulValue / ulDivisor;
  84.  
  85.         if (fDisplay || ulQuotient > 0 || sDigit == 9) {
  86.             fDisplay = TRUE;
  87.  
  88.                *pchResult++ = (CHAR) ('0' + ulQuotient) ;
  89.  
  90.                 if ((sDigit % 3 == 0) && sDigit != 9)
  91.                     *pchResult++ = ',' ;
  92.         }
  93.         ulValue -= ulQuotient * ulDivisor ;
  94.         ulDivisor /= 10;
  95.     }
  96.     *pchResult = '\0' ;
  97. }
  98.  
  99. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  100. {
  101.     static RECTL rcl;
  102.     static ULONG ulFreeMem, ulPrevMem ;
  103.     CHAR         szBuffer [24] ;
  104.     HPS          hps ;
  105.  
  106.     switch (msg) {
  107.         case WM_SIZE:
  108.             WinQueryWindowRect (hwnd, &rcl) ;
  109.             return 0;
  110.         case WM_TIMER:
  111.             DosMemAvail (&ulFreeMem) ;
  112.             if (ulFreeMem != ulPrevMem) {
  113.                 WinInvalidateRect (hwnd, NULL, FALSE) ;
  114.                 ulPrevMem = ulFreeMem;
  115.             }
  116.             return 0;
  117.         case WM_PAINT:
  118.             hps = WinBeginPaint (hwnd, NULL, NULL) ;
  119.  
  120.             FormatNumber (szBuffer, ulFreeMem) ;
  121.             strcat (szBuffer, " bytes") ;
  122.  
  123.             WinDrawText (hps, -1, szBuffer, &rcl,
  124.                          CLR_NEUTRAL, CLR_BACKGROUND,
  125.                          DT_CENTER | DT_VCENTER | DT_ERASERECT) ;
  126.  
  127.             WinEndPaint (hps) ;
  128.             return 0;
  129.     }
  130.     return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  131. }
  132.