定义那改一下
var
aInput  : TFileStream;
  aOutput : TMemoryStream;

解决方案 »

  1.   

    这是因为你的DFM文件本身就是文本的,不需要转换.
    注意Tools|Environment Options...
    在Preferences页的Form designer组的New forms as text选项,如果被选中了,此后创建的DFM就是文本的,这时不能用ObjectResourceToText去转换(也不需要).
    如果DFM文件创建时这个选项是取消的,这个DFM文件就可以用这段代码去转换.
    我把你的代码改了改:
    function dfmFileLoad(const aFile: String): String;
    var
      aInput  : TStream;
      aOutput : TStream;
      aStr    : TStrings;
      FileHead: SmallInt;//增加一个变量:文件的前两个字节.
    begin
      Result := '';
      aInput := TFileStream.Create(aFile, fmOpenRead);
      try
        aOutput := TMemoryStream.Create;
        try
          //下面几句是我加的.
          aInput.Position :=0;
          aInput.Read(FileHead,2);
          aInput.Position :=0;
          if FileHead = $0aff then//如果是二进制格式
            ObjectResourceToText(aInput,aOutput)
          else//如果是文本格式
            (aOutput as TMemoryStream).LoadFromStream(aInput);
          //上面几句是我加的   
          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;
      

  2.   

    文件格式不对。
    会不会是你的.dfm文件本来就是text格式?
      

  3.   

    多谢大家关注,接受plainsong(轻风) 的答案了,但出现了新的问题:
    1、我用的D6,根本没有New forms as text项,不知各位的如何;
    2、使用转化的原来目的是想查找出DFM里面的汉字,打开后中文显示有问题:
      举个例子,如果FORM的CAPTION设置为中文《中国人》的话,
     在用该方法打开FORM文件时是这样的
      Caption = #20013#22269#20154
    这该如何转换,还请赐教
      

  4.   

    D6中是在Designer页的Module creation options组.
      

  5.   

    呵呵,找到了,
    但是
    Caption = #20013#22269#20154这中间的到底是什么码,该如何转换为中文那?
      

  6.   

    1.Tools|Environment Options...
    选中在Designer页的Module creation options组的New forms as text选项
      

  7.   

    Unicode
    //请参考
    http://www.csdn.net/Expert/TopicView1.asp?id=711278嘻嘻嘻 还记得我吧! 嘻嘻嘻嘻
      

  8.   

    zswang(伴水)(* 嘻嘻 *) 的:DFM文件与标准文本文件转换 
    整理编辑:China ASP
      在Delphi可视化设计环境中,允许程序员在代码编辑器中以文本的方式浏览和修改DFM文件内容。当用File/Open命令直接打开DFM文件或者选择窗体设计窗口的弹出式菜单上的View as Text命令时,就会在编辑器中出现文本形式的信息。在一些资料中将这种文本形式称之为窗体设计脚本。Delphi提供的这种脚本编辑功能是对Delphi可视化设计的一大补充。当然这个脚本编辑能力是有限制的,比方说不能在脚本任意地添加和删除部件,因为代码和DFM脚本是紧密相连的,任意添加和修改会导致不一致性。但在动态生成的DFM文件中,就不存在这一限制。
      实际上,DFM文件内容是二进制数据,它的脚本是经过Delphi开发环境自动转化的,而且Delphi VCL中的Classes库单元提供了在二进制流中的文件DFM和它的脚本之相互转化的过程。它们是ObjectBinaryToText和ObjectTextToBinary、ObjectResourceToText和ObjectTextToResource。
      ObjectBinaryToText过程将二进制流中存储的部件转化为基于文本的表现形式,这样就可以用文本处理函数进行处理,还可以用文本编辑器进行查找和替代操作,最后可以将文本再转化成二进制流中的部件。
      ObjectTextToBinary过程执行的功能与ObjectBinaryToText相反,将TXT文件转换为二进制流中的部件,而且只要TXT文件内容的书写符合DFM脚本语法,ObjectTextToBinary可将任何程序生成的TXT文件转换为部件,这一功能也为DFM文件的动态生成和编辑奠定了基础。
      DFM文件与DFM脚本语言之间相互转换的任务由ObjectResourceToText和ObjectTextToResource两个过程完成。下面以我所拥有的Delphi编程资料作详细说明:
      ObjectResourseToText过程比较简单,如下:
       procedure ObjectResourceToText(Input,Output:TStream);
       begin
       Input.ReadResHeader;
       ObjectBinaryToText(Input,Output);
       end;
      ObjectTextToResource过程就比较复杂,因为DFM文件资源头中要包含继承标志信息,因此在调用ObjectTextToBinary后,就读取标志信息,然后写入资源头。
       procedure ObjectTextToResource(Input,Output:TStream);
       var
       Len:Byte;
       Tmp:Longint;
       MemoryStream:TMemoryStream;
       MemorySize:Longint;
       Header:array[0.79] of Char;
       begin
       MomoryStream:=TMemoryStream.Create;
        try
       ObjectTextToBinary(Input,MemoryStream);
       MemorySize:=MemoryStream.Size;
       FillChar(Header,SizeOF(Header),0);
      MemoryStream.Position:=SizeOf(Longint);{Skip header}
       MemoryStream.Read(Len,1);
       if Len and $F0=$F0 then
       begin
       if ffChildPos in TFilerFlags(Len and $F0) then
       begin
       MemoryStream.Read(Len,1);
       case TValueType(Len) of
       vaInt8:Len:=1;
       vaInt16:Len=2;
       vaInt32:Len=4;
       end;
       MemoryStream.Read(Tmp,Len);
        end;
       MemoryStream.Read(Len,1);
     end;
       MemoryStream.Read(Header[3],Len);
      StrUpper(@Header[3]);
       Byte((@Header[0])^):=$FF;
      Word((@Header[1])^):=10;
       Word((@Header[Len+4])^):=$1030;
      Longint((@Header[Len+6])^):=MemorySize;
       Output.Write(Header,Len+10);
      Output.Write(MemoryStream.Memory^,MemorySize);
    finally
      MemoryStream.Free;
      end;
    **************************
    如何在运行过程中将本窗体保存成一个文本格式的.dfm文件?
    zswang(伴水) (2001-11-21 9:52:59)  得0分 
    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; { ComponentToString }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; { StringToComponent }
     
    回复人: zswang(伴水) (2001-11-21 9:54:28)  得0分 
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Memo1.Text := ComponentToString(Self);
    end;
     
    回复人: zswang(伴水) (2001-11-21 9:58:13)  得0分 
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      StringToComponent(
    'object Label1: TLabel'#13#10 +
    '  Left = 232'#13#10 +
    '  Top = 56'#13#10 +
    '  Width = 26'#13#10 +
    '  Height = 13'#13#10 +
    '  Caption = #20320#22909'#13#10 +
    '  Font.Charset = GB2312_CHARSET'#13#10 +
    '  Font.Color = clRed'#13#10 +
    '  Font.Height = -13'#13#10 +
    '  Font.Name = #23435#20307'#13#10 +
    '  Font.Style = []'#13#10 +
    '  ParentFont = False'#13#10 +
    'end'#13#10, Label1);
    end;//要注册类
      

  9.   

    在新的公司上了两个半月的班,感觉比以前的公司好很多
    不过九月份可能要到深圳,这里不能发挥我的特长//请看看这个//也许对你更有帮助
    http://kingron.myetang.com/zsfunc0j.htm