2009. június 6., szombat

How to start an instance of an application inside another program


Problem/Question/Abstract:

Is there an easy way to start an instance of an application inside another application so it looks like it's MDI when its not. I have the sub-apps and they need to be able to be run seperately, but I would also like to create an application that runs them inside itself, kind of like Word running Excel.

Answer:

If you want to use the Office model (each application is a OLE document server that can be activated in an OLE container) be prepared for a lot of work. Writing OLE document servers is a major effort and the VCLs ActiveX framework will get you only partways to the goal.

For some reason one can get away with parenting a window in another process to a window in your own, via Windows.SetParent. It will then act somewhat like a child window.

procedure TForm1.Button1Click(Sender: TObject);
var
  wnd: HWND;
begin
  WinExec('notepad.exe', sw_hide);
  Sleep(500);
  wnd := FindWindow('notepad', nil);
  Windows.SetParent(wnd, handle);
  SetWindowPos(wnd, 0, 0, 0, clientwidth, clientheight, SWP_NOZORDER or
    SWP_SHOWWINDOW);
end;

You will probably need to implement some inter-app communication, e.g. based on WM_COPYDATA messages, between your applets. Depending on how far you need to go with the integration that may get you most of the way.

Nincsenek megjegyzések:

Megjegyzés küldése