还原最小化的应用程序

Restore minimized application

如果我再次尝试执行我的应用程序,我需要恢复并将其置于最前面。我使用下一个代码来防止两次执行并恢复它。我已经尝试了我在 Whosebug 上找到的关于这个问题的所有内容,但没有成功。我打开我的应用程序并将其最小化。然后我尝试再次执行该应用程序,它在任务栏上闪烁但既没有被带到前面也没有被恢复。

program MyApplication;

uses
  Forms,
  Windows,
  Unit_Form1 in 'Unit_Form1.pas' {Form1};

{$R *.res}

var
  RvHandle : Hwnd;

begin

  RvHandle := FindWindow(PWideChar('MyApplication'), NIL);
  if RvHandle > 0 then
  begin
      PostMessage(RvHandle, CM_RESTORE, 0, 0);
      Exit;
  end;

  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := 'My Application';
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

和表格 1:

unit Unit_Form1;

interface 

uses ....

const
  CM_RESTORE = WM_USER + 00;

type
  TForm1 = class(TForm)
    ....

 private
    procedure ShowTheApplication; 
 protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure RestoreRequest(var message: TMessage); message CM_RESTORE;

 public

 end;

 ...
implementation

...
procedure TForm1.ShowTheApplication;
 var k: integer;
begin
   // iterate backward to ensure that the correct Z-order is preserved
   for k := Screen.FormCount - 1 downto 0 do begin
      Screen.Forms[k].Show;
   end;
end;

 procedure TForm1.RestoreRequest(var message: TMessage);
 begin
    if IsIconic(Application.Handle) = TRUE then ShowTheApplication;
    Application.MainForm.Visible := True;
    ShowWindow(Application.Handle, SW_RESTORE) ;
    Application.MainForm.WindowState := wsNormal;    // Override minimized state
    Application.BringToFront;
    Application.Restore;
    SetActiveWindow(Application.Handle) ;
    SetForegroundWindow(Application.Handle) ;
 end;

 procedure TForm1.CreateParams(var Params: TCreateParams);
 begin
    inherited CreateParams(Params);
    Params.WinClassName := 'MyApplication';
 end;
end.

使用来自 JVCL 的 TJvAppInstances 组件 https://wiki.delphi-jedi.org/wiki/JVCL_Help:TJvAppInstances

或者查看源代码并获得启发。