作者在 2010-01-25 17:52:02 发布以下内容
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,mmsystem, Gauges,TLHelp32;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Timer1: TTimer;
Gauge1: TGauge;
Gauge2: TGauge;
Gauge3: TGauge;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
str:String;
ml:TMemoryStatus;
begin
{const //完整的TMemoryStatus结构
GMEM_DISCARDED = $4000;
GMEM_LOCKCOUNT = 255;
type
PMemoryStatus = ^TMemoryStatus;
_MEMORYSTATUS = record
dwLength: DWORD; //1.数据结构长度
dwMemoryLoad: DWORD; //2.内存使用百分比
dwTotalPhys: DWORD; //3.物理内存总字节数
dwAvailPhys: DWORD; //4.剩余的物理内存字节数
dwTotalPageFile: DWORD; //5.页面文件总字节数
dwAvailPageFile: DWORD; //6.剩余的页文件字节数
dwTotalVirtual: DWORD; //7.虚拟内存的总字节数
dwAvailVirtual: DWORD; //8.剩余的虚拟内存字节数
end;
TMemoryStatus = _MEMORYSTATUS;
MEMORYSTATUS = _MEMORYSTATUS;
}
GlobalMemoryStatus(ml);
str:='数据结构长度:'+IntToStr(ml.dwLength)+#13#10
+'内存使用百分比:'+IntToStr(ml.dwMemoryLoad)+'%'+#13#10
+'实际内存总字节数 :'+FormatFloat('###,###',ml.dwTotalPhys)+#13#10
+'可用的实际内存字节数:'+FormatFloat('###,###',ml.dwAvailPhys)+#13#10
+'分页文件总字节数 :'+FormatFloat('###,###',ml.dwTotalPageFile)+#13#10
+'分页文件可用字节数 :'+FormatFloat('###,###',ml.dwAvailPageFile)+#13#10
+'虚拟内存的总字节数 :'+FormatFloat('###,###',ml.dwTotalVirtual)+#13#10
+'可用的虚拟内存字节数:'+FormatFloat('###,###',ml.dwAvailVirtual)+#13#10;
Memo1.Lines.Clear;
Memo1.Lines.Add(str);
//进度条显示方式
Gauge1.Progress:=Round((1-ml.dwAvailPhys/ml.dwTotalPhys)*100); //
Gauge2.Progress:=Round((1-ml.dwAvailPageFile/ml.dwTotalPageFile)*100);
Gauge3.Progress:=Round((1-ml.dwAvailVirtual/ml.dwTotalVirtual)*100);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Timer1.Enabled:=False; //程序退出时关闭Timer1
end;
procedure TForm1.Button2Click(Sender: TObject);
var lppe: TProcessEntry32;
{ tagPROCESSENTRY32 = packed record
dwSize: DWORD; //指定该结构的大小
cntUsage: DWORD; //进程的参照数
th32ProcessID: DWORD; //当前进程标志符
th32DefaultHeapID: DWORD; //当前进程的默认堆栈标志符
th32ModuleID: DWORD; //当前进程的模块标志符
cntThreads: DWORD; //进程激活的执行线程数
th32ParentProcessID: DWORD; //创建进程的父进程标志符
pcPriClassBase: Longint; //基础优先类
dwFlags: DWORD; //保留类,未使用
szExeFile: array[0..MAX_PATH - 1] of Char;// 进程相关的文件名
end;
}
found : boolean;
Hand : THandle;
begin
ListBox1.Clear;
ListBox1.Items.Add('进程名________进程ID');
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); //创建一个进程快照
found := Process32First(Hand,lppe); //获取第一个进程
while found do
begin
ListBox1.Items.Add(lppe.szExeFile+'__________'+IntToStr(lppe.th32ProcessID));//列出所有进程。
found := Process32Next(Hand,lppe); //不断的获取下一个进程
end;
Caption:='进程总数:'+IntToStr(listBox1.Items.Count-1); //得到进程总数
end;
end.