unit
frmTestEmbedApp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 =
class
(TForm)
pnlApp: TPanel;
procedure
FormCreate(Sender: TObject);
procedure
FormClose(Sender: TObject;
var
Action: TCloseAction);
procedure
FormResize(Sender: TObject);
private
public
end
;
var
Form1: TForm1;
hWin: HWND =
0
;
implementation
{$R *.dfm}
type
PProcessWindow = ^TProcessWindow;
TProcessWindow =
record
ProcessID:
Cardinal
;
FoundWindow: hWnd;
end
;
function
EnumWindowsProc(Wnd: HWND; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
WndProcessID:
Cardinal
;
begin
GetWindowThreadProcessId(Wnd, @WndProcessID);
if
WndProcessID = ProcWndInfo^.ProcessID
then
begin
ProcWndInfo^.FoundWindow := Wnd;
Result :=
False
;
end
else
Result :=
True
;
end
;
function
GetProcessWindow(ProcessID:
Cardinal
): HWND;
var
ProcWndInfo: TProcessWindow;
begin
ProcWndInfo
.
ProcessID := ProcessID;
ProcWndInfo
.
FoundWindow :=
0
;
EnumWindows(@EnumWindowsProc,
Integer
(@ProcWndInfo));
Result := ProcWndInfo
.
FoundWindow;
end
;
function
RunAppInPanel(
const
AppFileName:
string
; ParentHandle: HWND;
var
WinHandle: HWND):
Boolean
;
var
si: STARTUPINFO;
pi: TProcessInformation;
begin
Result :=
False
;
FillChar(si, SizeOf(si),
0
);
si
.
cb := SizeOf(si);
si
.
wShowWindow := SW_SHOW;
if
not
CreateProcess(
nil
,
PChar
(AppFileName),
nil
,
nil
,
true
,
CREATE_NEW_CONSOLE
or
NORMAL_PRIORITY_CLASS,
nil
,
nil
, si, pi)
then
Exit;
WaitForInputIdle(pi
.
hProcess,
10000
);
WinHandle := GetProcessWindow(pi
.
dwProcessID);
if
WinHandle >
0
then
begin
Windows
.
SetParent(WinHandle, ParentHandle);
SetWindowPos(WinHandle,
0
,
0
,
0
,
0
,
0
, SWP_NOSIZE
or
SWP_NOZORDER);
SetWindowLong(WinHandle, GWL_STYLE, GetWindowLong(WinHandle, GWL_STYLE)
and
(
not
WS_CAPTION)
and
(
not
WS_BORDER)
and
(
not
WS_THICKFRAME));
Result :=
True
;
end
;
CloseHandle(pi
.
hProcess);
CloseHandle(pi
.
hThread);
end
;
procedure
TForm1
.
FormClose(Sender: TObject;
var
Action: TCloseAction);
begin
if
hWin >
0
then
PostMessage(hWin, WM_CLOSE,
0
,
0
);
end
;
procedure
TForm1
.
FormCreate(Sender: TObject);
const
App =
'C:\Windows\Notepad.exe'
;
begin
pnlApp
.
Align := alClient;
if
not
RunAppInPanel(App, pnlApp
.
Handle, hWin)
then
ShowMessage(
'App not found'
);
end
;
procedure
TForm1
.
FormResize(Sender: TObject);
begin
if
hWin <>
0
then
MoveWindow(hWin,
0
,
0
, pnlApp
.
ClientWidth, pnlApp
.
ClientHeight,
True
);
end
;
end
.
请发表评论