vmstat.c

来自「一本已经绝版的好书」· C语言 代码 · 共 171 行

C
171
字号
/************************************************************
Module name: VMStat.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include "Resource.H"


/////////////////////////////////////////////////////////////


// The update timer's ID
#define IDT_UPDATE   1


/////////////////////////////////////////////////////////////


// This function accepts a number and converts it to a string,
// inserting commas where appropriate.
LPTSTR WINAPI BigNumToString (LONG lNum, LPTSTR szBuf) {
   WORD wNumDigits = 0, wNumChars = 0;

   do {
      // Put the last digit of the string
      // in the character buffer.
      szBuf[wNumChars++] = (TCHAR) (lNum % 10 + __TEXT('0'));

      // Increment the number of digits
      // that we put in the string.
      wNumDigits++;

      // For every three digits put in
      // the string, add a comma (,).
      if (wNumDigits % 3 == 0)
         szBuf[wNumChars++] = __TEXT(',');

      // Divide the number by 10, and repeat the process.
      lNum /= 10;
      // Continue adding digits to
      // the string until the number is zero.
   } while (lNum != 0);

   // If the last character added to
   // the string was a comma, truncate it.
   if (szBuf[wNumChars - 1] == __TEXT(','))
      szBuf[wNumChars - 1] = 0;

   // Ensure that the string is zero-terminated.
   szBuf[wNumChars] = 0;

   // We added all the characters to the string in reverse 
   // order. We must reverse the contents of the string.
   _tcsrev(szBuf);

   // Returns the address of the string.  This is the same
   // value that was passed to us initially.  Returning it
   // here makes it easier for the calling function
   // to use the string.
   return(szBuf);
}


/////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus,
   LPARAM lParam) {

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_VMSTAT, IDI_VMSTAT);

   // Set a timer so that the information updates periodically.
   SetTimer(hwnd, IDT_UPDATE, 1 * 1000, NULL);

   // Force a timer message for the initial update.
   FORWARD_WM_TIMER(hwnd, IDT_UPDATE, SendMessage);

   return(TRUE);
}


/////////////////////////////////////////////////////////////


void Dlg_OnTimer(HWND hwnd, UINT id) {

   TCHAR szBuf[50];
   MEMORYSTATUS ms;

   // Initialize the structure length before
   // calling GlobalMemoryStatus.
   ms.dwLength = sizeof(ms);
   GlobalMemoryStatus(&ms);

   // Fill the static controls in the
   // list box with the appropriate number.
   SetDlgItemText(hwnd, IDC_MEMLOAD,
      BigNumToString(ms.dwMemoryLoad, szBuf));

   SetDlgItemText(hwnd, IDC_TOTALPHYS,
      BigNumToString(ms.dwTotalPhys, szBuf));

   SetDlgItemText(hwnd, IDC_AVAILPHYS,
      BigNumToString(ms.dwAvailPhys, szBuf));

   SetDlgItemText(hwnd, IDC_TOTALPAGEFILE,
      BigNumToString(ms.dwTotalPageFile, szBuf));

   SetDlgItemText(hwnd, IDC_AVAILPAGEFILE,
      BigNumToString(ms.dwAvailPageFile, szBuf));

   SetDlgItemText(hwnd, IDC_TOTALVIRTUAL,
      BigNumToString(ms.dwTotalVirtual, szBuf));

   SetDlgItemText(hwnd, IDC_AVAILVIRTUAL,
      BigNumToString(ms.dwAvailVirtual, szBuf));
}


/////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl,
   UINT codeNotify) {

   switch (id) {
      case IDCANCEL:
         KillTimer(hwnd, IDT_UPDATE);
         EndDialog(hwnd, id);
         break;
   }
}


/////////////////////////////////////////////////////////////


BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg,
   WPARAM wParam, LPARAM lParam) {

   switch (uMsg) {
      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND,    Dlg_OnCommand);
      chHANDLE_DLGMSG(hwnd, WM_TIMER,      Dlg_OnTimer);
   }
   return(FALSE);
}


/////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();
   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_VMSTAT),
      NULL, Dlg_Proc);

   return(0);
}


//////////////////////// End Of File ////////////////////////

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?