home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / FREEMEM.ZIP / FREEMEM.C next >
Text File  |  1989-06-07  |  5KB  |  151 lines

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