File:  [OS/2 SDKs] / pmsdk / samples / petzold / chap07 / freemem.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:28:12 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: pmsdk-1988, HEAD
Microsoft OS/2 SDK PM 08-08-1988

/*----------------------------------
   FREEMEM.C -- Free Memory Display 
  ----------------------------------*/

#define INCL_WIN
#define INCL_GPI
#define INCL_DOS

#include <os2.h>
#include <stddef.h>

#define ID_TIMER 1

MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
VOID    SizeTheWindow (HWND) ;

int main (void)
     {
     static CHAR szClientClass[] = "FreeMem" ;
     HAB         hab ;
     HMQ         hmq ;
     HWND        hwndClient, hwndFrame ;
     QMSG        qmsg ;
     ULONG       flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU ;
     ULONG       flFrameStyle = FS_BORDER ;

     hab = WinInitialize (0) ;
     hmq = WinCreateMsgQueue (hab, 0) ;

     WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0) ;

     hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                                     &flFrameFlags, szClientClass,
                                     szClientClass,
				     0L, NULL, 0, &hwndClient) ;

     SizeTheWindow (hwndFrame) ;
     WinShowWindow (hwndFrame, TRUE) ;

     if (WinStartTimer (hab, hwndClient, ID_TIMER, 1000))
          {
          while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
               WinDispatchMsg (hab, &qmsg) ;

          WinStopTimer (hab, hwndClient, ID_TIMER) ;
          }
     else
          WinMessageBox (HWND_DESKTOP, hwndClient,
                         "Too many clocks or timers",
                         szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;

     WinDestroyWindow (hwndFrame) ;
     WinDestroyMsgQueue (hmq) ;
     WinTerminate (hab) ;

     return 0 ;
     }

VOID SizeTheWindow (HWND hwndFrame)
     {
     FONTMETRICS fm ;
     HPS         hps ;
     RECTL       rcl ;

     hps = WinGetPS (hwndFrame) ;
     GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
     WinReleasePS (hps) ;

     rcl.yBottom = 0 ;
     rcl.yTop    = 3 * fm.lMaxBaselineExt / 2 ;
     rcl.xLeft   = 0 ;
     rcl.xRight  = 14 * fm.lAveCharWidth ;

     WinCalcFrameRect (hwndFrame, &rcl, FALSE) ;

     WinSetWindowPos (hwndFrame, NULL, (SHORT) rcl.xLeft, (SHORT) rcl.yBottom,
                      (SHORT) (rcl.xRight - rcl.xLeft),
                      (SHORT) (rcl.yTop - rcl.yBottom), SWP_SIZE | SWP_MOVE) ;
     }

VOID FormatNumber (ULONG ulValue, CHAR *szResult)
     {
     BOOL  fDisplay = FALSE ;
     SHORT sDigit ;
     ULONG ulQuotient, ulDivisor = 1000000000L ;

     for (sDigit = 0 ; sDigit < 10 ; sDigit++)
          {
          ulQuotient = ulValue / ulDivisor ;

          if (fDisplay || ulQuotient > 0 || sDigit == 9)
               {
               fDisplay = TRUE ;

               *szResult++ = (CHAR) ('0' + ulQuotient) ;

               if ((sDigit % 3 == 0) && sDigit != 9)
                    *szResult++ = ',' ;
               }
          ulValue -= ulQuotient * ulDivisor ;
          ulDivisor /= 10 ;
          }
     *szResult = '\0' ;
     }

MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
     {
     static RECTL rcl ;
     static ULONG ulFreeMem, ulPrevMem ;
     CHAR         szBuffer [16] ;
     HPS          hps;

     switch (msg)
          {
          case WM_SIZE:
               WinQueryWindowRect (hwnd, &rcl) ;
               return 0 ;               

          case WM_TIMER:
               DosMemAvail (&ulFreeMem) ;

               if (ulFreeMem != ulPrevMem)
                    {
                    WinInvalidateRect (hwnd, NULL, FALSE) ;
                    ulPrevMem = ulFreeMem ;
                    }
               return 0 ;

          case WM_PAINT:
               hps = WinBeginPaint (hwnd, NULL, NULL) ;

               FormatNumber (ulFreeMem, szBuffer) ;

               WinDrawText (hps, -1, szBuffer, &rcl, 
                            CLR_NEUTRAL, CLR_BACKGROUND,
                            DT_CENTER | DT_VCENTER | DT_ERASERECT) ;

               WinEndPaint (hps) ;
               return 0 ;
          }
     return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
     }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.