~下午比较忙,没来及看。
你试试吧。
//-------------------
// Unit1.pas
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, Unit2;
type
TfrmMain = class(TForm)
opendlg: TOpenDialog;
btnLoad: TButton;
procedure btnLoadClick(Sender: TObject);
private
{ Private declarations }
procedure LoadDfm(filename:String);
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.btnLoadClick(Sender: TObject);
begin
if openDlg.Execute then
begin
LoadDfm(openDlg.FileName);
end;
end;
procedure TfrmMain.LoadDfm(filename: String);
var
dfmFile : TStream;
binDfmFile : TStream;
frm : TForm1;
comp : TComponent;
begin
dfmFile := TFileStream.Create(filename, fmOpenRead);
binDfmFile := TMemoryStream.Create;
ObjectTextToBinary(dfmFile, binDfmFile);
binDfmFile.Position := 0;
// dfmFile 判断
comp := binDfmFile.ReadComponent(nil);
frm := comp as TForm1;
frm.Show;
dfmFile.Free;
binDfmFile.Free;
end;
end.
//-------------------------------
// Unit对应的dfm
object frmMain: TfrmMain
Left = 426
Top = 274
Width = 462
Height = 339
Caption = 'frmMain'
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 btnLoad: TButton
Left = 104
Top = 88
Width = 75
Height = 25
Caption = 'Load...'
TabOrder = 0
OnClick = btnLoadClick
end
object opendlg: TOpenDialog
Filter = '*.dfm|*.dfm'
Left = 320
Top = 32
end
end
//---------------------------------
// Unit2.cpp(TForm1)
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Initialization
RegisterClass(TForm1);
RegisterClass(TMainMenu);
end.
//-----------------
// Unit2.dfm 里面没有东西