Mirror

Convert a Delphi form (from file) to text and vice versa (Views: 101)


Problem/Question/Abstract:

Convert your Delphi form from .dfm format to text and vice versa

Answer:

use this function to convert:

  Example (DFM->TXT): ConvertFormToText('unit1.dfm');
  Example (TXT->DFM): ConvertTextToForm('unit1.txt');

uses
  SysUtils;

function ConvertFormToText(SourceFileName: string): boolean;
var
  InputStream, OutputStream: TFileStream;
  DestFileName: string;
begin
  result := true;

  { change the file extension to .txt }
  DestFileName := ChangeFileExt(SourceFileName, '.txt');

  { Create a file stream for the specified file }
  InputStream := TFileStream.Create(SourceFileName, fmOpenRead);
  OutputStream := TFileStream.Create(DestFileName, fmCreate);

  { convert }
  try
    try
      ObjectResourceToText(InputStream, OutputStream);
    except
      on EStreamError do
        Result := False;
    end
  finally
    { free memory }
    InputStream.Free;
    OutputStream.Free;
  end;
end;

function ConvertTextToForm(SourceFileName: string): boolean;
var
  InputStream, OutputStream: TFileStream;
  DestFileName: string;
begin
  result := true;

  DestFileName := ChangeFileExt(SourceFileName, '.dfm');

  InputStream := TFileStream.Create(SourceFileName, fmOpenRead);
  OutputStream := TFileStream.Create(DestFileName, fmCreate);

  try
    try
      ObjectTextToResource(InputStream, OutputStream);
    except
      on EStreamError do
        result := false;
    end
  finally
    InputStream.Free;
    OutputStream.Free;
  end;
end;

<< Back to main page