Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to Make Secondary Forms Independent of the Main Form Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

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 

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:

1   unit Unit2;
3   interface
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
8   type
9     TForm2 = class(TForm)
10    private
11      { Private declarations }
12      //override the CreateParams procedure for any child forms you want to
13      //make independent of the main form
14      procedure CreateParams(var Params: TCreateParams); override;
15    public
16      { Public declarations }
17    end;
19  var
20    Form2: TForm2;
22  implementation
24  {$R *.DFM}
26  //Here's the implementation of CreateParams
28  procedure TForm2.CreateParams(var Params: TCreateParams);
29  begin
30    inherited CreateParams(Params); //Don't ever forget to do this!!!
31    Params.WndParent := GetDesktopWindow;
32  end;
34  end.

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!!!

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC