How can I make the second instance of my program pass control back to the first instance?

前端 未结 1 359
自闭症患者
自闭症患者 2021-01-13 10:09

I have create an application with Delphi XE3. My application have a trayicon (I use TCoolTrayIcon for this) so when the user minimize it there is not a icon on taskbar but o

相关标签:
1条回答
  • 2021-01-13 11:02

    You need to send a message to the other application to request that it shows itself.

    First of all you need to find the other application's main window. There are many ways to do that. For instance you can use FindWindow. Or you can enumerate the top-level windows with EnumWindows. Typically you'd then check for matching window text and/or class name.

    Once you've found the main window of the other instance, you need to give it the ability to set itself to be the foreground window. You need to call AllowSetForegroundWindow.

    var
      pid: DWORD;
    ....
    GetWindowThreadProcessId(hwndOtherInstance, pid);
    AllowSetForegroundWindow(pid);
    

    Then send the window a user-defined message. For instance:

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

    Finally, your other instance's main form needs to listen for this message.

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

    When it encounters the message it must do this:

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

    I'm a little sceptical of your mutex handling code. I don't understand the need for security attributes since you are creating it in the local namespace. But then I see a second call to CreateMutex that ignores the return value, but creates an object in the global namespace.

    0 讨论(0)
提交回复
热议问题