10

For aesthetic reasons, I want to show a form on top of another form, just as if it were a component, say like a TPanel. It should resize with the parent, move around as the parent is dragged by its title bar, etc.

-----------------------------
| main form component 1     |
-----------------------------
| main |  the 'embedded'    |  
| form |  form goes here    |
|comp 2|                    |
-----------------------------

can I do that? If so how?


I am now leaning towards MDI...

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

3 Answers3

22

Put a panel where you want your embedded form to be at design time. At run time, Create the form, then set the embedded form's Parent property to the panel.

procedure TParentForm.FormCreate(ASender: TObject);
begin
  FEmbeddedForm := TEmbeddedForm.Create(self);

  FEmbeddedForm.Parent := Panel1;
  FEmbeddedForm.Align := alClient;
  FEmbeddedForm.Visible := True;

end;

Edit:

If you want to stop the window title and border from being displayed, add this to the bottom of the FormCreate()

  LForm.Caption := '';
  LForm.BorderStyle := bsNone;

BTW, I am not advocating using parented forms over frames, just answering the question. Frames are great (I use them all the time), but they are not exactly the same as Forms. They are almost exactly like a panel with controls on it.

For instance, a frame does not have an OnCreate event, nor an OnShow event, which can be painful sometimes when you are reusing them and need that sort of behavior.

N@

Nat
  • 5,414
  • 26
  • 38
  • 2
    @Nat: .Parent takes a TWinControl, not a WindowHandle. I took the liberty to fix your code. – Francesca Mar 19 '10 at 08:38
  • Anyone know if setting Parent differs in any way from TForm.ManualDock with align=alClient? – Warren P Mar 19 '10 at 21:22
  • @Fransois: Thanks! You are dead right! The end of a long week... @Warran P: `ManualDoc()` can have interesting effects, I keep to setting Parent unless I really am 'docking' a form. – Nat Mar 19 '10 at 22:55
  • and Frame does not have an independent alphablend property – PA. Mar 22 '10 at 18:34
  • EDIT: I was wrong! alphablend property of the embedded form is just ignored. – PA. Mar 22 '10 at 18:42
  • You use frames all the time means I am going to use them all the time too. thanks Nat. – user30478 Aug 08 '19 at 16:33
  • Where to place the components: On the first form, on the second form FEmbeddedForm, or on the panel ?? – gevaraweb Dec 02 '20 at 11:13
14

You could use a frame.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
3

Native win32 MDI is considered "out of date". I think you might be looking for something like the JEDI JvDocking library. I use it to emulate MDI but without using the win32 MDI support.

Warren P
  • 65,725
  • 40
  • 181
  • 316