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 xe 横屏竖屏的管理

Delphi 如何快速读取非常大的文本文件

Delphi unigui执行程序部署有3种形式

Delphi-adoquery查询、插入、删除、修改

Delphi 用idhttp得到本机外网ip

Delphi xe6 取得app自己的版本号(横跨4个平台)

Delphi mediaplayer 左声道右声道

Delphi 如何判断当前网卡是物理网卡

Delphi中如何给一个字符串从左边进行补0

Delphi 判断当前网络连接方式

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



打赏

取消

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

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

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

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

评论

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