加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 09:30:22 所属栏目:大数据 来源:网络整理
导读:我用Delphi XE3创建了一个应用程序. 我的应用程序有一个trayicon(我使用TCoolTrayIcon),所以当用户最小化它时,任务栏上没有图标,只有在trayicon上. 为了避免更多的应用程序,我使用此代码: procedure CreateMutexes(const MutexName: String);const SECURITY
我用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忽略了返回值,但在全局命名空间中创建了一个对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读