views:

220

answers:

1

Hello there,

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;
+5  A: 

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;
Sertac Akyuz
thanks for an answer, there were too many comments piling up below the question. I'll try this now.
Peter Perháč
superb! This did the trick! I didn't know that "ShowModal causes all the forms of the application to be disabled". This is a pretty useful insight. ta
Peter Perháč
You're welcome! To prevent a misunderstanding, rather than a cause, ShowModal disables the forms (by calling DisableTaskWindows). Anyway, what is being done here is somewhat against the TApplication design, so you'd be watching for anomaly, for instance if there are more forms.
Sertac Akyuz