All windowed controls have a parent, or control, of some sort that maintains visual control (for example, display) over it. Main forms of an application all point to the Application as their parent. Likewise, by default, secondary forms point to the main form of the application for parentage. But what's neat about creating windowed objects in Delphi (though you need to be careful with some controls) is that you can change the parentage of a control to isolate its visual control, essentially making it independent from its default parent. Okay, so how do you do it? You might think that you can reset parentage at FormCreate, but that's not the right place to do it. The only way to do this is before the window gets created in the first place in the CreateParams procedure.
CreateParams is an inherited procedure that wraps the WinAPI functions that are responsible for how a window initially appears: CreateWindow and CreateWindowEx. It conveniently sets display parameters and allows you to change the variable parameter called Params, a TCreateParams structure (you should look this structure up in the online help), to affect a form in a number of ways. One of the fields in the TCreateParams structure is WndParent. This parameter specifies the handle of the window that controls the display of the window being created. By changing this parameter to point to another window handle, we can change the default parentage.
So now it's a matter of deciding what window is going to be the secondary form's new parent. In this case, whenever we want to make a secondary form independent of the main form, we're essentially turning it into its own mini-application without creating a new EXE. So it's best to choose a parent that's at the highest order in the system: Windows' Desktop Window. Fortunately we can get to its handle by using the WinAPI call GetDesktopWindow, which returns the handle of the Desktop.
Okay, we've covered all the bases. Now you're going to kill me for belaboring the point. Here's the code:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm2 = class(TForm)
private
{ Private declarations }
//override the CreateParams procedure for any child forms you want to
//make independent of the main form
procedure CreateParams(var Params : TCreateParams); override;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
//Here's the implementation of CreateParams
procedure TForm2.CreateParams(var Params : TCreateParams);
begin
inherited CreateParams(Params); //Don't ever forget to do this!!!
Params.WndParent := GetDesktopWindow;
end;
end.
Although the solution appears to be insanely simple, I wanted to provide some background information to help those who aren't familiar with the internal workings of the WinAPI. In any case, have at it!