Application.Restore does not get me to where I was before, why?

情到浓时终转凉″ 提交于 2019-12-10 10:50:25

问题


The application I am now trying to support (a former creation of mine) is a complete mess, and so I programmed an extension to it as a separate executable which I then launch, call application.minimize; and WaitForSingleObject (the recently created process). Right after that I call application.restore to get me back to where I left off.

application.Minimize;
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
Application.Restore;
Application.BringToFront;
BringToFront; //the topmost form which was used to launch the app
Show;

I can then see (Win XP), how to describe it?, the frame of the app jump up from the task bar and do as if the app was restoring itself to screen but it does not actually show. As you can see, I am quite desperate and combined app.restore, app.bringtofront,form.bringtofront,form.show... but I think I need some sort of application.show, activate, focus... can't seem to find those.

Also, why is this not enough?

application.Minimize;
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
Application.Restore;

EDIT

The main form is wsMaximized, this calls anotherform.showmodal; which then eventually tries to minimize the app, launch the other process, and restore the app. I think the trick is with the MODALity of the topmost form.

sample code for the other (topmost) form that is shown as modal:

function ExecAndWait(const FileName, Params: string;
  WindowState: Word): Boolean;
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
begin
  { Enclose filename in quotes to take care of
    long filenames with spaces. }
  CmdLine := '"' + FileName + '" ' + Params;
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
    CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS, nil,
    PChar(ExtractFilePath(FileName)),
    SUInfo, ProcInfo);
  { Wait for it to finish. }
  if Result then
  begin
    application.Minimize;
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
    Application.Restore;
    Application.BringToFront;
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  ExecAndWait('C:\Windows\system32\mspaint.exe' , '' , SW_NORMAL);
end;

回答1:


ShowModal causes all the forms of the application to be disabled, except the modal form. You cannot minimize, restore a disabled window at will. Try sth. like;

  if Result then
  begin
    EnableWindow(Application.MainForm.Handle, True);
    application.Minimize;
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
    Application.Restore;
    EnableWindow(Application.MainForm.Handle, False);
    Application.BringToFront;
  end;


来源:https://stackoverflow.com/questions/2162236/application-restore-does-not-get-me-to-where-i-was-before-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!