Are there any Delphi DFM to Delphi source code convertion tools?
Asked Answered
B

2

4

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?

Borough answered 7/9, 2011 at 10:49 Comment(13)
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 There are TReader and TWriter classes, well documented, for these operations.Faires
The forms are in text format so a non delphi IDE tool is usable/desirableBorough
GExperts source is available as ToTo and Rudy says.Faires
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.Leigh
Please clarify the question: do you need Pascal source, or the text representation of the DFM?Pottery
@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.Faires
Delphi source code = pascal codeBorough
@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).Borough
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.Leigh
@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.Buff
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.Buff
@Buff 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.Lecompte
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.Fiacre
W
14

You can use the ComponentsToCode function from GExperts

Waterside answered 7/9, 2011 at 11:10 Comment(3)
No batch mode and it does it for selected controls to the clipboardBorough
But the sources should make it easy to turn this into a batch mode converter.Snailpaced
Beware that GExperts cannot handle every control under the sun. Most will work though.Dyann
A
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', []));
Andris answered 8/1, 2022 at 17:35 Comment(1)
full source code github.com/ricardodarocha/DfmGeneratorAndris

© 2022 - 2024 — McMap. All rights reserved.