先看下面两端代码
procedure TForm1.BitBtn1Click(Sender: TObject);
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
if found = True then BitBtn1.Caption := IntToStr(i);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
//if found = True then BitBtn1.Caption := IntToStr(i);
end;
end;
大家看清楚了吗,区别就是那就注释,第一个ListBox1是有数据的,然而第二个ListBox1没有数据,那段注释难道有什么蹊跷吗?
procedure TForm1.BitBtn1Click(Sender: TObject);
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
if found = True then BitBtn1.Caption := IntToStr(i);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
//if found = True then BitBtn1.Caption := IntToStr(i);
end;
end;
大家看清楚了吗,区别就是那就注释,第一个ListBox1是有数据的,然而第二个ListBox1没有数据,那段注释难道有什么蹊跷吗?
你把//if found = True then BitBtn1.Caption := IntToStr(i);改成application.ProcessMessages;应该也可以
而运行第二段代码,ListBox1没有一个数据项,是空的。
这是为什么啊,难道被我注释的那段代码,起很重要的作用吗,也不见得啊,你说是吧。
var
FProcessEntry32: TProcessEntry32;
PID: integer;
List: TStringList;
ModuleListHandle: Thandle;
ModuleStruct: TMODULEENTRY32;
J: integer;
Yn: boolean;
begin
if Combobox1.itemindex = -1 then exit;
List := TStringList.Create;
FProcessEntry32 := GetProcessID(List, Combobox1.text);
PID := FProcessEntry32.th32ProcessID;
ModuleListHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pID);
ListBox1.Items.Clear;
ModuleStruct.dwSize := sizeof(ModuleStruct);
yn := Module32First(ModuleListHandle, ModuleStruct);
j := 0;
while (yn) do
begin
SetLength(ModuleArray, j + 1);
ModuleArray[j] := ModuleStruct;
ListBox1.Items.Add(ModuleStruct.szExePath);
yn := Module32Next(ModuleListHandle, ModuleStruct);
J := j + 1;
end;
CloseHandle(ModuleListHandle);
procedure TForm1.BitBtn4Click(Sender: TObject);
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
//if found = True then BitBtn1.Caption := IntToStr(i);
end;
end;
楼主,我就用你自己的代码也没有发现问题啊.
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
if found = false then Button1.Caption := IntToStr(i);
end;
end;这样也没有问题.看样子是TBitBtn的问题.
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
//if found = True then BitBtn1.Caption := IntToStr(i);
end;
ShowMessage(IntToStr(ListBox1.Items.Count));结果提示是0
TBitBtn,为什么会导致这个情况啊?
procedure TForm1.BitBtn1Click(Sender: TObject);
var
lppe: tprocessentry32;
sshandle: thandle;
found: boolean;
i,k: integer;
begin
i := 0;
sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
found := process32first(sshandle, lppe);
while found do
begin
inc(i);
ListBox1.Items.Add(lppe.szExefile);
found := Process32Next(sshandle, lppe);
//if found = True then BitBtn1.Caption := IntToStr(i);这里IF判断是多余的吧?
k:= k;加上这个也是可以的。
end;
end;