How to shell to another app and have it appear in a delphi form

前端 未结 3 2220
既然无缘
既然无缘 2020-12-01 05:47

In Delphi I\'ve used ShellExecute for years to launch (and optionally wait for) other applications. Now though, I need to have one of these applications appear in one of my

相关标签:
3条回答
  • 2020-12-01 06:31

    All error checking omitted, but this should get you started:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Rec: TShellExecuteInfo;
    const
      AVerb = 'open';
      AParams = '';
      AFileName = 'Notepad.exe';
      ADir = '';
    begin
      FillChar(Rec, SizeOf(Rec), #0);
    
      Rec.cbSize       := SizeOf(Rec);
      Rec.fMask        := SEE_MASK_NOCLOSEPROCESS;
      Rec.lpVerb       := PChar( AVerb );
      Rec.lpFile       := PChar( AfileName );
      Rec.lpParameters := PChar( AParams );
      Rec.lpDirectory  := PChar( Adir );
      Rec.nShow        := SW_HIDE;
    
      ShellExecuteEx(@Rec);
      WaitForInputIdle(Rec.hProcess, 5000);
    
      fNotepadHandle := Windows.FindWindow( 'Notepad', nil );
      Windows.SetParent( fNotepadHandle, Handle );
    
      Resize;
      ShowWindow(fNotepadHandle, SW_SHOW);
    end;
    
    procedure TForm1.FormResize(Sender: TObject);
    begin
      if IsWindow(fNotepadHandle) then begin
        SetWindowPos(fNotepadHandle, 0, 0, 0, ClientWidth, ClientHeight,
          SWP_ASYNCWINDOWPOS);
      end;
    end;
    

    What you should definitely do is enumerate the windows of the new process, instead of simply using any window handle that FindWindow() returns.

    0 讨论(0)
  • 2020-12-01 06:31

    That will be a tricky one, if it's even possible.

    I've seen approaches that will work for text-based applications - they generally capture the standard output of the process as it happens and put it into a text control.

    But what you're talking about is a fully fledged graphical application (Notepad, despite working on text, display pixels, not character codes).

    So, unless Notepad provides an interface where you can:

    • request arbitrary characters in the buffer; and
    • send arbitrary keystrokes to the program, I'd say you're flat out of luck.

    Definitely a kludge, but one option is to continuously monitor the Notepad windows and ensure it's always superimposed over your forms client area. That's pretty horrible since you have to stop it moving, resizing, minimizing and so on, and maintain its z-order to be just above your applications. I wouldn't wish those requirements on my worst enemy.

    Have you thought of using an editor control built specifically for Delphi (or an ActiveX editor that you could embed)? That might be a better approach.

    0 讨论(0)
  • 2020-12-01 06:35
    var
      URL: string;
    begin
      URL:= DBMemoURL.Text;
      // ShellExecute(self.WindowHandle,'open', PChar(URL), nil, nil, SW_SHOW); //default browser
         ShellExecute(self.WindowHandle,'open','chrome.exe', PChar(URL), nil, SW_SHOW); 
    
    0 讨论(0)
提交回复
热议问题