如何实现应用程序只能运行一次啊?谢谢!
如何实现应用程序只能运行一次啊?谢谢! 问题点数:30、回复次数:12Top
1 楼hongqi162(失踪的月亮)回复于 2003-12-01 09:03:50 得分 2
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.Top
2 楼rlpcdk()回复于 2003-12-01 09:16:13 得分 1
关注Top
3 楼ndujun(小军)回复于 2003-12-01 09:16:25 得分 2
留信箱,我给你我写的完整代码。Top
4 楼fireflyer(雪阳)回复于 2003-12-01 09:18:42 得分 3
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;
这段程序的意思是如果找到一个类名相同的窗口,则向该窗口发送一个消息,并退 出,而本例中
原窗口收到该消息后会自动激活或从图标还原,从而达到了避免二次运行.Top
5 楼softwaiter(softwaiter)回复于 2003-12-01 09:22:55 得分 1
网上有很多这样的代码,你可以搜一下,搜不到的话,给我发信,可以给你Top
6 楼flyforlove(吾将远去)回复于 2003-12-01 09:33:35 得分 1
搞不懂你说的意思,你说只让它运行一次,是指的同时只能存在一个实例呢,
还是这个程序运行一次,就不能运行了呢?
如果是第一种情况,楼上两位说的方法都很经典。
如果事后种情况,运行一次删除自身就可以了。Top
7 楼fireflyer(雪阳)回复于 2003-12-01 09:34:27 得分 4
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.
Top
8 楼moqiyayan(万俟雅言)回复于 2003-12-01 09:39:14 得分 3
在*.DPR中加上这样一段:
CreateMutex(nil, True, 'Pro');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '程序已经在运行中......', '提醒' ,MB_ICONINFORMATION);
Halt;
end;
Application.Initialize;
……Top
9 楼lemon_wei(研究BT,做好P2P)回复于 2003-12-01 09:40:24 得分 4
这是我的程序:供你参考,修改工程文件
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.Top
10 楼xiaoqiang123(xiaoqiang)回复于 2003-12-01 10:06:46 得分 4
可以修改.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.
Top
11 楼bandrui(蔡尔哈察苏)(知道自己不知道)回复于 2003-12-01 10:25:50 得分 1
收了Top
12 楼ls2008(結婚了吧,傻B了吧)回复于 2003-12-01 10:30:19 得分 4
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.Top




