Author Topic: Creating forms dynamically  (Read 21393 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
Creating forms dynamically
« on: July 12, 2017, 06:40:00 pm »
Hi,

is there anywhere out here an example how to create forms "on the fly" during program runtime?
I think I will have to calculate the position where elements are setup...?

Dietmar

Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

minesadorada

  • Sr. Member
  • ****
  • Posts: 453
  • Retired
Re: Creating forms dynamically
« Reply #1 on: July 12, 2017, 06:43:19 pm »
Hi,

is there anywhere out here an example how to create forms "on the fly" during program runtime?
I think I will have to calculate the position where elements are setup...?

Dietmar


Not only that, but be sure to set the created form's parent property.  That is the easiest mistake to make.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI


sam707

  • Guest
Re: Creating forms dynamically
« Reply #3 on: July 12, 2017, 07:38:11 pm »
http://lazarus-ccr.sourceforge.net/docs/lcl/forms/requirederivedformresource.html

if you plan to work with ONLY dynamic forms, you can set RequireDerivedFromResource to false, in your project main source file.

After what you would use Application.CreateForm in a classic way

Description of global variable RequireDerivedFromResource from wiki
"The form resource is the lfm file compiled into the executable of your application. For this flag it does not matter if it was compiled via an lrs file or via fpcres. The resource is automatically loaded by TForm and therfore any descendant when it is created via Create(Owner). If the resource is missing there is something wrong with either a resource or the unit which contains the faulty form. If the flag is false you will see a blank form and probably search a long time what is wrong. If you set this flag to true you get an exception. For creating forms without resources you have 3 options:
1) Create a TForm class (not a descendant)
2) Construct your form using the CreateNew() constructor.
3) It is also possible to disable the exception by setting the global variable RequireDerivedFormResource to False."

[Lasarus Menu bar ] >> [Project] >> [View  Project Source] will open the main project file in lazarus.
RequireDerivedFromResource should be one of the 1st instructions at the begin..end. block.

I do personnally prefer the CreateNew constructor. check it  :)
« Last Edit: July 12, 2017, 07:41:27 pm by sam707 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Creating forms dynamically
« Reply #4 on: July 12, 2017, 10:38:04 pm »
Hi,

is there anywhere out here an example how to create forms "on the fly" during program runtime?
I think I will have to calculate the position where elements are setup...?

Dietmar


Not only that, but be sure to set the created form's parent property.  That is the easiest mistake to make.

I think minesadorada means remembering to set the various contained components' Parent properties to the dynamic form that displays them.
The form's Parent property itself should be nil.

sam707

  • Guest
Re: Creating forms dynamically
« Reply #5 on: July 12, 2017, 11:26:35 pm »
@howardpc

any "constructive" comment apart from your keyboard color or the length of your pants?

LOVE YOU LOL

when I debugged delphi 5.5 years ago I was wondering why a MAIN form created in a DLL (BPL / Borland Package Library) was animating on minimize/restore, and DEF NOT a SAME main form created in the executable.
After nights and nights of disassembly spy (2 weeks), i discovered that the main form in the executable had Application's hidden window handle as Owner, so when Application terminated the form was destroyed (FIFO childcomponents way). under windows 95 it was disabling the OS animations.
As far as i can see, Lazarus can not create a MainForm from a package (concept did never happen because of multiplatform issues) BUT the MainForm do not follow OS animations, so I suppose/guess, that the Application.CreateForm from Lazarus is walking the same path as old delphi.
I'm not going to invistigate disassembler as I did, my old bones do prefer to rest at night
« Last Edit: July 12, 2017, 11:31:28 pm by sam707 »

sam707

  • Guest
Re: Creating forms dynamically
« Reply #6 on: July 12, 2017, 11:35:29 pm »
basic concept
in pascal GUI OOP
Parent is the one receiving notifications while Owner is the one destroying childcomponents

so Parent and Owner 'can' be the same component, but not always

minesadorada

  • Sr. Member
  • ****
  • Posts: 453
  • Retired
Re: Creating forms dynamically
« Reply #7 on: July 13, 2017, 08:27:15 am »
Hi,

is there anywhere out here an example how to create forms "on the fly" during program runtime?
I think I will have to calculate the position where elements are setup...?

Dietmar


Not only that, but be sure to set the created form's parent property.  That is the easiest mistake to make.

I think minesadorada means remembering to set the various contained components' Parent properties to the dynamic form that displays them.
The form's Parent property itself should be nil.
Yes of course - forgive me folks for a senior moment :)  I was thinking of components.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Creating forms dynamically
« Reply #8 on: July 14, 2017, 08:38:38 pm »
Hm,

that's what I tried:

Code: Pascal  [Select][+][-]
  1.   frmSplash := TForm.Create(nil);
  2.   frmSplash.Name := 'frmSplash';
  3.   frmSplash.SetBounds(0, 0, 800, 600);
  4.   frmSplash.BorderStyle := bsNone;
  5.  

The compiler breaks in the last line, because it doesn't know "bsNone" like in the Lazarus GUI.
Where is that defined?

Thx,
Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

Handoko

  • Hero Member
  • *****
  • Posts: 5559
  • My goal: build my own game engine using Lazarus
Re: Creating forms dynamically
« Reply #9 on: July 14, 2017, 09:01:10 pm »
bsNone is in unit: Controls, so put Controls in your uses clause.

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Creating forms dynamically
« Reply #10 on: July 14, 2017, 09:53:41 pm »
Thanks!

Now, I am searching for "poScreenCenter" of a form.
Documentation says: "Source position: forms.pp line 59"
But I have Forms already in my uses statement.

... :/

Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Creating forms dynamically
« Reply #11 on: July 14, 2017, 10:41:15 pm »
Here is an easy example...  :)

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Creating forms dynamically
« Reply #12 on: July 15, 2017, 12:47:39 am »
Quote
frmSplash := TForm.Create(nil);
frmSplash.Name := 'frmSplash';
frmSplash.SetBounds(0, 0, 800, 600);
frmSplash.BorderStyle := bsNone;

If you want to make a splash screen and you are on Windows then this is what I would do:
A nice and cool Splash screen...


RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Creating forms dynamically
« Reply #13 on: July 15, 2017, 12:49:11 am »
 :D

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Creating forms dynamically
« Reply #14 on: July 15, 2017, 12:52:07 am »
 :D

 

TinyPortal © 2005-2018