delphi – 如何让我的程序的第二个实例将控制权传递回第一个实例?

delphi – 如何让我的程序的第二个实例将控制权传递回第一个实例?,第1张

概述我用Delphi XE3创建了一个应用程序. 我的应用程序有一个trayicon(我使用TCoolTrayIcon),所以当用户最小化它时,任务栏上没有图标,只有在trayicon上. 为了避免更多的应用程序,我使用此代码: procedure CreateMutexes(const MutexName: String);const SECURITY_DESCRIPTOR_REVISION 我用Delphi XE3创建了一个应用程序.
我的应用程序有一个trayicon(我使用TCoolTrayIcon),所以当用户最小化它时,任务栏上没有图标,只有在trayicon上.

为了避免更多的应用程序,我使用此代码:

procedure CreateMutexes(const Mutexname: String);const  Security_DESCRIPTOR_REVISION = 1;var  SecurityDesc: TSecurityDescriptor;  SecurityAttr: TSecurityAttributes;  MutexHandle: THandle;begin  InitializeSecurityDescriptor(@SecurityDesc,Security_DESCRIPTOR_REVISION);  SetSecurityDescriptorDacl(@SecurityDesc,True,nil,False);  SecurityAttr.nLength := SizeOf(SecurityAttr);  SecurityAttr.lpSecurityDescriptor := @SecurityDesc;  SecurityAttr.binheritHandle := False;  MutexHandle := CreateMutex(@SecurityAttr,False,PChar(Mutexname));  if MutexHandle <> 0 then    begin      if GetLastError = ERROR_ALREADY_EXISTS then        begin          MessageBox(0,'You cannot start more than one instance of ContLab.'                      + #13#10 + 'Use the instance has already started.','ContLab',mb_IconHand);          CloseHandle(MutexHandle);          Halt;        end    end;  CreateMutex(@SecurityAttr,PChar('Global\' + Mutexname));end;

这样,当用户启动应用程序2次时,他收到错误消息,第二个实例终止.

现在我不想显示错误消息,而是打开第一个应用程序实例的主要形式并终止第二个实例.

可能吗?

解决方法 您需要向其他应用程序发送消息以请求它显示自己.

首先,您需要找到其他应用程序的主窗口.有很多方法可以做到这一点.例如,您可以使用FinDWindow.或者您可以使用Enumwindows枚举顶级窗口.通常,您将检查匹配的窗口文本和/或类名称.

一旦找到了另一个实例的主窗口,就需要让它能够将自己设置为前景窗口.您需要调用AllowSetForegrounDWindow.

var  pID: DWORD;....GetwindowThreadProcessID(hwndOtherInstance,pID);AllowSetForegrounDWindow(pID);

然后向窗口发送用户定义的消息.例如:

const  WM_RESTOREWINDOW = WM_APP;....SendMessage(hwndOtherInstance,WM_RESTOREWINDOW,0);

最后,您的其他实例的主要表单需要侦听此消息.

type  TMainForm = class(TForm)  ....  protected    procedure WMRestoreWindow(var Message: TMessage); message WM_RESTOREWINDOW;  ....  end;

当它遇到消息时,它必须这样做:

procedure TMainForm.WMRestoreWindow(var Message: TMessage);begin  inherited;  Visible := True;  Application.Restore;  Application.BringToFront;end;

我对你的互斥锁处理代码有点怀疑.我不了解安全属性的必要性,因为您在本地命名空间中创建它.但后来我看到第二次调用CreateMutex忽略了返回值,但在全局命名空间中创建了一个对象.

总结

以上是内存溢出为你收集整理的delphi – 如何让我的程序的第二个实例将控制权传递回第一个实例?全部内容,希望文章能够帮你解决delphi – 如何让我的程序的第二个实例将控制权传递回第一个实例?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1263812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存