home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv2_1 / freemem / freemem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-28  |  4.0 KB  |  142 lines

  1. /*
  2. Microsoft Systems Journal
  3. Volume 2; Issue 1; March, 1987
  4.  
  5. Code Listings For:
  6.  
  7.     FREEMEM
  8.     pp. 33-40
  9.  
  10. Author(s): Charles Petzold
  11. Title:     Keep Track of Your Windows Memory With FREEMEM
  12.  
  13.  
  14.  
  15. ==============================================================================
  16. ==============================================================================
  17. Figure 1: WinMain Function of FREEMEM.C
  18.  
  19. ==============================================================================
  20. */
  21.  
  22. /*  FreeMem.C -- Windows application that displays free memory */
  23.  
  24. #include <windows.h>    /* all Windows functions */
  25. #include <stdlib.h>     /* itoa */
  26. #include <string.h>     /* strcat & strlen */
  27.  
  28. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  29.  
  30. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  31.     HANDLE  hInstance, hPrevInstance ;
  32.     LPSTR   lpszCmdLine ;
  33.     int     nCmdShow ;
  34.     {
  35.     static  char szAppName [] = "FreeMem" ;
  36.     WNDCLASS WndClass ;
  37.     HWND    hWnd ;
  38.     MSG     msg ;
  39.  
  40.     if (hPrevInstance) 
  41.         return FALSE ;
  42.  
  43.     WndClass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  44.     WndClass.hIcon         = NULL ;
  45.     WndClass.cbClsExtra    = 0 ;
  46.     WndClass.cbWndExtra    = 0 ;
  47.     WndClass.lpszMenuName  = NULL ;
  48.     WndClass.lpszClassName = (LPSTR) szAppName ;
  49.     WndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  50.     WndClass.hInstance     = hInstance ;
  51.     WndClass.style         = CS_HREDRAW | CS_VREDRAW;
  52.     WndClass.lpfnWndProc   = WndProc ;
  53.  
  54.     if (!RegisterClass ((LPWNDCLASS) &WndClass))
  55.         return FALSE ;
  56.  
  57.     hWnd = CreateWindow ((LPSTR) szAppName,
  58.             (LPSTR) szAppName,
  59.             WS_TILEDWINDOW,
  60.             0, 0, 0, 0,
  61.             (HWND)   NULL,
  62.  
  63.             (HMENU)  NULL,
  64.             (HANDLE) hInstance,
  65.             (LPSTR)  NULL) ;
  66.  
  67.     if (!SetTimer (hWnd, 1, 1000, NULL))
  68.         return FALSE ;
  69.  
  70.     ShowWindow (hWnd, SHOW_ICONWINDOW) ;
  71.     UpdateWindow (hWnd) ;
  72.  
  73.     while (GetMessage ((LPMSG) &msg, NULL, 0, 0))
  74.         {
  75.         TranslateMessage ((LPMSG) &msg) ;
  76.         DispatchMessage ((LPMSG) &msg) ;
  77.         }
  78.     return (int) msg.wParam ;
  79.     }
  80.  
  81. /*
  82. ==============================================================================
  83. ==============================================================================
  84. Figure 2: WndProc Function of FREEMEM.C
  85.  
  86. ==============================================================================
  87. */
  88.  
  89. long FAR PASCAL WndProc (hWnd, message, wParam, lParam)
  90.     HWND    hWnd ;
  91.     unsigned message ;
  92.     WORD    wParam ;
  93.     LONG    lParam ;
  94.     {
  95.     static  int mem, lastmem ;
  96.     char    buffer [20] ;
  97.     PAINTSTRUCT ps ;
  98.     RECT    rect ;
  99.  
  100.     switch (message)
  101.         {
  102.         case WM_TIMER:
  103.             mem = (int) (GlobalCompact (0L) / 1024) ;
  104.             if (mem != lastmem)
  105.                 InvalidateRect (hWnd, NULL, TRUE) ;
  106.             lastmem = mem ;
  107.             break ;
  108.  
  109.         case WM_PAINT:
  110.             BeginPaint (hWnd, (LPPAINTSTRUCT) &ps) ;
  111.             GetClientRect (hWnd, (LPRECT) &rect) ;
  112.             DrawText (ps.hdc, (LPSTR) buffer,
  113.                 strlen (strcat (itoa (mem, buffer, 10), "K Free")),
  114.                 (LPRECT) &rect, DT_WORDBREAK) ;
  115.             EndPaint (hWnd, (LPPAINTSTRUCT) &ps) ;
  116.             break ;
  117.  
  118.         case WM_DESTROY:
  119.             KillTimer (hWnd, 1) ;
  120.             PostQuitMessage (0) ;
  121.             break ;
  122.  
  123.         default:
  124.             return DefWindowProc (hWnd, message, wParam, lParam) ;
  125.         }
  126.     return (long) 0 ;
  127.     }
  128.  
  129. /*
  130. ==============================================================================
  131. ==============================================================================
  132. Figure 3: Alternate SetTimer Logic
  133.  
  134. ==============================================================================
  135. */
  136.  
  137. if (!SetTimer (hWnd, 1, 1000, NULL)
  138.      {
  139.      MessageBox (hWnd, "Hey! Too many timers!", NULL, MB_OK) ;
  140.      return FALSE ;
  141.      }
  142.