不用findwindow也可以用下面代码实现procedure TForm1.Button1Click(Sender: TObject); var FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32; Ret: BOOL; ProcessID: integer; s: string; begin FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := SizeOf(FProcessEntry32); Ret := Process32First(FSnapshotHandle, FProcessEntry32); while Ret do begin s := UpperCase(ExtractFileName(FProcessEntry32.szExeFile)); if (s = '你的文件名.EXE') then begin //这是终止程序执行,当然可以修改成删除你的表 ProcessID := FProcessEntry32.th32ProcessID; TerminateProcess(OpenProcess(PROCESS_TERMINATE, false, ProcessID), 1); s := ''; end; Ret := Process32Next(FSnapshotHandle, FProcessEntry32); end;end;
改造一下,就成了 procedure TForm1.Timer1Timer(Sender: TObject); var FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32; Ret: BOOL; s: string; i: integer; begin i := 0; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := SizeOf(FProcessEntry32); Ret := Process32First(FSnapshotHandle, FProcessEntry32); while Ret do begin s := ExtractFileName(FProcessEntry32.szExeFile); if s = '你的文件名.exe' then begin inc(i); end; Ret := Process32Next(FSnapshotHandle, FProcessEntry32); end; if i = 0 then begin // 删除表 end; end;
var HWndCalc: HWnd; begin HWndCalc := FindWindow(nil, '程序的标题caption'); if HWndCalc = 0 then //删除表; end;
var
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
Ret: BOOL;
ProcessID: integer;
s: string;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
Ret := Process32First(FSnapshotHandle, FProcessEntry32);
while Ret do
begin
s := UpperCase(ExtractFileName(FProcessEntry32.szExeFile));
if (s = '你的文件名.EXE') then
begin
//这是终止程序执行,当然可以修改成删除你的表
ProcessID := FProcessEntry32.th32ProcessID;
TerminateProcess(OpenProcess(PROCESS_TERMINATE, false, ProcessID), 1);
s := '';
end;
Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
end;end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
Ret: BOOL;
s: string;
i: integer;
begin
i := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
Ret := Process32First(FSnapshotHandle, FProcessEntry32); while Ret do
begin
s := ExtractFileName(FProcessEntry32.szExeFile);
if s = '你的文件名.exe' then
begin
inc(i);
end;
Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
if i = 0 then
begin
// 删除表
end;
end;
HWndCalc: HWnd;
begin
HWndCalc := FindWindow(nil, '程序的标题caption');
if HWndCalc = 0 then
//删除表;
end;
程序每次开的时候都检查一次