比如procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(handle,'open',‘c:\bz.exe','nil','nil',SW_SHOWNORMAL);
end;
现在希望在关闭FORM1时,或关闭FORM1所在的工程时,C:\BZ.EXE 能够自动关闭。
begin
ShellExecute(handle,'open',‘c:\bz.exe','nil','nil',SW_SHOWNORMAL);
end;
现在希望在关闭FORM1时,或关闭FORM1所在的工程时,C:\BZ.EXE 能够自动关闭。
Var
Hd,Hs:THandle;
dExit:Cardinal;
Tmp,Tmp1:String;
Lp:TProcessEntry32;
begin
Result:=false;
Lp.dwSize:=sizeof(TProcessEntry32);
Hd:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if Process32First(Hd,Lp) then
Repeat
Tmp:=UpperCase(Trim(Name));
Tmp1:=Trim(UpperCase(Lp.szExeFile));
if AnsiPos(Tmp,Tmp1)>0 then
begin
Hs:=OpenProcess(PROCESS_ALL_ACCESS,true,Lp.th32ProcessID);
Result:=GetExitCodeProcess(Hs,dExit);
if Result then
begin
Result:=TerminateProcess(Hs,0);
end;
break;
end
Until Process32Next(Hd,Lp)=False;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shellapi;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
handle:hwnd;
begin
handle:=findwindow(nil,'bz'); //这步:你先建一个窗体标题为bz的应用程序
if handle>0 then sendmessage(handle,wm_close,0,0);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShellExecute(handle,'open','c:\windows\desktop\bz.exe','nil','nil',SW_SHOWNORMAL);
end;end.
这句代码不目的是找到要关闭程序的句柄
如果那个程序不是楼主写的
就要手动找类名或窗体标题了
然后再做相应的处理OVER GAME!!:)
http://expert.csdn.net/Expert/topic/2004/2004608.xml?temp=.6706659