2005. augusztus 19., péntek

Making Secondary Forms Independent of the Main Form


In my application, I want to be able to iconize the main form and still leave the secondary forms displaying on the desktop. Likewise, I want to be able to select secondary forms without the main form popping up. How can I do this?


Recently a user asked me about this, and I had to do a bit of experimentation before I finally figured it out. And the solution to this problem is actually so simple, you'll scream (actually, I did all the screaming myself). But it's not something that's necessarily easy to find out nor intuitive (maybe it is for some, but it wasn't for me). But before I give you the solution, let's discuss the concept that's behind it.

All windowed controls have a parent of some sort; that is, some control that maintains visual control (ie. 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 the neat thing 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 giving it independence 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, and that place is in the CreateParams procedure.

I've discussed CreateParams in previous articles, so I won't go into details about it, though I will brush over what it does. CreateParams is an inherited procedure that wraps the WinAPI functions CreateWindow and CreateWindowEx that are responsible for a window's initial appearance. It's a convenient way to set display parameters. With it, we can change the a variable parameter called Params that is a TCreateParams structure (you should look this structure up in the online help) to affect a number of different things about a form. 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 another window handle (hint, hint), 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. That window is Windows' Desktop Window. Fortunately we have a way of getting 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;


  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

  TForm2 = class(TForm)
    { 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 declarations }

  Form2: TForm2;


{$R *.DFM}

//Here's the implementation of CreateParams

procedure TForm2.CreateParams(var Params: TCreateParams);
  inherited CreateParams(Params); //Don't ever forget to do this!!!
  Params.WndParent := GetDesktopWindow;


Insanely simple, huh? Sorry I took so long to lead up to it, but while the solution was simple, I just couldn't get away from explaining at least a bit of background information to help those who aren't familiar with the internal workings of the WinAPI. In any case, HAVE AT IT!!!

Nincsenek megjegyzések:

Megjegyzés küldése