Delphi程序流程(3)(while)(自己构建结构数组写进程管理器 包含动态结构数组的添加)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,TLHelp32, ComCtrls; type TForm1 = class(TForm)
btn1: TButton; lv1: TListView; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation type my32=record //自己定义的结构
A1:string; // 进程名 A2:Integer; //进程id end; {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); var i,x:Integer; kk:LongBool; pe32: tagPROCESSENTRY32; myrecode:array of my32; //自己定义结构数组 newitem:TListItem; begin myrecode:=nil; //清空 结构数组 i:=CreateToolhelp32Snapshot(2,0); pe32.dwSize:=1024; kk:=Process32First(i,pe32); x:=0; while kk=True do begin x:=x+1; SetLength(myrecode,x); //重命名结构上限 myrecode[x-1].A2:=pe32.th32ProcessID; //x-1下标表示是从0下标开始 myrecode[x-1].A1:= pe32.szExeFile; kk:=Process32Next(i,pe32); end; CloseHandle(i); //关闭句柄 i:= Length(myrecode); //取数组个数 //ShowMessage(IntToStr(i)); lv1.Clear; //清空超级列表框 for x:=0 to i-1 do begin newitem:=lv1.Items.Add; newitem.Caption:=myrecode[x].A1; newitem.SubItems.Add(IntToStr(myrecode[x].A2)); end; end; end.