在Delphi程序中提取本身的DFM资源数据

在Delphi程序中提取本身的DFM资源数据_第1张图片
在Delphi程序中提取本身的DFM资源数据_第2张图片

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    function ExtractDFMText(FormClassName: String): String;
  public
    function GetCaption(FormClassName: String): String;
    procedure SaveDFMTextToFile(FormClassName: String);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowMessage(GetCaption('TForm2'));
  SaveDFMTextToFile('TForm2');
end;

function TForm1.ExtractDFMText(FormClassName: String): String;
var
  ResStream: TResourceStream;
  MemStream: TMemoryStream;
  StrList: TStringList;
begin
  ResStream := TResourceStream.Create(HInstance, FormClassName, RT_RCDATA);
  ResStream.Position := 0;

  MemStream := TMemoryStream.Create;
  ObjectBinaryToText(ResStream, MemStream);
  MemStream.Position := 0;

  StrList := TStringList.Create;
  StrList.LoadFromStream(MemStream);

  Result := StrList.Text;

  MemStream.Free;
  ResStream.Free;
  StrList.Free;
end;

procedure TForm1.SaveDFMTextToFile(FormClassName: String);
var
  FileName: String;
  StrList: TStringList;
begin
  StrList := TStringList.Create;

  FileName := FormClassName + '.DFM';
  StrList.Text := ExtractDFMText(FormClassName);
  StrList.SaveToFile(FileName);

  StrList.Free;
end;

function TForm1.GetCaption(FormClassName: String): String;
var
  StrList: TStringList;
  S: String;
  I: Integer;
begin
  StrList := TStringList.Create;
  StrList.Text := ExtractDFMText(FormClassName);

  Result := '';
  for I := 0 to StrList.Count-1 do
  begin
    S := StrList[I].Trim;
    if S.StartsWith('Caption =') then
    begin
      Result := S.Substring(9).Trim.Replace('''', '');
      Break;
    end;
  end;

  StrList.Free;
end;

end.
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'FormTest'
  ClientHeight = 270
  ClientWidth = 473
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end
unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

end.
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'FormTest2'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
end

你可能感兴趣的:(Delphi开发,Delphi,DFM资源)