显示 当前CPU 和 内存 占用率

cqy19868403 2011-06-30 06:20:53
我想做个 点击BUTTON1后 LABEL1 显示当前 CPU LABEL2 显示内存 是实时的数据 希望提供 本人比较菜 最好能做好把程序发到我邮箱 我立刻结贴 谢谢

kriscn@qq.com
...全文
139 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
专制的网站 2011-06-30
  • 打赏
  • 举报
回复
路过~~ 正好想找这个~~ 来得早不如来得巧,试试先~~
cqy19868403 2011-06-30
  • 打赏
  • 举报
回复
调用案例有吗 调用不了
SQLDebug_Fan 2011-06-30
  • 打赏
  • 举报
回复
代码都贴给你了:

function GetCPUUsage(var liOldIdleTime, liOldSystemTime: LARGE_INTEGER): string;
{
返回CPU的利用率
}
type
TSystem_Basic_Information = packed record
dwUnknown1: DWORD;
uKeMaximumIncrement: ULONG;
uPageSize: ULONG;
uMmNumberOfPhysicalPages: ULONG;
uMmLowestPhysicalPage: ULONG;
uMmHighestPhysicalPage: ULONG;
uAllocationGranularity: ULONG;
pLowestUserAddress: Pointer;
pMmHighestUserAddress: Pointer;
uKeActiveProcessors: ULONG;
bKeNumberProcessors: byte;
bUnknown2: byte;
wUnknown3: word;
end;

TSystem_Performance_Information = packed record
liIdleTime: LARGE_INTEGER; {LARGE_INTEGER}
dwSpare: array[0..75] of DWORD;
end;

TSystem_Time_Information = packed record
liKeBootTime: LARGE_INTEGER;
liKeSystemTime: LARGE_INTEGER;
liExpTimeZoneBias: LARGE_INTEGER;
uCurrentTimeZoneId: ULONG;
dwReserved: DWORD;
end;

TNtQuerySystemInformation = function(infoClass: DWORD; buffer: Pointer; bufSize: DWORD; var returnSize: Dword): DWORD; stdcall;
var
NtQuerySystemInformation: TNtQuerySystemInformation;
SysBaseInfo: TSystem_Basic_Information;
SysPerfInfo: TSystem_Performance_Information;
SysTimeInfo: TSystem_Time_Information;
status: DWORD; {long}
dbSystemTime: Double;
dbIdleTime: Double;
begin
Result := '<N/A>';

NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'), 'NtQuerySystemInformation');
if @NtQuerySystemInformation = nil then Exit;

if (NtQuerySystemInformation(0, @SysBaseInfo, SizeOf(SysBaseInfo), status) <> 0)
or (NtQuerySystemInformation(3, @SysTimeInfo, SizeOf(SysTimeInfo), status) <> 0)
or (NtQuerySystemInformation(2, @SysPerfInfo, SizeOf(SysPerfInfo), status) <> 0) then Exit;

if liOldIdleTime.QuadPart <> 0 then
begin
// CurrentValue = NewValue - OldValue
dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime := dbIdleTime / dbSystemTime;

// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors{ + 0.5};

// Show Percentage
Result := FormatFloat('0.00 %',dbIdleTime);
end;
liOldIdleTime := SysPerfInfo.liIdleTime;
liOldSystemTime := SysTimeInfo.liKeSystemTime;
end;

function GetSystemMemoryStatus: TMemoryStatus;
{
返回系统内存状态
}
begin
FillChar(Result, SizeOf(Result), 0);
Result.dwLength := SizeOf(Result);
GlobalMemoryStatus(Result);
end;

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