你看一下这个吧,它是将其它程序在自己的MDI窗体里打开的,你看看有没有借荐的吧
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
PProcessWindow = ^TProcessWindow;
TProcessWindow = record
TargetProcessID: Cardinal;
FoundWindow: hWnd;
end;
TForm1 = class(TForm)
Button1: TButton;
OpenD: TOpenDialog;
Panel1: TPanel;
Splitter1: TSplitter;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hWin: HWND = 0;
implementation
{$R *.dfm}
function EnumWindowsProc(Wnd: hWnd; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
WndProcessID: Cardinal;
begin
GetWindowThreadProcessId(Wnd, @WndProcessID);
if WndProcessID = ProcWndInfo^.TargetProcessID then begin
ProcWndInfo^.FoundWindow := Wnd;
Result := False; // This tells EnumWindows to stop enumerating since we've already found a window.
end else Result := True; // Keep searching
end;
function GetProcessWindow(TargetProcessID: Cardinal): hWnd;
var
ProcWndInfo: TProcessWindow;
begin
ProcWndInfo.TargetProcessID := TargetProcessID;
ProcWndInfo.FoundWindow := 0;
EnumWindows(@EnumWindowsProc, Integer(@ProcWndInfo));
Result := ProcWndInfo.FoundWindow;
end;
function RunAppInPanel(const AppName: string; PanelHandle: HWND): boolean;
var
si: STARTUPINFO;
pi: TProcessInformation;
begin
FillChar(si, SizeOf(si), 0);
si.cb := SizeOf(si);
si.wShowWindow := SW_SHOW;
result := CreateProcess(nil, PChar(AppName), nil,
nil, true, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi);
if not result then exit;
WaitForInputIdle(pi.hProcess, 10000); // let process start!
hWin := GetProcessWindow(pi.dwProcessID);
if hWin > 0 then begin
Windows.SetParent(hWin, PanelHandle);
SetWindowPos(hWin, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
result := true;
end;
// we don't need the handles so we close them
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenD.Execute then begin
if hWin > 0 then PostMessage(hWin, WM_CLOSE, 0, 0); // close any app currently opened
if not RunAppInPanel(OpenD.FileName, Panel1.Handle) then ShowMessage('App not found');
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if hWin > 0 then
PostMessage(hWin, WM_CLOSE, 0, 0);
end;
end.