home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap05 / freemem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.5 KB  |  118 lines

  1. /*------------------------------------------
  2.    FREEMEM.C -- Free Memory Display Program
  3.                 (c) Charles Petzold, 1990
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #define ID_TIMER    1
  9.  
  10. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  11.  
  12. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  13.                     LPSTR lpszCmdLine, int nCmdShow)
  14.      {
  15.      static char szAppName[] = "FreeMem" ;
  16.      HDC         hdc ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      TEXTMETRIC  tm ;
  20.      WNDCLASS    wndclass ;
  21.  
  22.      if (hPrevInstance) 
  23.           return FALSE ;
  24.  
  25.      wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  26.      wndclass.lpfnWndProc   = WndProc ;
  27.      wndclass.cbClsExtra    = 0 ;
  28.      wndclass.cbWndExtra    = 0 ;
  29.      wndclass.hInstance     = hInstance ;
  30.      wndclass.hIcon         = NULL ;
  31.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  32.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  33.      wndclass.lpszMenuName  = NULL ;
  34.      wndclass.lpszClassName = szAppName ;
  35.  
  36.      !RegisterClass (&wndclass) ;
  37.  
  38.      hwnd = CreateWindow (szAppName, "Free Memory",
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      hdc = GetDC (hwnd) ;
  45.      GetTextMetrics (hdc, &tm) ;
  46.      ReleaseDC (hwnd, hdc) ;
  47.  
  48.      if (4 * tm.tmAveCharWidth > GetSystemMetrics (SM_CXICON) ||
  49.                2 * tm.tmHeight > GetSystemMetrics (SM_CYICON))
  50.           {
  51.           MessageBox (hwnd, "Icon size too small for display!",
  52.                       szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  53.           return FALSE ;
  54.           }
  55.  
  56.      if (!SetTimer (hwnd, ID_TIMER, 1000, NULL))
  57.           {
  58.           MessageBox (hwnd, "Too many clocks or timers!",
  59.                       szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  60.           return FALSE ;
  61.           }
  62.  
  63.      ShowWindow (hwnd, SW_SHOWMINNOACTIVE) ;
  64.      UpdateWindow (hwnd) ;
  65.  
  66.      while (GetMessage (&msg, NULL, 0, 0))
  67.           {
  68.           TranslateMessage (&msg) ;
  69.           DispatchMessage (&msg) ;
  70.           }
  71.      return msg.wParam ;
  72.      }
  73.  
  74. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  75.      {
  76.      static DWORD  dwFreeMem, dwPrevMem ;
  77.      static RECT   rect ;
  78.      char          cBuffer [20] ;
  79.      HDC           hdc ;
  80.      PAINTSTRUCT   ps ;
  81.  
  82.      switch (message)
  83.           {
  84.           case WM_TIMER:
  85.                dwFreeMem = GetFreeSpace (0) ;
  86.  
  87.                if (dwFreeMem != dwPrevMem)
  88.                     InvalidateRect (hwnd, NULL, TRUE) ;
  89.  
  90.                dwPrevMem = dwFreeMem ;
  91.                return 0 ;
  92.  
  93.           case WM_SIZE:
  94.                GetClientRect (hwnd, &rect) ;
  95.                return 0 ;
  96.  
  97.           case WM_PAINT:
  98.                hdc = BeginPaint (hwnd, &ps) ;
  99.  
  100.                DrawText (hdc, cBuffer,
  101.                          sprintf (cBuffer, "%.2f megs",
  102.                                   dwFreeMem / 1024.0 / 1024.0),
  103.                          &rect, DT_WORDBREAK) ;
  104.  
  105.                EndPaint (hwnd, &ps) ;
  106.                return 0 ;
  107.  
  108.           case WM_QUERYOPEN:
  109.                return 0 ;
  110.  
  111.           case WM_DESTROY:
  112.                KillTimer (hwnd, ID_TIMER) ;
  113.                PostQuitMessage (0) ;
  114.                return 0 ;
  115.           }
  116.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  117.      }
  118.