200分,请各位帮忙把RichEdit的内容(有图片,也有文字)保存到一结构体文件中.求各位快点帮忙想想办法.(100分)

  • 主题发起人 主题发起人 hxiaomin888
  • 开始时间 开始时间
H

hxiaomin888

Unregistered / Unconfirmed
GUEST, unregistred user!
该自定义格式文件定义如下.<br>type<br> &nbsp;TSoftDetailRecord=packed record<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Autoid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Classid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftName:String[100];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftPath:String[200];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description:Pointer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>我要把RichEdit的内容保存到Description中,并可以再次读出来.如果分不够的话,我可以再加一百分!只求各位能快点帮我想想办法.
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3566065
 
怎么就没人知道呢?还是其它原因不愿回答?
 
我觉得是否可以这样:把RichEdit的内容保存到流内。然后让指针指向流 。
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, ComCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;RichEdit1: TRichEdit;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;procedure SaveREFile;<br> &nbsp; &nbsp;procedure LoadREFile;<br> &nbsp;end;<br><br> &nbsp;TSoftDetailRecord=packed record<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Autoid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Classid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftName:String[100];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftPath:String[200];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description:Pointer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;SaveREFile;<br>end;<br><br>procedure TForm1.LoadREFile;<br>var<br> &nbsp;tmpStream, tmpFStream: TStream;<br> &nbsp;tmpRd: TSoftDetailRecord;<br>begin<br> &nbsp;tmpStream := TMemoryStream.Create;<br> &nbsp;tmpFStream := TFileStream.Create('c:/1.dat', fmOpenReadWrite);<br> &nbsp;try<br> &nbsp; &nbsp;tmpFStream.Read(tmpRd, SizeOf(tmpRd));<br> &nbsp; &nbsp;tmpStream.CopyFrom(tmpFStream, Integer(tmpRd.Description));<br> &nbsp; &nbsp;tmpStream.Position := 0;<br> &nbsp; &nbsp;RichEdit1.Lines.LoadFromStream(tmpStream);<br> &nbsp;finally<br> &nbsp; &nbsp;tmpFStream.Free;<br> &nbsp; &nbsp;tmpStream.Free;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.SaveREFile;<br>var<br> &nbsp;tmpStream, tmpFStream: TStream;<br> &nbsp;tmpRd: TSoftDetailRecord;<br>begin<br> &nbsp;tmpStream := TMemoryStream.Create;<br> &nbsp;tmpFStream := TFileStream.Create('c:/1.dat', fmOpenReadWrite or fmCreate);<br> &nbsp;try<br> &nbsp; &nbsp;RichEdit1.Lines.SaveToStream(tmpStream);<br> &nbsp; &nbsp;Integer(tmpRd.Description) := tmpStream.Size;<br> &nbsp; &nbsp;tmpFStream.Write(tmpRd, SizeOf(tmpRd));<br> &nbsp; &nbsp;tmpStream.Position := 0;<br> &nbsp; &nbsp;tmpFStream.CopyFrom(tmpStream, tmpStream.Size);<br> &nbsp;finally<br> &nbsp; &nbsp;tmpFStream.Free;<br> &nbsp; &nbsp;tmpStream.Free;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;LoadREFile;<br>end;<br><br>end.
 
我先试试,OK后再给分.
 
to nicai_wgl<br>请问:<br>RichEdit1.Lines.SaveToStream(tmpStream);<br> &nbsp; &nbsp;Integer(tmpRd.Description) := tmpStream.Size;<br>你的这两句就算把RichEdit1的数据保存到tmpRd.Description了吗?能帮我解释一下吗?这里我还不太明白.
 
to nicai_wgl<br>请问,以下四句中,到底是哪句的作用是保存tmpRD到文件?(不好意思,我很菜的)<br> &nbsp; &nbsp;Integer(tmpRd.Description) := tmpStream.Size;<br> &nbsp; &nbsp;tmpFStream.Write(tmpRd, SizeOf(tmpRd));<br> &nbsp; &nbsp;tmpStream.Position := 0;<br> &nbsp; &nbsp;tmpFStream.CopyFrom(tmpStream, tmpStream.Size);
 
呵呵,我给你注释一下:<br> &nbsp; &nbsp;Integer(tmpRd.Description) := tmpStream.Size; &nbsp;//赋给Description RichEdit1的数据的长度<br> &nbsp; &nbsp;tmpFStream.Write(tmpRd, SizeOf(tmpRd)); &nbsp;//把结构写入文件<br> &nbsp; &nbsp;tmpStream.Position := 0; &nbsp;<br> &nbsp; &nbsp;tmpFStream.CopyFrom(tmpStream, tmpStream.Size); //把RichEdit1的数据从内存流里写入文件
 
