program xxxxx; ... var MyHandle: THandle; begin MyHandle := CreateMutex(nil, False, 'xxxxx'); if WaitForSingleObject(MyHandle, 0) <> wait_TimeOut then begin Application.Initialize; Application.CreateForm(TFrmMain, FrmMain); Application.Run; end; end.
先定义一常量 const max_app=1; //控制运行一次,如要二次就设max_app=2; 代码如下: procedure TFlogon.FormCreate(Sender: TObject); var sem:integer; win:hwnd; bre:integer; begin Application.CreateForm(TDM1, DM1); sem:=createsemaphore(nil,0,max_app,'only'); if sem=0 then exit; if not releasesemaphore(sem,1,nil) then begin application.MessageBox(pchar('程序正在运行中。'),'提示信息',MB_ICONinformation+mb_ok); application.Terminate; end; end; 供参考。
procedure onlyone; var find:hwnd; title:string; p:array[0..255] of char; begin //防止程序运行多个事例 title:=application.Title; strpcopy(p,title); application.Title:='temp'; find:=findwindow(nil,p); if find<>0 then application.Terminate else application.Title:=title; end; //如假保换!!
在程序的项目文件(*.dpr)中加入代码如下: var HParentMain:HWND;begin HParentMain:=findwindow(nil,'业务管理');//如果的你的程序的标题是业务管理 if HParentMain<>0 then begin Application.MessageBox('程序已经运行!','业务管理',MB_ICONWARNING); PostMessage(HParentMain,WM_ACTIVATE,0,0); end else begin Application.Initialize; Application.Title := '业务管理'; Application.CreateForm(TDM, DM); Application.CreateForm(TFrmMain, FrmMain); Application.Run; end; end.
...
var
MyHandle: THandle;
begin
MyHandle := CreateMutex(nil, False, 'xxxxx');
if WaitForSingleObject(MyHandle, 0) <> wait_TimeOut then
begin
Application.Initialize;
Application.CreateForm(TFrmMain, FrmMain);
Application.Run;
end;
end.
const
max_app=1; //控制运行一次,如要二次就设max_app=2;
代码如下:
procedure TFlogon.FormCreate(Sender: TObject);
var
sem:integer;
win:hwnd;
bre:integer;
begin
Application.CreateForm(TDM1, DM1);
sem:=createsemaphore(nil,0,max_app,'only');
if sem=0 then
exit;
if not releasesemaphore(sem,1,nil) then
begin
application.MessageBox(pchar('程序正在运行中。'),'提示信息',MB_ICONinformation+mb_ok);
application.Terminate;
end;
end;
供参考。
var
find:hwnd;
title:string;
p:array[0..255] of char;
begin
//防止程序运行多个事例
title:=application.Title;
strpcopy(p,title);
application.Title:='temp';
find:=findwindow(nil,p);
if find<>0 then
application.Terminate else
application.Title:=title;
end;
//如假保换!!
var
HParentMain:HWND;begin HParentMain:=findwindow(nil,'业务管理');//如果的你的程序的标题是业务管理
if HParentMain<>0 then begin
Application.MessageBox('程序已经运行!','业务管理',MB_ICONWARNING);
PostMessage(HParentMain,WM_ACTIVATE,0,0);
end
else begin
Application.Initialize;
Application.Title := '业务管理'; Application.CreateForm(TDM, DM);
Application.CreateForm(TFrmMain, FrmMain);
Application.Run;
end;
end.