在windows环境中的应用程序,一般要有一个“关于”对话框,可以用来显示关于应用程序软件、开发者以及“操作系统”的信息。下面向工程中添加System Info组件,实现“”对话框中显示一些系统信息:
在Components and Controls Gallery对话框里选中“System Info for About Dialog”条目,单击Insert按钮,插入System Info组件。如果这时候链接、运行程序(不能通过编译),系统信息显示在“关于”对话框中还未起作用。下面对添加组件后增加的代码进行简单的修改:
BOOL CAboutDlg::OnInitDialog() { CDialog::OnInitDialog(); // CG: This was added by System Info Component. // CG: Following block was added by System Info Component. { CString strFreeDiskSpace; CString strFreeMemory; CString strFmt; // Fill available memory MEMORYSTATUS MemStat; MemStat.dwLength = sizeof(MEMORYSTATUS); GlobalMemoryStatus(&MemStat); strFmt.LoadString(CG_IDS_PHYSICAL_MEM); strFreeMemory.Format(strFmt, MemStat.dwTotalPhys / 1024L); //TODO: Add a static control to your About Box to receive the memory // information. Initialize the control with code like this: SetDlgItemText(IDC_PHYSICAL_MEM, strFreeMemory); // Fill disk free information struct _diskfree_t diskfree; int nDrive = _getdrive(); // use current default drive if (_getdiskfree(nDrive, &diskfree) == 0) { strFmt.LoadString(CG_IDS_DISK_SPACE); strFreeDiskSpace.Format(strFmt, (DWORD)diskfree.avail_clusters * (DWORD)diskfree.sectors_per_cluster * (DWORD)diskfree.bytes_per_sector / (DWORD)1024L, nDrive-1 + _T('A')); } else strFreeDiskSpace.LoadString(CG_IDS_DISK_SPACE_UNAVAIL); //TODO: Add a static control to your About Box to receive the memory // information. Initialize the control with code like this: SetDlgItemText(IDC_DISK_SPACE, strFreeDiskSpace); } return TRUE; // CG: This was added by System Info Component. }
其中MEMORYSTRUCT结构的定义如下:
typedef struct _MEMORYSTATUS {
DWORD dwLength; //sizeof(MEMORYSTRUCT)
DWORD dwMemoryLoad; //percentage of memory in use
SIZE_T dwTotalPhys; //bytes of physical memory
SIZE_T dwAvailPhys; //free physical memory bytes
SIZE_T dwTotalPageFile; //bytes of paging file
SIZE_T dwAvailPageFile; //free bytes of paging file
SIZE_T dwTotalVirtual; //user bytes of address space
SIZE_T dwAvailVirtual; //free user bytes
} MEMORYSTATUS, *LPMEMORYSTATUS;
还要记得向“关于”对话框的模板资源中添加标志号为IDC_PHYSICAL_MEM和IDC_DISK_SPACE的两个静态文本控件。