Code:
// VMStatsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VMStats.h"
#include "VMStatsDlg.h"
#include <winbase.h> // For GlobalMemoryStatus and MEMORYSTATUS
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
void AFXAPI DDX_Text_Custom(CDataExchange* pDX, int nIDC, unsigned long& value);
void AFX_CDECL My_DDX_TextWithFormat(CDataExchange* pDX, int nIDC,
LPCTSTR lpszFormat, UINT nIDPrompt, ...);
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVMStatsDlg dialog
CVMStatsDlg::CVMStatsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVMStatsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVMStatsDlg)
m_AvPage = 0;
m_AvPhys = 0;
m_AvVirt = 0;
m_Percent = 0;
m_TotPage = 0;
m_TotPhys = 0;
m_TotVirt = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_CHIPICON);
m_nReserved = m_nCommitted = m_nUsed = 0;
}
void CVMStatsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
/*
//{{AFX_DATA_MAP(CVMStatsDlg)
DDX_Text(pDX, IDC_AVPAGE, m_AvPage);
DDX_Text(pDX, IDC_AVPHYS, m_AvPhys);
DDX_Text(pDX, IDC_AVVIRT, m_AvVirt);
DDX_Text(pDX, IDC_PERCENT, m_Percent);
DDX_Text(pDX, IDC_TOTPAGE, m_TotPage);
DDX_Text(pDX, IDC_TOTPHYS, m_TotPhys);
DDX_Text(pDX, IDC_TOTVIRT, m_TotVirt);
//}}AFX_DATA_MAP
*/
DDX_Text_Custom(pDX, IDC_AVPAGE, m_AvPage);
DDX_Text_Custom(pDX, IDC_AVPHYS, m_AvPhys);
DDX_Text_Custom(pDX, IDC_AVVIRT, m_AvVirt);
DDX_Text_Custom(pDX, IDC_PERCENT, m_Percent);
DDX_Text_Custom(pDX, IDC_TOTPAGE, m_TotPage);
DDX_Text_Custom(pDX, IDC_TOTPHYS, m_TotPhys);
DDX_Text_Custom(pDX, IDC_TOTVIRT, m_TotVirt);
}
BEGIN_MESSAGE_MAP(CVMStatsDlg, CDialog)
//{{AFX_MSG_MAP(CVMStatsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_RESERVE, OnReserve)
ON_BN_CLICKED(IDC_USE, OnUse)
ON_BN_CLICKED(IDC_RADIODEC, OnRadiodec)
ON_BN_CLICKED(IDC_RADIOHEX, OnRadiohex)
ON_BN_CLICKED(IDC_COMMIT, OnCommit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVMStatsDlg message handlers
BOOL CVMStatsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_DisplayFmtHex = TRUE;
TCHAR szBuf[60];
_stprintf(szBuf, __TEXT("System page size %dKB"), m_PageSize/1024);
::SetDlgItemText(m_hWnd, IDC_PAGESIZE, szBuf);
// SetDlgItemText(IDC_PAGESIZE, szBuf);
_stprintf(szBuf, __TEXT("Virtual memory allocation granularity %dKB"), m_Granularity/1024);
::SetDlgItemText(m_hWnd, IDC_GRANULARITY, szBuf);
// SetDlgItemText(IDC_GRANULARITY, szBuf);
CheckRadioButton(IDC_RADIODEC, IDC_RADIOHEX, IDC_RADIOHEX);
return TRUE; // return TRUE unless you set the focus to a control
}
void CVMStatsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CVMStatsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVMStatsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CVMStatsDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
m_PageSize = SystemInfo.dwPageSize;
m_Granularity = SystemInfo.dwAllocationGranularity;
m_MemoryStatus.dwLength = sizeof(m_MemoryStatus);
GlobalMemoryStatus(&m_MemoryStatus);
m_AvPage = m_MemoryStatus.dwAvailPageFile;
m_AvPhys = m_MemoryStatus.dwAvailPhys;
m_AvVirt = m_MemoryStatus.dwAvailVirtual;
m_Percent = m_MemoryStatus.dwMemoryLoad;
m_TotPage = m_MemoryStatus.dwTotalPageFile;
m_TotPhys = m_MemoryStatus.dwTotalPhys;
m_TotVirt = m_MemoryStatus.dwTotalVirtual;
if((m_TimerID = SetTimer(1, 100, NULL)) == 0){
MessageBox("No system timer available");
}
return 0;
}
void CVMStatsDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if(m_TimerID != 0)
KillTimer(m_TimerID);
}
void CVMStatsDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
BOOL changed = FALSE;
m_MemoryStatus.dwLength = sizeof(m_MemoryStatus);
GlobalMemoryStatus(&m_MemoryStatus);
if(m_AvPage != m_MemoryStatus.dwAvailPageFile){
m_AvPage = m_MemoryStatus.dwAvailPageFile;
changed = TRUE;
}
if(m_AvPhys != m_MemoryStatus.dwAvailPhys){
m_AvPhys = m_MemoryStatus.dwAvailPhys;
changed = TRUE;
}
if(m_AvVirt != m_MemoryStatus.dwAvailVirtual){
m_AvVirt = m_MemoryStatus.dwAvailVirtual;
changed = TRUE;
}
if(m_Percent != m_MemoryStatus.dwMemoryLoad){
m_Percent = m_MemoryStatus.dwMemoryLoad;
changed = TRUE;
}
if(m_TotPage != m_MemoryStatus.dwTotalPageFile){
m_TotPage = m_MemoryStatus.dwTotalPageFile;
changed = TRUE;
}
if(m_TotPhys != m_MemoryStatus.dwTotalPhys){
m_TotPhys = m_MemoryStatus.dwTotalPhys;
changed = TRUE;
}
if(m_TotVirt != m_MemoryStatus.dwTotalVirtual){
m_TotVirt = m_MemoryStatus.dwTotalVirtual;
changed = TRUE;
}
if(changed)UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CVMStatsDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg AboutDlg;
AboutDlg.DoModal();
}
void CVMStatsDlg::OnReserve()
{
// TODO: Add your control notification handler code here
m_DataBlocks[m_nReserved++] = (char *)VirtualAlloc(NULL, 65536, MEM_RESERVE, PAGE_READWRITE);
}
void CVMStatsDlg::OnUse()
{
// TODO: Add your control notification handler code here
if(m_nCommitted > m_nUsed){
char *p = m_DataBlocks[m_nUsed++];
for(int i = 0; i < 65536; i++){
for(int j = 0; j < 256; j++)
*p = j;
p++;
}
}
}
void CVMStatsDlg::OnRadiodec()
{
// TODO: Add your control notification handler code here
m_DisplayFmtHex = FALSE;
}
void CVMStatsDlg::OnRadiohex()
{
// TODO: Add your control notification handler code here
m_DisplayFmtHex = TRUE;
}
void AFXAPI DDX_Text_Custom(CDataExchange* pDX, int nIDC, unsigned long& value)
{
if (pDX->m_bSaveAndValidate)
My_DDX_TextWithFormat(pDX, nIDC, _T("%X"), AFX_IDP_PARSE_INT, &value);
else
if(((CVMStatsDlg *)AfxGetMainWnd())->m_DisplayFmtHex)
My_DDX_TextWithFormat(pDX, nIDC, _T("%X"), AFX_IDP_PARSE_INT, value);
else
My_DDX_TextWithFormat(pDX, nIDC, _T("%d"), AFX_IDP_PARSE_INT, value);
}
void AFX_CDECL My_DDX_TextWithFormat(CDataExchange* pDX, int nIDC,
LPCTSTR lpszFormat, UINT nIDPrompt, ...)
// only supports windows output formats - no floating point
{
va_list pData;
va_start(pData, nIDPrompt);
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
TCHAR szT[32];
if (pDX->m_bSaveAndValidate)
{
/*
// the following works for %d, %u, %ld, %lu
::GetWindowText(hWndCtrl, szT, _countof(szT));
if (!_AfxSimpleScanf(szT, lpszFormat, pData))
{
AfxMessageBox(nIDPrompt);
pDX->Fail(); // throws exception
}
*/
}
else
{
wvsprintf(szT, lpszFormat, pData);
// does not support floating point numbers - see dlgfloat.cpp
SetWindowText(hWndCtrl, szT);
}
va_end(pData);
}
void CVMStatsDlg::OnCommit()
{
// TODO: Add your control notification handler code here
if(m_nReserved > m_nCommitted)
VirtualAlloc(m_DataBlocks[m_nCommitted++], 65536, MEM_COMMIT, PAGE_READWRITE);
}