The Delphi form designer is very good but we need to work directly from source. Is there a tool or script that can take a batch of DFM files and convert them to Delphi source code?
Asked
Active
Viewed 2,532 times
5
-
Provided the dfm files are in text format, it is readable in any text editor. To programatically operate on these files, see [longevity-of-using-the-delphi-text-dfm-format-for-my-own-store-and-retriev](http://stackoverflow.com/questions/4171998/longevity-of-using-the-delphi-text-dfm-format-for-my-own-store-and-retrieve) There are TReader and TWriter classes, well documented, for these operations. – LU RD Sep 07 '11 at 11:18
-
The forms are in text format so a non delphi IDE tool is usable/desirable – MX4399 Sep 07 '11 at 11:35
-
GExperts source is available as ToTo and Rudy says. – LU RD Sep 07 '11 at 11:49
-
I don't understand the rationale behind this question. Text DFM files are just as editable as text PAS files. DFM files are part of the source of your program, so working "directly from source" doesn't preclude working on DFM files. – Rob Kennedy Sep 07 '11 at 13:51
-
Please clarify the question: do you need Pascal source, or the text representation of the DFM? – Chris Thornton Sep 07 '11 at 13:59
-
@Chris, the OP wants to convert many text dfm files into their pascal counterparts, kind of reverse engineering. Hence the reference to GExperts which has this ability. – LU RD Sep 07 '11 at 14:11
-
Delphi source code = pascal code – MX4399 Sep 07 '11 at 15:14
-
@Rob Kennedy - Is a DFM not the storage format for a resource to be compiled into the exe? That's not "source code" per se - that's a resource. The rationale for the question is to avoid having TReader reading a stream and then constructing and binding class instances - regardless of how well the standard Delphi approach works, it has practical limits in the end (e.g. dynamic object composition). – MX4399 Sep 07 '11 at 15:33
-
The DFM is converted to binary and linked to the project, not unlike how an RC file is converted to a RES file, or how a PAS file is converted to a DCU file. Once you've converted all the text files into binary, the linker can do its thing. You keep the DFMs in source control, so they're source. – Rob Kennedy Sep 07 '11 at 16:04
-
@Rob I don't completely agree. The DFM files are more autogenerated auxiliaries and are found as plain text resources (RC DATA) in the final executable, even if you store them in binary form. – NGLN Sep 07 '11 at 17:29
-
Maybe the confusion here is source code = source, but source <> source code. A bitmap is a source too. I agree that a DFM is not source code. – NGLN Sep 07 '11 at 17:30
-
@NGLN Doesn't matter whether they are stored as text or binary in .exe, they are exactly analagous to a dialog resource in a traditional Win32 app. – David Heffernan Sep 07 '11 at 18:27
-
IIRC DeDe and/or RevenderPro were able to reconstruct *design class* (OTA term) source given its DFM data. Yeah, Delphi forms were sort of inspired by Window dialog resources. – Premature Optimization Sep 07 '11 at 20:03
2 Answers
15
-
-
5But the sources should make it easy to turn this into a batch mode converter. – Rudy Velthuis Sep 07 '11 at 11:22
-
1Beware that GExperts cannot handle every control under the sun. Most will work though. – dummzeuch Sep 07 '11 at 12:36
0
It's difficult, but i have done a sollution
First, you design a Form Template with form editor, in Delphi, then you write a code to generate a .dfm with the same layout you designed.
For example, we can export an Edit with label we created in template.
var Component: integer;
For Component := 0 to Form1.ComponentCount -1 do
begin
if Form1.Component[Component] is TEdit then
ExportEditToMemo
else
if Form1.Component[Component] is TLabel then
ExportLabelToMemo
...
//all components kind you want
end;
I just show a piece of code to aim this layout
class procedure TTemplateFormatter.ExportLabel(ALabel: TLabel; ALines: TStrings);
begin
ALines.add(format(' object %s: %s', [ALabel.Name, ALabel.ClassName]));
ALines.add(format(' Left = %d', [ALabel.Left]));
ALines.add(format(' Top = %d', [ALabel.Top]));
ALines.add(format(' Width = %d', [ALabel.Width]));
ALines.add(format(' Height = %d', [ALabel.Height]));
ALines.add(format(' Caption = ''%s''', [ALabel.Caption]));
if Not ALabel.ParentFont then
begin
ALines.add(format(' Font.Charset = DEFAULT_CHARSET', []));
ALines.add(format(' Font.Color = clWindowText', []));
ALines.add(format(' Font.Height = %d', [ALabel.Font.Height]));
ALines.add(format(' Font.Name = ''%s', [ALabel.Font.Name]));
ALines.add(format(' Font.Style = []', []));
ALines.add(format(' ParentFont = False', []));
end;
ALines.add(format(' end', []));

Ricardo da Rocha Vitor
- 475
- 5
- 15
-
full source code https://github.com/ricardodarocha/DfmGenerator – Ricardo da Rocha Vitor Jan 08 '22 at 18:58