老话题了,禁止程序二次运行,可我的程序是托盘程序,于是新的问题产生了? 新问题就是当我的程序最小化的托盘时,用户二次打开程序(用户常犯这样的毛病),使托盘的程序醒来,而不是仅出一个提示!,winamp就是这样做的,如何实现,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 程序写成如下结构var hMutex:hwnd; ret:integer; BroadcastList : DWORD; sName:String;{$R *.res}begin Application.Initialize; sName:=...;//这个名称必须是唯一的,可以用GUID字符串 BringTopMessage := RegisterWindowMessage(PChar(sName));//注册消息 hMutex:=CreateMutex(nil,false,PChar(sName)); ret:=GetLastError; if ret=ERROR_ALREADY_EXISTS then//程序已经运行 begin BroadcastList := BSM_APPLICATIONS; BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,BringTopMessage,0,0);//广播一个消息 end else begin Application.CreateForm(TMDIFrame, MDIFrame);//你的主程序界面 Application.Run; ReleaseMutex(hMutex); end; CloseHandle(hMutex);end.另外,在你的程序主窗体中作如下处理var BringTopMessage:UInt;//该变量定义在Interface段写如下函数procedure TMDIFrame.ApplicationMessage(var Msg: TMsg; var Handled: Boolean);begin if Msg.Message = BringTopMessage then begin //在这里写代码唤醒你的托盘程序 Handled := true; end;end;//在主窗体的Create事件中加入如下代码procedure TMDIFrame.FormCreate(Sender: TObject);begin Application.OnMessage:=ApplicationMessage;end;这样就没有问题了 程序写成如下结构var hMutex:hwnd; ret:integer; BroadcastList : DWORD; sName:String;{$R *.res}begin Application.Initialize; sName:=...;//这个名称必须是唯一的,可以用GUID字符串 BringTopMessage := RegisterWindowMessage(PChar(sName));//注册消息 hMutex:=CreateMutex(nil,false,PChar(sName)); ret:=GetLastError; if ret=ERROR_ALREADY_EXISTS then//程序已经运行 begin BroadcastList := BSM_APPLICATIONS; BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,BringTopMessage,0,0);//广播一个消息 end else begin Application.CreateForm(TMDIFrame, MDIFrame);//你的主程序界面 Application.Run; ReleaseMutex(hMutex); end; CloseHandle(hMutex);end.另外,在你的程序主窗体中作如下处理var BringTopMessage:UInt;//该变量定义在Interface段写如下函数procedure TMDIFrame.ApplicationMessage(var Msg: TMsg; var Handled: Boolean);begin if Msg.Message = BringTopMessage then begin //在这里写代码唤醒你的托盘程序 Handled := true; end;end;//在主窗体的Create事件中加入如下代码procedure TMDIFrame.FormCreate(Sender: TObject);begin Application.OnMessage:=ApplicationMessage;end;这样就没有问题了 《Delphi5开发人员指南》第13章中有非常详细的介绍。还有个mutex.pas单元直接引用就行。 不管是用哪种方式判断已经存在一个实例,关键是要在.dpr里写判别代码,如果发现已经存在实例后就退出,这时主窗体还未创建,即使你用控件实现托盘图标,由于窗体都没创建,此时是不会显示出第2个托盘图标的。 对,楼上说的很好!主要是判断的代码是要写在dpr里。 方法很多的,Google上N多的都是Mutex等的http://lysoft.7u7.net 用Mutex的话,如果程序死掉了,用任务管理器强行终止后,互斥量可能无法清除,再运行程序就运行不起来了。 我用Mutex,程序强制结束,XP系统下会自动清除 Mutex. webbrowser控件使用问题 PIamge白痴问题 beep函数调用之迷惑 请问delphi学到什么程度可以到外面面试程序员工作呀. 我用ADSL 如何让别人经互联网访问我提供的服务程序? 代码该错!大虾帮忙! *关于zero-length string masting delphi... 如何用Socket编程统计出在线人数 *.rar 文件用什么软件阅读 在哪可以download ? thank you ! 如何安装第三方delphi控件 如何在按纽或标签上做超级链接?
hMutex:hwnd;
ret:integer;
BroadcastList : DWORD;
sName:String;{$R *.res}begin
Application.Initialize; sName:=...;//这个名称必须是唯一的,可以用GUID字符串
BringTopMessage := RegisterWindowMessage(PChar(sName));//注册消息 hMutex:=CreateMutex(nil,false,PChar(sName)); ret:=GetLastError;
if ret=ERROR_ALREADY_EXISTS then//程序已经运行
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,BringTopMessage,0,0);//广播一个消息
end
else begin
Application.CreateForm(TMDIFrame, MDIFrame);//你的主程序界面
Application.Run;
ReleaseMutex(hMutex);
end;
CloseHandle(hMutex);
end.另外,在你的程序主窗体中作如下处理
var
BringTopMessage:UInt;//该变量定义在Interface段写如下函数
procedure TMDIFrame.ApplicationMessage(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.Message = BringTopMessage then
begin
//在这里写代码唤醒你的托盘程序
Handled := true;
end;
end;//在主窗体的Create事件中加入如下代码
procedure TMDIFrame.FormCreate(Sender: TObject);
begin
Application.OnMessage:=ApplicationMessage;
end;这样就没有问题了
hMutex:hwnd;
ret:integer;
BroadcastList : DWORD;
sName:String;{$R *.res}begin
Application.Initialize; sName:=...;//这个名称必须是唯一的,可以用GUID字符串
BringTopMessage := RegisterWindowMessage(PChar(sName));//注册消息 hMutex:=CreateMutex(nil,false,PChar(sName)); ret:=GetLastError;
if ret=ERROR_ALREADY_EXISTS then//程序已经运行
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(BSF_POSTMESSAGE,@BroadcastList,BringTopMessage,0,0);//广播一个消息
end
else begin
Application.CreateForm(TMDIFrame, MDIFrame);//你的主程序界面
Application.Run;
ReleaseMutex(hMutex);
end;
CloseHandle(hMutex);
end.另外,在你的程序主窗体中作如下处理
var
BringTopMessage:UInt;//该变量定义在Interface段写如下函数
procedure TMDIFrame.ApplicationMessage(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.Message = BringTopMessage then
begin
//在这里写代码唤醒你的托盘程序
Handled := true;
end;
end;//在主窗体的Create事件中加入如下代码
procedure TMDIFrame.FormCreate(Sender: TObject);
begin
Application.OnMessage:=ApplicationMessage;
end;这样就没有问题了
主要是判断的代码是要写在dpr里。
都是Mutex等的http://lysoft.7u7.net