下面的这个代码是判断如果打开了“计算器”程序,就把它关掉 procedure TForm1.Button1Click(Sender: TObject); begin SendMessage(FindWindow(nil,Pchar('计算器')), WM_CLose,0,0); end;
1、FindWindow 2、 uses TlHelp32;procedure TForm1.Button1Click(Sender: TObject); var ippe:tprocessentry32; sshandle:thandle; found:boolean; ttt : boolean; begin sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0); found:=process32first(sshandle,ippe); ttt:=false; while found do begin if ansicomparetext(extractfilename(ippe.szExeFile),'My.exe')=0 then begin ttt:=true; break; end; found:=process32next(sshandle,ippe); sleep(1); end; if not ttt then showmessage('没有找到');end;
findwindows不一定好用, 如果另一个程序也是你写的,你可以在工程文件里写上: hMutex:=CreateMutex(nil,False,'test');然后在程序里判断: Var hMutex:HWND; Ret:Integer;begin hMutex:=CreateMutex(nil,False,'test'); Ret:=GetLastError; If Ret=ERROR_ALREADY_EXISTS Then Begin //已经运行。 End; ReleaseMutex(hMutex); end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(FindWindow(nil,Pchar('计算器')), WM_CLose,0,0);
end;
2、
uses TlHelp32;procedure TForm1.Button1Click(Sender: TObject);
var
ippe:tprocessentry32;
sshandle:thandle;
found:boolean;
ttt : boolean;
begin
sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0);
found:=process32first(sshandle,ippe);
ttt:=false;
while found do
begin
if ansicomparetext(extractfilename(ippe.szExeFile),'My.exe')=0 then
begin
ttt:=true;
break;
end;
found:=process32next(sshandle,ippe);
sleep(1);
end;
if not ttt then
showmessage('没有找到');end;
如果另一个程序也是你写的,你可以在工程文件里写上:
hMutex:=CreateMutex(nil,False,'test');然后在程序里判断:
Var
hMutex:HWND;
Ret:Integer;begin
hMutex:=CreateMutex(nil,False,'test');
Ret:=GetLastError;
If Ret=ERROR_ALREADY_EXISTS Then
Begin
//已经运行。
End;
ReleaseMutex(hMutex);
end;