20.3.1.5 dynamic DFM File Application Secrets
1. dynamic DFM File Overview
The dynamic DFM file is relative to the static DFM file. The so-called static DFM file refers to the design of a form file in the Delphi development environment. The design process of a form is the process of programming. Therefore, the dynamic DFM file is the DFM file that is generated or accessed in the process of running the program.
Dynamic DFM file creation and use of the following two kinds of situations:
The Create method dynamically generates the form or part, and then dynamically generates other parts to insert it into the DFM file while the program is running
In the Delphi development environment, the design produces the DFM file, then uses the DFM file access function, or uses the stream object and the Filer object method, the DFM file reads into the memory, carries on the processing, finally also in the disk
The DFM file generated by the general method of Delphi Form design provides the component structure at the beginning of the program operation. Because in the form design process, each part in the form defines a part variable in the program's object declaration. This fixed structure, while convenient to use, is at the expense of flexibility.
In a Delphi application, you sometimes need to create control during the run and then insert that control into another part. For example:
Procedure Tform1.button1click (Sender:tobject);
Var
Ctrl:tcontrol
Begin
Ctrl: = Tedit.create (Self);
Ctrl.top: = 100;
Ctrl.left: = 100;
Ctrl.width: = 150;
Ctrl.height: = 20;
Insertcontrol (Ctrl);
End
The advantage of dynamic insertion control is that any number of any type of control can be inserted at any time, anywhere. Because application requirements are known in many cases when the program is running, dynamic insertion control becomes important. And in many cases, you need to save these interface elements for the program to call again. such as the customization of the application interface, the preservation of the system State, the preservation of the dialog box, etc. This is the best choice to generate a dynamic DFM file.
The disadvantage of dynamic insertion control is that before inserting control, it is impossible to see the control's size, style, position and so on, that is, the process of dynamic Insert control is non-visual. But the visual design of static DFM file can be used. This is the second method of generating and using a dynamic DFM file. That is, before the application runs, in the Delphi development environment, use the visual development tool to design the desired window or part style to be saved in the DFM file. The DFM file is then read into memory while the application is running. The Delphi stream object and the Filer object automatically create the part and all the parts it owns based on the contents of the DFM file when reading the DFM file.
There are two points to be aware of when using a dynamic DFM file.
Each dynamically inserted control or part must be registered in the program by calling RegisterClass
Read into the DFM file automatically creates a part, and if the Insertcontrol method is called, the Removecontrol method is called to remove the control when the window is closed, otherwise an exception event is generated
2. One of dynamic DFM file application: The card design of hypermedia system
Delphi various types of visual parts, such as text parts, editing parts, graphics and image parts, database parts, media components and OLE parts, each part of the screen occupies a certain area, has a considerable performance, can be used as a card in the media, Therefore, these visual parts can be used for the design of the hypermedia system card.
Hypermedia card design requires that the number of media and media in the card is unrestricted, and must be able to modify and access the card, therefore, the use of dynamic DFM file is more appropriate. And if the stream object is used to store the card in the Database Blob field, it creates an opportunity to combine hypertext with relational database technology.
The following is a partial source program in the Hypermedia card design subsystem that demonstrates how to create objects, insert objects, and Access dynamic DFM files.
⑴ registering objects in the application
Procedure Tmainform.formcreate (Sender:tobject);
Begin
RegisterClass (Tlabel);
RegisterClass (Tedit);
RegisterClass (TMemo);
RegisterClass (TButton);
RegisterClass (Tpanel);
RegisterClass (TPANELP);
RegisterClass (TBITBTN);
...
End
⑵ Create and Insert objects
Procedure Tmdichild.formclick (Sender:tobject);
Var
Ctrl:tcontrol;
Point:tpoint;
Begin
GetCursorPos (point);
Point: = Background.screentoclient (point);
Case Curtoolindex of
1:begin
Ctrl: = Tlabel.create (self);
Tlabel (Ctrl). AutoSize: = False;
Tlabel (ctrl). Caption: = ' Label ' +s;
Tlabel (ctrl). Name: = ' Label 1 ';
Tlabel (ctrl). Top: = Point.y;
Tlabel (ctrl). Left: = Point.x;
Tlabel (Ctrl). Height: = Round (100*res/1000/ratio);
Tlabel (Ctrl). Width: = Round (600*res/1000/ratio);
Tlabel (Ctrl). Color: = Clwhite;
Tlabel (Ctrl). Font.Color: = Clblack;
Tlabel (Ctrl). Font.Name: = ' Roman ';
Tlabel (Ctrl). Font.height: =-tlabel (Ctrl). Height;
Tlabel (Ctrl). Font.pitch: = fpfixed;
Tlabel (Ctrl). Enabled: = False;
Tlabel (Ctrl). OnClick: = Labelclick;
Tlabel (Ctrl). OnMouseMove: = Reportpos;
Background.insertcontrol (Ctrl);
Curtool.down: = False;
Curtool: = nil;
...
End
2:begin
Ctrl: = Tedit.create (self);
Tedit (ctrl). AutoSize: = True;
Tedit (ctrl). Top: = Point.y;
Tedit (ctrl). Left: = Point.x;
Tedit (Ctrl). Height: = 20;
Background.insertcontrol (Ctrl);
...
End
3:
...
End
End
⑵ Access Dynamic DFM file
Procedure Tmainform.fileopen (Sender:tobject);
Begin
If Opendialog.execute Then
Begin
Designwin: = tmdichild.create (application);
Readcomponentresfile (Opendialog.filename, Designwin);
Designwin.init;
FileName: = Opendialog.filename;
Designwin.caption: = Ffilename;
End
End
Designwin is a form part of the Tmdichild type defined in Tmainform, a card design platform; Ffilename is a private variable that holds the current edited card file name. The Designwin init method is implemented as follows:
Procedure Tmdichild.init;
Var
I:integer;
Ctrl:tcontrol;
Begin
Background.bringtofront;
With BackGround do
For i:= 0 to ControlCount-1 do
If Controls[i]. Name <> ' Then
ObjectIns.ObjectList.Items.AddObject (Controls[i]. Name, Controls[i]);
End
Background is a tpanel type of widget, all dynamically created objects are inserted into the background, so call Background.insertcontrol (Ctrl) later; Objectins is a copy of Delphi. Media Property Editor.
The stored procedure for the dynamic DFM file is this:
Procedure Tmainform.filesave (Sender:tobject);
Begin
If Designwin.curcontrol <> Nil Then
DesignWin.CurControl.Enabled: = True;
Writecomponentresfile (Ffilename, Designwin);
Designwin.caption: = FileName;
End
End
Because the Insertcontrol method is called in the Designwin init method, the Removecontrol is called when the Designwin window is closed, or a memory error is generated when the Designwin window is closed.
Procedure Tmdichild.formclosequery (Sender:tobject; var canclose:boolean);
Var
I:integer;
Ctrl:tcontrol;
Removed:boolean;
Begin
If Modified = True Then
If Messagedlg (' Close the form? ', Mtconfirmation,
[Mbok, Mbcancel], 0 = Mrcancel Then
Canclose: = False;
If Canclose = True Then
Begin
Repeat
Removed: = False;
I: = 0;
Repeat
If Background.controls[i]. Name <> ' Then
Begin
Background.removecontrol (Background.controls[i]);
Removed: = True;
End
I: = i + 1
Until (I >= background.controlcount) or (removed = True);
Until (removed = False);
SendMessage (Objectins.handle, wm_mdichildclosed, 0, 0);
End
End