谢谢nicai_wgl<br>能否再问一下,如果我要添加多条记录,你这种方法行吗?能否再请你提示一下该如何做呢?(问得太多,请别见怪!)
 
请nicai_wgl兄到http://www.delphibbs.com/delphibbs/dispq.asp?lid=3569839 领另外100分.
 
刚才没明白你记录的意思,不好意思,如果是多个结构要保存到一个文件,也没有问题,只是你读写函数要改一下。等下贴代码上来。
 
谢谢nicai_wgl兄对我的问题的一直关注.<br>我想在你的Button1Click事件中,按一次按钮就添加一条记录,那你的SaveREFile应该如何修改?<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;SaveREFile;<br>end;
 
我所说的记录就是:<br>因为我定义了一个数据结构:<br>type<br> &nbsp;TSoftDetailRecord=packed record<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Autoid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Classid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftName:String[100];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftPath:String[200];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description:Pointer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>var<br> &nbsp;tmpRD:TSoftDetailRecord<br>每保存一个tmpRD就是保存了一条记录.
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, ComCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;RichEdit1: TRichEdit;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;UpDown1: TUpDown;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp;public<br> &nbsp; &nbsp;procedure SaveREFile;<br> &nbsp; &nbsp;procedure LoadREFile(AutoID: Integer);<br> &nbsp;end;<br><br> &nbsp;TSoftDetailRecord=packed record<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Autoid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Classid:integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftName:String[100];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoftPath:String[200];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description:Pointer;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;SaveREFile;<br>end;<br><br>procedure TForm1.LoadREFile(AutoID: Integer);<br>var<br> &nbsp;tmpStream, tmpFStream: TStream;<br> &nbsp;tmpRd: TSoftDetailRecord;<br>begin<br> &nbsp;tmpStream := TMemoryStream.Create;<br> &nbsp;tmpFStream := TFileStream.Create('c:/1.dat', fmOpenReadWrite);<br> &nbsp;try<br> &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp;tmpFStream.Read(tmpRd, SizeOf(tmpRd));<br> &nbsp; &nbsp; &nbsp;if tmpRd.Autoid &lt;&gt; AutoID then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if tmpFStream.Size &gt; (tmpFStream.Position + Integer(tmpRd.Description)) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmpFStream.Seek(Integer(tmpRd.Description), soCurrent)<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('找不到这条记录!');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else break;<br> &nbsp; &nbsp;until tmpRd.Autoid = AutoID;<br> &nbsp; &nbsp;//读入数据<br> &nbsp; &nbsp;tmpStream.CopyFrom(tmpFStream, Integer(tmpRd.Description));<br> &nbsp; &nbsp;tmpStream.Position := 0;<br> &nbsp; &nbsp;RichEdit1.Lines.LoadFromStream(tmpStream);<br> &nbsp;finally<br> &nbsp; &nbsp;tmpFStream.Free;<br> &nbsp; &nbsp;tmpStream.Free;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.SaveREFile;<br>var<br> &nbsp;tmpStream, tmpFStream: TStream;<br> &nbsp;tmpRd: TSoftDetailRecord;<br>begin<br> &nbsp;tmpStream := TMemoryStream.Create;<br> &nbsp;if not FileExists('c:/1.dat') then<br> &nbsp; &nbsp;tmpFStream := TFileStream.Create('c:/1.dat', fmOpenReadWrite or fmCreate)<br> &nbsp;else tmpFStream := TFileStream.Create('c:/1.dat', fmOpenReadWrite);<br> &nbsp;try<br> &nbsp; &nbsp;RichEdit1.Lines.SaveToStream(tmpStream);<br> &nbsp; &nbsp;tmpRd.Autoid := StrToInt(Edit1.Text);<br> &nbsp; &nbsp;Integer(tmpRd.Description) := tmpStream.Size;<br> &nbsp; &nbsp;tmpFStream.Seek(0, soEnd); //移到文件最后<br> &nbsp; &nbsp;tmpFStream.Write(tmpRd, SizeOf(tmpRd));<br> &nbsp; &nbsp;tmpStream.Position := 0;<br> &nbsp; &nbsp;tmpFStream.CopyFrom(tmpStream, tmpStream.Size);<br> &nbsp;finally<br> &nbsp; &nbsp;tmpFStream.Free;<br> &nbsp; &nbsp;tmpStream.Free;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;LoadREFile(StrToInt(Edit1.Text));<br>end;<br><br>end.
 
谢谢nicai_wgl,谢谢各位!
 

Similar threads

后退
顶部