program xxx;....var OnlyRunOnce : THandle;begin OnlyRunOnce := CreateMutex(nil, True, 'XXX_CENTER'); if GetLastError = 183 then Halt; Application.Initialize; Application.Title := 'XXXXXX系统'; Application.CreateForm(TMainForm, MainForm); Application.Run; CloseHandle(OnlyRunOnce); end.
1.对主窗口程序的改动: 在主窗口(即程序创建的第一个窗口)中interface节加入 const CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息} MYAPPNAME = "My Delphi Program"; 并在Form的定义的public节中加入 procedure CreateParams(var Params: TCreateParams); override; Procedure RestoreRequest(var message: TMessage); message CM_RESTORE; 在implementation节中加入 {指定窗口名称} procedure TForm1.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); Params.WinClassName := MYAPPNAME; end; {处理“恢复”消息} procedure TForm1.RestoreRequest(var message: TMessage); begin if IsIconic(Application.Handle) = TRUE then Application.Restore else Application.BringToFront; end; 经过以上修改,程序的主窗口的类名已经被指定了,这是进行判断的基础。一般在程 序刚开始运 行的时候进行判断,所以还要对DPR文件进行修改。 2、对DPR文件的改动 在 uses 节中添加 windows、messages这两个单元加入下列语句,注意两个文件中常 量 CM_RESTORE和MYAPPNAME的定义必须一致 const CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息} MYAPPNAME = "My Delphi Program"; var RvHandle : hWnd; 将下列语句插到程序最前部(在Application.Initialize之前) RvHandle := FindWindow(MYAPPNAME, NIL); if RvHandle > 0 then begin PostMessage(RvHandle, CM_RESTORE, 0, 0); Exit; end; 这段程序的意思是如果找到一个类名相同的窗口,则向该窗口发送一个消息,并退 出,而本例中 原窗口收到该消息后会自动激活或从图标还原,从而达到了避免二次运行.
program Project1;uses windows,//必须加入 Forms, Unit2 in 'Unit2.pas' {Form2}, Unit1 in 'Unit1.pas' {Form1}, Unit3 in 'Unit3.pas' {Form3}, Unit4 in 'Unit4.pas' {Form4}, Unit5 in 'Unit5.pas' {Form5}, Unit6 in 'Unit6.pas' {Form_about};{$R *.res}const classname='tform2';//form2为主窗体 var handle:integer; begin handle:= findwindow(classname,nil); if handle<>0 then begin messagebox(0,'此程序已经在运行!','提示',0); halt; end else begin Application.Initialize; Application.CreateForm(TForm2, Form2); Application.Run; end; end.
在*.DPR中加上这样一段: CreateMutex(nil, True, 'Pro'); if GetLastError = ERROR_ALREADY_EXISTS then begin MessageBox(0, '程序已经在运行中......', '提醒' ,MB_ICONINFORMATION); Halt; end; Application.Initialize; ……
这是我的程序:供你参考,修改工程文件 program DSDATA;uses Forms, Windows, MainF in 'MainF.pas' {MainForm}, DayRep in 'DayRep.pas' {FrmReport1}, MonthRep in 'MonthRep.pas' {FrmReport2}, YearRep in 'YearRep.pas' {FrmReport3}, About in 'About.pas' {AboutBox}, uCurveLine in 'uCurveLine.pas' {FrmCurveLine};{$R *.RES} Var hMutex:HWND; Ret:Integer; begin Application.Initialize; hMutex:=CreateMutex(nil,False,'灌区数据库自动处理系统'); Application.Title := '灌区数据库自动处理系统'; try Ret:=GetLastError; If Ret<>ERROR_ALREADY_EXISTS Then begin Application.CreateForm(TMainForm, MainForm); Application.CreateForm(TFrmReport1, FrmReport1); Application.CreateForm(TFrmReport2, FrmReport2); Application.CreateForm(TFrmReport3, FrmReport3); Application.CreateForm(TAboutBox, AboutBox); Application.CreateForm(TFrmCurveLine, FrmCurveLine); Application.Run; end else finally //先关闭Mutex Object CloseHandle(hMutex); //后释放Mutex Object ReleaseMutex(hMutex); end;// try end.
可以修改.dpr文件,举个例子:program IETool;uses Forms, Windows, Main in 'Main.pas' {frmMain};var hMutex:hwnd; ret:integer;{$R *.res}begin Application.Initialize; // run only once--------------------------------------- hMutex:=CreateMutex(nil,false,'project1'); ret:=GetLastError; if ret=ERROR_ALREADY_EXISTS then begin ReleaseMutex(hMutex); MessageBox(Application.Handle,'抱歉,本程序已经在运行!','收藏伴侣' , MB_OK + MB_DEFBUTTON1 + MB_ICONEXCLAMATION); exit; end; //----------------------------------------------------- Application.CreateForm(TfrmMain, frmMain); Application.Run; end.
program Project1;uses windows, Forms, Unit2 in 'Unit2.pas' {Form2}, Unit1 in 'Unit1.pas' {Form1}, Unit3 in 'Unit3.pas' {Form3}, Unit4 in 'Unit4.pas' {Form4}, Unit5 in 'Unit5.pas' {Form5}, Unit6 in 'Unit6.pas' {Form_about};{$R *.res}const classname='tform2'; var handle:integer; begin handle:= findwindow(classname,nil); if handle<>0 then begin messagebox(0,'此程序已经在运行!','提示',0); halt; end else begin Application.Initialize; Application.CreateForm(TForm2, Form2); Application.Run; end; end.
OnlyRunOnce : THandle;begin
OnlyRunOnce := CreateMutex(nil, True, 'XXX_CENTER');
if GetLastError = 183 then Halt;
Application.Initialize;
Application.Title := 'XXXXXX系统';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
CloseHandle(OnlyRunOnce);
end.
const
CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
MYAPPNAME = "My Delphi Program";
并在Form的定义的public节中加入
procedure CreateParams(var Params: TCreateParams); override;
Procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
在implementation节中加入
{指定窗口名称} procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WinClassName := MYAPPNAME;
end; {处理“恢复”消息}
procedure TForm1.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) = TRUE then
Application.Restore
else
Application.BringToFront;
end;
经过以上修改,程序的主窗口的类名已经被指定了,这是进行判断的基础。一般在程 序刚开始运
行的时候进行判断,所以还要对DPR文件进行修改。 2、对DPR文件的改动 在 uses 节中添加 windows、messages这两个单元加入下列语句,注意两个文件中常 量
CM_RESTORE和MYAPPNAME的定义必须一致
const
CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
MYAPPNAME = "My Delphi Program";
var
RvHandle : hWnd; 将下列语句插到程序最前部(在Application.Initialize之前)
RvHandle := FindWindow(MYAPPNAME, NIL);
if RvHandle > 0 then
begin
PostMessage(RvHandle, CM_RESTORE, 0, 0);
Exit;
end;
这段程序的意思是如果找到一个类名相同的窗口,则向该窗口发送一个消息,并退 出,而本例中
原窗口收到该消息后会自动激活或从图标还原,从而达到了避免二次运行.
windows,//必须加入
Forms,
Unit2 in 'Unit2.pas' {Form2},
Unit1 in 'Unit1.pas' {Form1},
Unit3 in 'Unit3.pas' {Form3},
Unit4 in 'Unit4.pas' {Form4},
Unit5 in 'Unit5.pas' {Form5},
Unit6 in 'Unit6.pas' {Form_about};{$R *.res}const classname='tform2';//form2为主窗体
var handle:integer;
begin
handle:= findwindow(classname,nil);
if handle<>0 then
begin
messagebox(0,'此程序已经在运行!','提示',0);
halt;
end
else begin
Application.Initialize;
Application.CreateForm(TForm2, Form2);
Application.Run;
end;
end.
CreateMutex(nil, True, 'Pro');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '程序已经在运行中......', '提醒' ,MB_ICONINFORMATION);
Halt;
end;
Application.Initialize;
……
Forms,
Windows,
MainF in 'MainF.pas' {MainForm},
DayRep in 'DayRep.pas' {FrmReport1},
MonthRep in 'MonthRep.pas' {FrmReport2},
YearRep in 'YearRep.pas' {FrmReport3},
About in 'About.pas' {AboutBox},
uCurveLine in 'uCurveLine.pas' {FrmCurveLine};{$R *.RES}
Var
hMutex:HWND;
Ret:Integer;
begin
Application.Initialize;
hMutex:=CreateMutex(nil,False,'灌区数据库自动处理系统');
Application.Title := '灌区数据库自动处理系统';
try
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS Then begin
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TFrmReport1, FrmReport1);
Application.CreateForm(TFrmReport2, FrmReport2);
Application.CreateForm(TFrmReport3, FrmReport3);
Application.CreateForm(TAboutBox, AboutBox);
Application.CreateForm(TFrmCurveLine, FrmCurveLine);
Application.Run;
end
else
finally
//先关闭Mutex Object
CloseHandle(hMutex);
//后释放Mutex Object
ReleaseMutex(hMutex);
end;// try
end.
Forms,
Windows,
Main in 'Main.pas' {frmMain};var
hMutex:hwnd;
ret:integer;{$R *.res}begin
Application.Initialize; // run only once---------------------------------------
hMutex:=CreateMutex(nil,false,'project1');
ret:=GetLastError;
if ret=ERROR_ALREADY_EXISTS then
begin
ReleaseMutex(hMutex);
MessageBox(Application.Handle,'抱歉,本程序已经在运行!','收藏伴侣' ,
MB_OK + MB_DEFBUTTON1 + MB_ICONEXCLAMATION);
exit;
end;
//----------------------------------------------------- Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end.
windows,
Forms,
Unit2 in 'Unit2.pas' {Form2},
Unit1 in 'Unit1.pas' {Form1},
Unit3 in 'Unit3.pas' {Form3},
Unit4 in 'Unit4.pas' {Form4},
Unit5 in 'Unit5.pas' {Form5},
Unit6 in 'Unit6.pas' {Form_about};{$R *.res}const classname='tform2';
var handle:integer;
begin
handle:= findwindow(classname,nil);
if handle<>0 then
begin
messagebox(0,'此程序已经在运行!','提示',0);
halt;
end
else begin
Application.Initialize;
Application.CreateForm(TForm2, Form2);
Application.Run;
end;
end.