program Project1;
uses
Forms, Windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
var Mutex: THandle;
procedure CheckPrevInst;
var
PrevWnd: HWnd;
begin
Mutex:=CreateMutex(NIL, False, 'SingleInstanceProgramMutex');
if WaitForSingleObject(Mutex, 10000)=WAIT_TIMEOUT then Halt;
PrevWnd:=FindWindow('TForm1', 'Form1');//主窗体的类名和标题
if PrevWnd<>0 then PrevWnd:=GetWindow(PrevWnd, GW_OWNER);
if PrevWnd<>0 then begin
if IsIconic(PrevWnd) then
ShowWindow(PrevWnd, SW_SHOWNORMAL)
else
SetForegroundWindow(PrevWnd);
Halt;
end;
end;
begin
Application.Initialize;
try
CheckPrevInst;
Application.CreateForm(TForm1, Form1);
finally
Form1.HandleNeeded;
ReleaseMutex(Mutex);
CloseHandle(Mutex);
end;
Application.Run;
end.