If I put a component on the form I don't see any code like MyComp:=TMyComponent.Create
in unit code. I think the component is created automatically, but when ? And the same happends with the properties that I configured after I put the component on the form. When they are applied at runtime ?
When, the code that create the components on the form and the code that set their properties, is called?
930 views Asked by Marus Gradinaru At
2
There are 2 answers
0
On
Information about controls and components as well as their properties that you edit while designing in IDE will be stored in your form .dfm
file. Creation of that form in run-time will trigger the process of automatic loading of that .dfm
file and all controls and components will be initialized at that time.
This is rather simple explanation of what exactly happens, you can start debugger at form creation line and follow what is happening there, but it is quite longish process and you can easily get lost if you are still learning.
You can find form creation code that Delphi automatically writes in .dpr file of your project.
Application.CreateForm(TForm1, Form1);
The properties for a form and all the design time components that live on it are streamed in by the framework during the construction of the form. That process is triggered from the form's constructor, in
TCustomForm.Create
. The pertinent code in there looks like this:The key is the call to
InitInheritedComponent
. That is a function defined in theClasses
unit that does the heavy lifting. In a very broad overview it does the following:RT_RCDATA
resource of that name. That resource is the .dfm file.Caption = 'My main form'
and it turns that into an assignment of the string'My main form'
to the form's propertyCaption
.In order for all of this to work, the streaming framework relies on RTTI. It knows nothing at all at compile time of your classes and components. Hence the need for RTTI. The streaming framework uses the old style RTTI and in fact that is the reason for the existence of old style RTTI. If ever you wonder about why old style RTTI is the way it is, try to view it from the perspective of having been designed to support streaming.