Wednesday, January 16, 2008

Embed out of process windows into Delphi Control

We may easily embed a Delphi GUI control into another container control like TForm or TPanel:
var F: TForm;
  B: TButton;
begin
F := TForm.Create(nil);
F.Show;
B := TButton.Create(nil);
B.Parent := F;
end;
As TForm or TPanel is a Window GUI control too, we may embed a non-delphi out of process windows into TForm or TPanel control too:
var F: TForm;
   H: THandle;
   T: Cardinal;
begin
 F := TForm.Create(nil);
 F.Show;

 ShellExecute(0, 'open', 'calc', nil, nil, 0);

 // New window may not be created yet, find the window in a loop until it show out
 H := 0;
 T := GetTickCount;
 while H = 0 do begin
   H := FindWindow(nil, PChar('Calculator'));
   if GetTickCount - T > 5000 then
     Break;
 end;

 if H <> 0 then
   Windows.SetParent(H, F.Handle);
end;

No comments: