我是这样写的。
var myMutex:HWND;begin myMutex:=CreateMutex(nil,false,'myCopy');// CreateMutex建立互斥对象,并且给互斥对象起一个唯一的名字。 if WaitForSingleObject(myMutex,0)<>wait_TimeOut then//程序没有被运行过
但程序打开后自动关闭了。
var myMutex:HWND;begin myMutex:=CreateMutex(nil,false,'myCopy');// CreateMutex建立互斥对象,并且给互斥对象起一个唯一的名字。 if WaitForSingleObject(myMutex,0)<>wait_TimeOut then//程序没有被运行过
但程序打开后自动关闭了。
if GetLastError = ERROR_ALREADY_EXISTS then
Application.Terminate;
“ Application.Initialize;” 之前加入如下代码:RvHandle := FindWindow(MYAPPNAME, NIL);
if RvHandle > 0 then
begin
PostMessage(RvHandle, CM_RESTORE, 0, 0);
Exit;
end;RvHandle : hWnd;MYAPPNAME为一个常量,在主界面中定义,是你自定义的文件名!在主界面中自定义一个public过程
procedure CreateParams(var Params: TCreateParams); override;在过程中写入代码:
inherited CreateParams(Params);
Params.WinClassName := MYAPPNAME;
application.title:='HighSoftClient';
tmp:=createmutex(nil,false,'HighSoftClient');
if getlasterror<>error_already_exists then
begin
Application.CreateForm(TMainFrm, MainFrm);
Application.Run;
end;
这样可以使你的程序只运行一次