delphi 实现一个程序在另一个程序内运行


本文整理自网络,侵删。

 首先制作一个子程序,这个程序用于在主程序中指定位置运行。

随便拖一个 exe 就行了,设置窗体的 Caption 为 Child。

然后制作主程序,在主窗体中放上一个Panel ,子程序将在这个 Panel 中运行。注意,不需要把主程序设为 MDIForm。

编写以下代码即可:

unit frmMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ShellAPI, TlHelp32;

type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
pnlChildForm: TPanel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1 : TForm1;

implementation

{$R *.dfm}

function TaskExists(ExeFileName: string): Boolean;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop : BOOL;
FSnapshotHandle : THandle;
FProcessEntry32 : TProcessEntry32;
begin
result := False;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
begin
Result := True;
Break;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if not TaskExists('ChildForm.exe') then
ShellExecute(0, 'open', 'ChildForm.exe',
nil, PChar(ExtractFilePath(ParamStr(0))), SW_SHOW);

end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
h : THandle;
begin
if TaskExists('ChildForm.exe') then
begin
h := FindWindow(nil, 'Child');
windows.SetParent(h, pnlChildForm.Handle);
end;
end;

end.

运行程序后可以看到,子程序在主程序内运行,效果类似于 MDI 窗体

相关阅读 >>

Delphi 开发中遇到的dll问题思考及解决方法

Delphi获取13位格林治时间实现方法

Delphi xe2 - 实现主窗口在任务栏上不显示

Delphi 判断图像格式bmp jpg gif pcx png psd ras sgi tiff err

Delphi 利用unigui中的tunipagecontrol实现多页面

Delphi实现类似嵌入桌面的效果

Delphi纯api窗体程序

Delphi 62进制转10进制

Delphi 打开"我的电脑"等特殊文件夹

idhttp相关:伪造来源地址、cookie欺骗、通过代理访问

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...