delphi - 如何shell到另一个应用程序并让它以delphi形式出现

标签 delphi shellexecute setparent

在 Delphi 中,我多年来一直使用 ShellExecute 来启动(并可选择等待)其他应用程序。但现在,我需要让这些应用程序之一出现在我的 Delphi 应用程序表单之一中。我尝试使用下面的代码作为一个简单的测试来打开记事本(它会这样做)并在我的表单上的 PAnel1 中显示结果(它不会)。有好心人能让我走上正轨吗? 谢谢

var
  Rec          : TShellExecuteInfo;
  wnd : HWnd;
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_Show;

  ShellExecuteEx(@Rec);

  wnd := Windows.FindWindow( 'Notepad', nil );
  Windows.SetParent( Wnd, PAnel1.Handle );

end;

最佳答案

省略了所有错误检查,但这应该可以帮助您开始:

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;

您绝对应该做的是枚举新进程的窗口,而不是简单地使用 FindWindow() 返回的任何窗口句柄。

关于delphi - 如何shell到另一个应用程序并让它以delphi形式出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/796883/

相关文章:

delphi - 如何在 Delphi 7 中释放 Windows 句柄指针?

delphi - 为什么 Indy 9 包含在 Delphi 2009 中?使用安全吗?

Kotlin native - 执行可执行文件

c++ - ShellExecute 在 C++ 中打开一个 .exe

c# - 在窗口打开之前设置窗口的Parent

delphi - 分配给 Setparent(..) 后 Showmodal 出现问题

Delphi XE2 - 构建 64 位应用程序

delphi - 如何将位图的一部分显示为透明

python - 如何从 Python 执行 VS2008 命令并获取其输出?

java - 在Java窗口中嵌入c++ opengl窗口