Recently, a question was posted on stackoverflow on how to embed an external window (created from an ActiveX DLL) into a MDI application created in Delphi.

I thought it was interesting, fired up Delphi and began tracing the default MDI app created by Delphi. In the VCL,

  • when a MDI Child is created, it is created in the method TCustomForm.CreateWindowHandle located in the Forms unit by calling CreateWindowEx.
  • when a MDI Child is being destroyed, it is destroyed in the method TCustomForm.DestroyWindowHandle by calling SendMessage.

So, even if you created dynamic methods to handle either or both WM_MDICREATE and WM_MDIDESTROY, your methods wouldn't get called.

One way of converting an external window, into an MDI child, is by calling SetParent(TargetExternalWindowHandle, TargetNewParentWindowHandle), setting the external window's parent to the target owner's window handle.

In order to destroy the MDI child, send the WM_CLOSE to the window handle of the MDI child.

A demo of my MDI hack is available on CodeCentral.