【问题标题】:Are there any Delphi DFM to Delphi source code convertion tools?有没有Delphi DFM 到Delphi 源代码的转换工具?
【发布时间】:2011-11-12 01:17:07
【问题描述】:

Delphi 表单设计器非常好,但我们需要直接从源代码工作。有没有什么工具或者脚本可以把一批DFM文件转换成Delphi源代码?

【问题讨论】:

  • 如果 dfm 文件是文本格式,它可以在任何文本编辑器中读取。要以编程方式对这些文件进行操作,请参阅longevity-of-using-the-delphi-text-dfm-format-for-my-own-store-and-retriev 这些操作有 TReader 和 TWriter 类,有据可查。
  • 表格是文本格式,所以非 delphi IDE 工具是可用/可取的
  • GExperts 源代码如 ToTo 和 Rudy 所说。
  • 我不明白这个问题背后的理由。文本 DFM 文件与文本 PAS 文件一样可编辑。 DFM 文件是程序源代码的一部分,因此“直接从源代码”工作并不排除使用 DFM 文件。
  • 请澄清问题:您需要Pascal源,还是DFM的文本表示?

标签: delphi dfm


【解决方案1】:

您可以使用GExperts 中的ComponentsToCode 函数

【讨论】:

  • 无批处理模式,它会为剪贴板中的选定控件执行此操作
  • 但是来源应该可以很容易地把它变成一个批处理模式转换器。
  • 请注意,GExperts 无法处理阳光下的所有控件。不过大多数都可以。
【解决方案2】:

这很难,但我已经解决了

首先,您在 Delphi 中使用表单编辑器设计一个表单模板, 然后您编写代码以生成与您设计的相同布局的 .dfm。

例如,我们可以导出带有我们在模板中创建的标签的编辑。

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;

我只是展示一段代码来瞄准这个布局

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', []));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
相关资源
最近更新 更多