home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol10n19.zip / WINFSR.C < prev    next >
Text File  |  1991-10-15  |  7KB  |  205 lines

  1.  
  2.  //  WinFSR BETA  by Fran Finnegan (76244,145)
  3.  //  Copyright (c) 1991 Finnegan O'Malley & Company Inc.
  4.  //              All Rights Reserved.
  5.  //  First Published in PC Magazine, November 12, 1991.
  6.  
  7.  #define NOCOMM
  8.  #include <windows.h>
  9.  
  10.  extern DWORD FAR PASCAL GetHeapSpaces(HANDLE hModule);
  11.  
  12.  ///////////////////////////////////////////////////////////////////
  13.  
  14.  extern int       PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  15.  extern long  FAR PASCAL WndProc(HWND, WORD, WORD, DWORD);
  16.  extern int   FAR PASCAL DlgProc(HWND, WORD, WORD, DWORD);
  17.  
  18.  #define FALSE           0
  19.  #define TRUE            1
  20.  
  21.  #define ID_TIMER        1
  22.  
  23.  static  WNDCLASS        mWc;
  24.  static  HWND            mhDlg;
  25.  static  char            mszName[] = "WINFSR";
  26.  
  27.  ///////////////////////////////////////////////////////////////////
  28.  
  29.  extern int       PASCAL WinMain(HANDLE ahInst, HANDLE ahPrevInst,
  30.                                  LPSTR alpsCmdLine, int aiCmdShow)
  31.  {
  32.  auto    FARPROC         aFpDlgProc;
  33.  auto    MSG             aMsg;
  34.  
  35.  static  HWND            shWnd;
  36.  
  37.  //  show any previous instance's dialog
  38.  if (ahPrevInst)
  39.      {
  40.      GetInstanceData(ahPrevInst, (NPSTR)&shWnd, sizeof(shWnd));
  41.      PostMessage(shWnd, WM_QUERYOPEN, 0, 0L);
  42.      return FALSE;
  43.      }
  44.  
  45.  //  register the window class
  46.  mWc.lpfnWndProc   = WndProc;
  47.  mWc.hInstance     = ahInst;
  48.  mWc.hIcon         = LoadIcon(ahInst, mszName);
  49.  mWc.lpszClassName = mszName;
  50.  RegisterClass(&mWc);
  51.  
  52.  //  create and show the window as an icon
  53.  shWnd = CreateWindow(mszName, "System Resources",
  54.          WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  55.          CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, ahInst, NULL);
  56.  ShowWindow(shWnd, SW_SHOWMINIMIZED);
  57.  
  58.  //  create and show the dialog
  59.  aFpDlgProc = MakeProcInstance(DlgProc, ahInst);
  60.  mhDlg = CreateDialog(ahInst, "DIALOG", shWnd, aFpDlgProc);
  61.  
  62.  //  update the icon and dialog every second
  63.  SetTimer(shWnd, ID_TIMER, 1000, NULL);
  64.  
  65.  while (GetMessage(&aMsg, 0, WM_NULL, WM_NULL))
  66.      if (!(mhDlg && IsDialogMessage(mhDlg, &aMsg)))
  67.          {
  68.          TranslateMessage(&aMsg);
  69.          DispatchMessage(&aMsg);
  70.          }
  71.  
  72.  FreeProcInstance(aFpDlgProc);
  73.  return FALSE;
  74.  }
  75.  
  76.  ///////////////////////////////////////////////////////////////////
  77.  
  78.  extern long  FAR PASCAL WndProc(HWND ahWnd, WORD awMsg,
  79.                                  WORD awParam, DWORD alParam)
  80.  {
  81.  auto    DWORD           adwGDI, adwUser;
  82.  auto    WORD            awPctGDI, awPctUser, awPctFree;
  83.  auto    HDC             ahDc;
  84.  auto    int             aiSmCyIcon;
  85.  auto    PAINTSTRUCT     aPs;
  86.  auto    char            aszText[128];
  87.  
  88.  static  WORD            swPctFree = 9999, swWarningTrigger;
  89.  static  BOOL            sbWarned;
  90.  static  int             siNumColors;
  91.  static  RECT            sRt;
  92.  
  93.  switch (awMsg)
  94.      {
  95.      case WM_CREATE:             //  WIN.INI default:  [WinFSR]
  96.      case WM_WININICHANGE:       //                    %=20
  97.          swWarningTrigger = GetProfileInt(mszName, "%", 20) * 100;
  98.          break;
  99.  
  100.      case WM_SIZE:
  101.          //  center the icon and text appropriately
  102.          *(DWORD *)&sRt.left   = 0L;
  103.          *(DWORD *)&sRt.right  = alParam;  //  same as GetClientRect
  104.          aiSmCyIcon = GetSystemMetrics(SM_CYICON);
  105.          InflateRect(&sRt, -((sRt.right  -
  106.                  GetSystemMetrics(SM_CXICON)) / 2 + 1),
  107.                  -((sRt.bottom - aiSmCyIcon)  / 2 + 1));
  108.          sRt.bottom = sRt.top  + aiSmCyIcon   / 2;
  109.          break;
  110.  
  111.      case WM_TIMER:
  112.          //  calculate the Free System Resources percentages
  113.          adwGDI  = GetHeapSpaces(GetModuleHandle("GDI" ));
  114.          adwUser = GetHeapSpaces(GetModuleHandle("USER"));
  115.  
  116.          awPctGDI  = (WORD)(10000L * LOWORD(adwGDI ) /
  117.                                      HIWORD(adwGDI ));
  118.          awPctUser = (WORD)(10000L * LOWORD(adwUser) /
  119.                                      HIWORD(adwUser));
  120.  
  121.          if (swPctFree != (awPctFree = min(awPctGDI, awPctUser)))
  122.              {
  123.              wsprintf(aszText,
  124.                      "Free System Resources... %u%%\r\n"
  125.                      "GDI: %u/%u=%u%%    USER: %u/%u=%u%%",
  126.                      (swPctFree = awPctFree) / 100, adwGDI,
  127.                      awPctGDI / 100, adwUser, awPctUser / 100);
  128.              SetDlgItemText(mhDlg, 2, aszText);
  129.              if (swWarningTrigger < swPctFree)   //  high FSRs
  130.                  sbWarned  = FALSE;
  131.              else                                //  low FSRs
  132.                  if (sbWarned == FALSE)
  133.                      {
  134.                      sbWarned  = TRUE ;  //  show the dialog
  135.                      PostMessage(ahWnd, WM_QUERYOPEN, 0, 0L);
  136.                      }
  137.              InvalidateRect(ahWnd, &sRt, FALSE);
  138.              }
  139.          break;
  140.  
  141.      case WM_PAINTICON:
  142.          if (ahDc = BeginPaint(ahWnd, &aPs))
  143.              {
  144.              DrawIcon(ahDc, sRt.left - 1, sRt.top - 1, mWc.hIcon);
  145.              if (siNumColors == 0)
  146.                  siNumColors  = GetDeviceCaps(ahDc, NUMCOLORS);
  147.              if (siNumColors >= 8)       //  check for colors
  148.                  SetBkMode(ahDc, TRANSPARENT);
  149.              DrawText(ahDc,   aszText,   //  paint over icon
  150.                      wsprintf(aszText, "%u%%", swPctFree / 100),
  151.                      &sRt, DT_CENTER | DT_NOCLIP | DT_NOPREFIX);
  152.              EndPaint(ahWnd, &aPs);
  153.              }
  154.          break;
  155.  
  156.      case WM_QUERYOPEN:
  157.          ShowWindow(mhDlg, SW_SHOW);     //  show the dialog
  158.          BringWindowToTop(mhDlg);
  159.          break;
  160.  
  161.      case WM_CLOSE:
  162.          KillTimer(ahWnd, ID_TIMER);
  163.          DestroyWindow(mhDlg);
  164.          mhDlg = 0;
  165.          DestroyWindow(ahWnd);
  166.          break;
  167.  
  168.      case WM_DESTROY:
  169.          PostQuitMessage(FALSE);
  170.          break;
  171.  
  172.      default:
  173.          return DefWindowProc(ahWnd, awMsg, awParam, alParam);
  174.      }
  175.  return FALSE;
  176.  }
  177.  
  178.  ///////////////////////////////////////////////////////////////////
  179.  
  180.  extern int   FAR PASCAL DlgProc(HWND ahDlg, WORD awMsg,
  181.                                  WORD awParam, DWORD alParam)
  182.  {
  183.  auto    RECT            aRt;
  184.  
  185.  switch (awMsg)
  186.      {
  187.      case WM_INITDIALOG:
  188.          //  center dialog box
  189.          GetWindowRect(ahDlg, &aRt);
  190.          OffsetRect(&aRt, -aRt.left  , -aRt.top   );
  191.          MoveWindow(ahDlg,   //  correct way to center a dialog:
  192.                  ((GetSystemMetrics(SM_CXSCREEN) - aRt.right ) / 2 +
  193.                                                             4) & ~7,
  194.                   (GetSystemMetrics(SM_CYSCREEN) - aRt.bottom) / 2,
  195.                  aRt.right , aRt.bottom, FALSE);
  196.          return TRUE;    //  did process the message
  197.  
  198.      case WM_COMMAND:
  199.          ShowWindow(ahDlg, SW_HIDE);     //  hide the dialog
  200.          return TRUE;    //  did process the message
  201.      }
  202.  return FALSE;   //  did not process the message
  203.  }
  204.  
  205.