dfm TO txt ( 积分: 200 )

  • 主题发起人 主题发起人 YJT-117
  • 开始时间 开始时间
Y

YJT-117

Unregistered / Unconfirmed
GUEST, unregistred user!
function TForm1.dfmFileLoad(const aFile: String): string;
var
aInput : TStream;
aOutput : TStream;
aStr : TStrings;
begin
Result := '';
aInput := TFileStream.Create(aFile, fmOpenRead);
try
aOutput := TMemoryStream.Create;
try
ObjectResourceToText(aInput,aOutput);
aOutput.Position:=0;
aStr:=TStringList.Create;
try
aStr.LoadFromStream(aOutput);
// aStr.LoadFromStream(ainput);
Result:=aStr.Text;
finally
aStr.Free;
end;

finally
aOutput.Free;
end;
finally
aInput.Free;
end;
end;
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EInvalidImage with message 'Invalid stream format'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
為什么老是報這個錯,請高手指點
 
function TForm1.dfmFileLoad(const aFile: String): string;
var
aInput : TStream;
aOutput : TStream;
aStr : TStrings;
begin
Result := '';
aInput := TFileStream.Create(aFile, fmOpenRead);
try
aOutput := TMemoryStream.Create;
try
ObjectResourceToText(aInput,aOutput);
aOutput.Position:=0;
aStr:=TStringList.Create;
try
aStr.LoadFromStream(aOutput);
// aStr.LoadFromStream(ainput);
Result:=aStr.Text;
finally
aStr.Free;
end;

finally
aOutput.Free;
end;
finally
aInput.Free;
end;
end;
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EInvalidImage with message 'Invalid stream format'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
為什么老是報這個錯,請高手指點
 
TO:update
TO:update
 
大约是这样吧,另外Delphi6,还是7之后DFM文件默认是以文本方式存储的

try
aOutput := TMemoryStream.Create;
try
aInput.ReadResHeader; //这里少了这个
ObjectResourceToText(aInput,aOutput);
 
Delphi自带的Example:

function ComponentToString(Component: TComponent): string;
var
BinStream:TMemoryStream;
StrStream: TStringStream;
s: string;
begin
BinStream := TMemoryStream.Create;
try
StrStream := TStringStream.Create(s);
try
BinStream.WriteComponent(Component);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, StrStream);
StrStream.Seek(0, soFromBeginning);
Result:= StrStream.DataString;
finally
StrStream.Free;

end;
finally
BinStream.Free
end;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := ComponentToString(Self);
end;
 
我是想把里面的
Caption = #32593#32476#25968#25454#21253#25429#33719#22120
變成中文字
 
后退
顶部