"Mastering Delphi 6" study notes

zhaozj2021-02-11  212

Serialiation in Delphi

Familiar with MFC is about the virtual function of Serialize. In the MFC, if you want to read and save data with the Serialization mechanism, it is probably this:

Void CMYDOC :: Serialize (CARCHIVE & AR)

{

IF (ar.isstoring ())

{

Ar << mystring;

ar << myint;

}

Else

{

AR >> mystring;

AR >> Myint;

}

}

Is there a similar mechanism in Object Pascal? After contact with Treader and Twriter, I know that I found the answer.

There is no need to talk more, to look at an actual example. Put three Edit and two button on the Form, add an OpenDialog and Savedialog. code show as below:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

if Savedialog1.execute THEN

Serialize (Savedialog1.FileName, True);

END;

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

Begin

IF openDialog1.execute the

Serialize (OpenDialog1.FileName, False);

END;

Procedure TForm1.Serialize (const filename: string; bsave: boolean);

VAR

Reader: Treader;

Writer: twriter;

Stream: TfileStream;

Begin

IF Bsave the Begin

Stream: = TFileStream.create (filename, fmopenwrite or fmcreate);

Writer: = twriter.create (stream, 4096);

Writer.writestring (edit1.text);

Writer.writeInteger (strtoint (edit2.text));

Writer.writefloat (strtofloat (edit3.text));

Writer.free;

Stream.free;

end

Else Begin

Stream: = TFileStream.create (filename, fmopenread);

Reader: = Treader.create (stream, 4096);

Edit1.Text: = Reader.Readstring;

Edit2.text: = INTOSTR (Reader.Readinteger);

Edit3.Text: = floattostr (reader.readfloat);

Reader.Free;

Stream.free;

END;

END;

If you prefer, you can pack Serialize into a Virtual Method so that the implementation of the derived class is more concise.

Treader and Twriter not only read and write most of the standard data types in Object Pascal, but also read and write collection / list / variant these advanced types, and even read and write Perperties and Component. However, Treader / Twriter itself actually provides limited, most actual work is done by this very powerful class of TSTREAM. From Treader and TWRITER, you can see some methods that are specifically designed for Component. It is not difficult to guess, the Delphi development environment is likely to use Treader / TWRITER to write .DFM files and from .dfm files from .dfm files Read attribute values. The following example is also very interesting, its effect is completely equivalent to the View as text command in Form Designer:

VAR

Dfmbuf, TextBuf: TSTREAM;

Begin

DFMBUF: = TMEMORYSTREAM.CREATE;

Dfmbuf.writeComponent (Self);

TEXTBUF: = TMEMORYSTREAM.CREATE

Dfmbuf.seek (0, SOFROMBEGINNIN);

ObjectBinaryTotext (DFMBUF, TextBuf);

TextBuf.seek; 0, Sofrombeginning;

Memo1.lines.LoadFromstream (TextBuf);

Textbuf.free;

Dfmbuf.free;

You can even read the FORM information from the executable:

VAR

Dfmbuf, TextBuf: TSTREAM;

BUF: POINTER;

Begin

Dfmbuf: = TRESOURCESTREAM.CREATE (Hinstance, 'TFORM1', RT_RCDATA);

DFMBUF.POSITION: = 0;

TEXTBUF: = TMEMORYSTREAM.CREATE

Dfmbuf.seek (0, SOFROMBEGINNIN);

ObjectBinaryTotext (DFMBUF, TextBuf);

TextBuf.seek; 0, Sofrombeginning;

Memo1.lines.LoadFromstream (TextBuf);

Textbuf.free;

Dfmbuf.free;

END;

(Note: If the form is not TFORM1, then change the second parameter in the TRESOURSTREAM.CREATE sentence to the corresponding form class name.)

Read all this code, believe that you are doing a DFM viewer is not a distant thing!

转载请注明原文地址:https://www.9cbs.com/read-3848.html

New Post(0)