Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14523825
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 21:27:49

下载本文示例代码
  下载本文源代码    程序思想与要点:   1)、本程序分两种情况来获取CPU的利用率,NT下利用ntdll.dll中没有公开的API: NtQuerySystemInformation, 9x下利用注册表来获取CPU的利用率  code:NT typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);PROCNTQSI NtQuerySystemInformation;NtQuerySystemInformation = (PROCNTQSI)GetProcAddress( GetModuleHandle("ntdll"), "NtQuerySystemInformation");if (!NtQuerySystemInformation){ return;}// get number of processors in the systemstatus = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);if (status != NO_ERROR){ return;}status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);if (status!=NO_ERROR){ return;}// get new CPU''s idle timestatus = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);if (status != NO_ERROR){ return;}// if it''s a first call - skip itif (m_liOldIdleTime.QuadPart != 0){ // CurrentValue = NewValue - OldValue dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(m_liOldIdleTime); dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(m_liOldSystemTime); // CurrentCpuIdle = IdleTime / SystemTime dbIdleTime = dbIdleTime / dbSystemTime; // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors 0.5; m_fNewUsges = (UINT)dbIdleTime;}  2)、 通过 GlobalMemoryStatus来获取内存的使用情况  code: MEMORYSTATUS MemStat;MemStat.dwLength = sizeof(MEMORYSTATUS);GlobalMemoryStatus(&MemStat);m_ulNewUsges = MemStat.dwMemoryLoad;  3)、 程序中封装了两个类 CcpuUsgesCtl和CmemUsgesCtl,使用这两这个类可以实现CPU,内存利用率的定时读取,并以图形化的形式显示出来   使用   有两种使用方法,不过要先把这个类的文件 add to project   1).声明一个这两类的对象,并用create来动态生成 such as:CMemUsgesCtl m_ MyMemCtrl;CCpuUsgesCtl m_ MyCpuCtrl;…………if(!m_MyCpuCtrl.Create(WS_CHILD | WS_VISIBLE, rect, this, IDC_CPUCTL)){ TRACE0("Create m_MyCtrl Failed!"); return 0;} rect.left = rect.right 20; rect.right = lpCreateStruct->cx / 2; if(!m_MyMemCtrl.Create(WS_CHILD | WS_VISIBLE,rect, this, IDC_MEMCTL)) {  TRACE0("Create m_MyCtrl Failed!");  return 0; }  2) 插入一个static控件,并从类向导中为其生成control形变量,最后将变量的类型换为我们就行了   效果图:   结束语  控件利用内存来绘图,实现了无闪烁显示,欢迎指教!   下载本文源代码    程序思想与要点:   1)、本程序分两种情况来获取CPU的利用率,NT下利用ntdll.dll中没有公开的API: NtQuerySystemInformation, 9x下利用注册表来获取CPU的利用率  code:NT typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);PROCNTQSI NtQuerySystemInformation;NtQuerySystemInformation = (PROCNTQSI)GetProcAddress( GetModuleHandle("ntdll"), "NtQuerySystemInformation");if (!NtQuerySystemInformation){ return;}// get number of processors in the systemstatus = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);if (status != NO_ERROR){ return;}status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);if (status!=NO_ERROR){ return;}// get new CPU''s idle timestatus = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);if (status != NO_ERROR){ return;}// if it''s a first call - skip itif (m_liOldIdleTime.QuadPart != 0){ // CurrentValue = NewValue - OldValue dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(m_liOldIdleTime); dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(m_liOldSystemTime); // CurrentCpuIdle = IdleTime / SystemTime dbIdleTime = dbIdleTime / dbSystemTime; // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors 0.5; m_fNewUsges = (UINT)dbIdleTime;}  2)、 通过 GlobalMemoryStatus来获取内存的使用情况  code: MEMORYSTATUS MemStat;MemStat.dwLength = sizeof(MEMORYSTATUS);GlobalMemoryStatus(&MemStat);m_ulNewUsges = MemStat.dwMemoryLoad;  3)、 程序中封装了两个类 CcpuUsgesCtl和CmemUsgesCtl,使用这两这个类可以实现CPU,内存利用率的定时读取,并以图形化的形式显示出来   使用   有两种使用方法,不过要先把这个类的文件 add to project   1).声明一个这两类的对象,并用create来动态生成 such as:CMemUsgesCtl m_ MyMemCtrl;CCpuUsgesCtl m_ MyCpuCtrl;…………if(!m_MyCpuCtrl.Create(WS_CHILD | WS_VISIBLE, rect, this, IDC_CPUCTL)){ TRACE0("Create m_MyCtrl Failed!"); return 0;} rect.left = rect.right 20; rect.right = lpCreateStruct->cx / 2; if(!m_MyMemCtrl.Create(WS_CHILD | WS_VISIBLE,rect, this, IDC_MEMCTL)) {  TRACE0("Create m_MyCtrl Failed!");  return 0; }  2) 插入一个static控件,并从类向导中为其生成control形变量,最后将变量的类型换为我们就行了   效果图:   结束语  控件利用内存来绘图,实现了无闪烁显示,欢迎指教! 下载本文示例代码


VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序VC 设计图形显示CPU内存利用率程序
阅读(176) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~