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

该代码段展示了一个Delphi程序,它在创建TForm1时,从资源中提取指定类名(如TForm2)的DFM文本,获取窗体标题,并将其保存到文件中。函数ExtractDFMText用于从资源流中转换二进制DFM数据到文本,GetCaption从中提取窗体的Caption属性。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值