Delphi的表单设计器非常好,但我们需要直接从源代码中工作。是否有工具或脚本可以将一批DFM文件转换为Delphi源代码?
Delphi的表单设计器非常好,但我们需要直接从源代码中工作。是否有工具或脚本可以将一批DFM文件转换为Delphi源代码?
ComponentsToCode
函数。虽然有些困难,但我已经找到了解决方案。
首先,在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', []));