Run the program and the dock with the existing Delphi 2010 program

Is there a way in Delphi 2010 to launch an application using ShellExecute and then install this application inside another?

those. Program A, written in Delphi, contains 1 form. When the form is shown, program B, written in C #, is launched and fixed on the client in the form of program A?

Floor

+3
source share
2 answers

Yes you can do it. You need to get the window handle of the main form in another process (call EnumWindows). Then call SetParentto make this window a child of your window.

, , , .. WaitForInputIdle, . .

. . . . , !


Delphi, . , , . , , . , , #. , ActiveX?

, :

program AppHost;

uses
  Windows, Messages, SysUtils, Forms, Controls, ComCtrls;

{$R *.res}

procedure ResizePage(Page: TTabSheet);
var
  hwnd: Windows.HWND;
  Rect: TRect;
begin
  hwnd := Page.Tag;
  Rect := Page.ClientRect;
  MoveWindow(hwnd, Rect.Left, Rect.Top, Rect.Right-Rect.Left, Rect.Bottom-Rect.Top, True);
end;

type
  PEnumData = ^TEnumData;
  TEnumData = record
    ProcessID: DWORD;
    hwnd: HWND;
  end;

function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
var
  ProcessId: DWORD;
  EnumData: PEnumData;
begin
  EnumData := PEnumData(lParam);
  GetWindowThreadProcessId(hwnd, ProcessId);
  if EnumData.ProcessID=ProcessID then begin
    EnumData.hwnd := hwnd;
    Result := False;
    exit;
  end;
  Result := True;
end;

procedure Absorb(PageControl: TPageControl; const App: string; StartupInfo: TStartupInfo);
var
  Page: TTabSheet;
  ProcessInformation: TProcessInformation;
  EnumData: TEnumData;
begin
  Page := TTabSheet.Create(PageControl);
  Page.PageControl := PageControl;
  Page.Caption := ChangeFileExt(ExtractFileName(App), '');
  CreateProcess(PChar(App), nil, nil, nil, False, 0, nil, nil, StartupInfo, ProcessInformation);
  WaitForInputIdle(ProcessInformation.hProcess, INFINITE);
  EnumData.ProcessID := ProcessInformation.dwProcessId;
  EnumData.hwnd := 0;
  EnumWindows(@EnumWindowsProc, LPARAM(@EnumData));
  Page.Tag := Integer(EnumData.hwnd);
  SetParent(HWND(Page.Tag), Page.Handle);
  ResizePage(Page);
end;

type
  TEventProvider = class
  private
    FForm: TForm;
    FPageControl: TPageControl;
    procedure FormResize(Sender: TObject);
  public
    constructor Create(Form: TForm; PageControl: TPageControl);
  end;

{ TEventProvider }

constructor TEventProvider.Create(Form: TForm; PageControl: TPageControl);
begin
  inherited Create;
  FForm := Form;
  FPageControl := PageControl;
  FForm.OnResize := FormResize;
end;

procedure TEventProvider.FormResize(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to FPageControl.PageCount-1 do begin
    ResizePage(FPageControl.Pages[i]);
  end;
end;

procedure Main(Form: TForm);
var
  StartupInfo: TStartupInfo;
  PageControl: TPageControl;
begin
  Form.ClientHeight := 600;
  Form.ClientWidth := 800;
  Form.Caption := 'All your processes are belong to us';
  PageControl := TPageControl.Create(Form);
  PageControl.Parent := Form;
  PageControl.Align := alClient;
  StartupInfo.cb := SizeOf(StartupInfo);
  GetStartupInfo(StartupInfo);
  Absorb(PageControl, 'C:\Windows\Notepad.exe', StartupInfo);
  Absorb(PageControl, 'C:\Program Files\CommandLine\depends.exe', StartupInfo);
  TEventProvider.Create(Form, PageControl);
end;

var
  Form: TForm;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm, Form);
  Main(Form);
  Application.Run;
  Form.Free;
end.
+6

#

, , ,

GetProcessID 0?

, , - 2 Delphi, 1

, dockapp2 dockapp1, , .

GetProcessID 0 !

procedure TForm2.BitBtn1Click(Sender: TObject);
var 
n: Integer;
n2: Integer;
begin
  n := ShellExecute(0, 'open', PChar('c:\temp\dockapp2\dockapp2.exe'), nil, nil,     SW_SHOWNORMAL); 
  n2:= GetProcessId(n);
 Caption := IntToStr(n2);
end;
+1

All Articles