请问如何打开dfm文件?(100分)

  • 主题发起人 主题发起人 chcw
  • 开始时间 开始时间
C

chcw

Unregistered / Unconfirmed
GUEST, unregistred user!
手头有一个文本格式的dfm文件和它对应的dcu文件(但是没有pas文件),如何在Delphi中
以可视化的方法打开这个dfm并进行编辑?
 
用UltraEdit
 
右击form选择 View as Test
 
就用delphi打开
虽然不可视,但是也差不多了
一条一条写的挺清楚的,就是图片比较恶心
如果你非要看着的话
就自己做一个吧
有一个老外用delphi做了一个delphi
他可以打开delphi的dfm文件
里面肯定有他的格式,你参照一下他是怎么做的
自己弄一个来
 
其实DFM文件就是一种资源文件,不管比一过的还是没有编译过的都可以使用资源编辑器来编辑
,甚至使用Borland C++ 都可以修改.exe文件的资源。也可以修改Form中的东西。
 
用delphi新建一个Form,然后View as Text,
然后复制你的DFM文件的内容到这个文件,注意按格式保留新建Form的第一行,然后View as Form
 
这个程序可以根据DFM文件动态生成窗体(注意要将TForm1改为TForm)。
procedure DFMtoObject(FileName:String);
Var
L:TStringList;
begin
RegisterClass(TForm);
RegisterClass(TButton);
RegisterClass(TPanel);
RegisterClass(TCheckBox);
RegisterClass(TImage);
RegisterClass(TPopupMenu);
RegisterClass(TMenuItem);
//...
RegisterClass(TListView);
L:=TStringList.Create;
L.LoadFromFile(FileName);
(StringToComponent(L.Text,Nil) as TForm).Show;
L.Free;
end;

function StringToComponent(Value: string;Instance: TComponent): TComponent;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result := BinStream.ReadComponent(Instance);
finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;
 
楼上的,我用你的方法我怎么得不到呢?
是不是还要求别的什么你没写出来。
 
随便搞个按钮
procedure TForm1.Button1Click(Sender: TObject);
var
S:TfileStream;
TempForm:TForm;
begin
if Opendialog1.Execute then
begin
S:=TFileStream.Create(OpenDialog1.FileName ,fmOpenRead);
Tempform:=TForm.Create(Application);
S.ReadComponentRes(TempForm);
TempForm.Show;
end;
end;

initialization
RegisterClasses ([TBitBtn,TLabel,TForm,TButton,TPanel,TCheckBox,TImage,TPopupMenu,TMenuItem,......]);
//别忘了在Usesz中加入定义组件的单元,如TPanel包含在ExtCtrls中

end.
 
注意:要把dfm中所有的事件声明都删掉。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Menus, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure Image1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
function StringToComponent(Value: string;Instance: TComponent): TComponent;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var
L:TStringList;
begin
RegisterClass(TForm);
RegisterClass(TButton);
RegisterClass(TPanel);
RegisterClass(TCheckBox);
RegisterClass(TImage);
RegisterClass(TPopupMenu);
RegisterClass(TMenuItem);
//...
RegisterClass(TListView);
if OpenDialog1.Execute then
begin
L:=TStringList.Create;
L.LoadFromFile(OpenDialog1.FileName);
(StringToComponent(L.Text,Nil) as TForm).Show;
L.Free;
end;
end;

function StringToComponent(Value: string;Instance: TComponent): TComponent;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result := BinStream.ReadComponent(Instance);
finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;

procedure TForm1.Image1Click(Sender: TObject);
begin

end;

end.

{Form1.DFM}
object Form1: TForm
Left = 480
Top = 108
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 80
Top = 136
Width = 105
Height = 105
Picture.Data = {
07544269746D617076010000424D760100000000000076000000280000002000
000010000000010004000000000000010000120B0000120B0000100000000000
0000000000000000800000800000008080008000000080008000808000007F7F
7F00BFBFBF000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFF
FF00333333333333333333333333333333333333333333333333333FF3333333
3FF333003333333300333377FF33333377FF300003333330000337777FFFFFF7
777F000000000000000077777777777777770F88FFFF8FFF88F07F333F333333
33370FFF9FFF8FFFFF707F337FF333FFFFF70FF999FF800000037F3777333777
77730FFF9FFF088880337F3373337F3337330FFFFFFF088803337FFFFFFF7FFF
73337000000000003333777777777777F3333333333939993933333333333777
3333333333333393333333333333337333333333333393339333333333333333
3333333333333393333333333333333333333333333333333333333333333333
3333}
end
object Button1: TButton
Left = 16
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
end
object Panel1: TPanel
Left = 72
Top = 208
Width = 185
Height = 41
Caption = 'Panel1'
TabOrder = 1
end
object CheckBox1: TCheckBox
Left = 112
Top = 288
Width = 97
Height = 17
Caption = 'CheckBox1'
TabOrder = 2
end
object ListView1: TListView
Left = 296
Top = 184
Width = 250
Height = 150
Columns = <>
TabOrder = 3
end
end
 
同意wk_knife
 
后退
顶部