20th Chapter-Development Delphi Object Type Data management function (five) (1)

Source: Internet
Author: User

20.3.1.1 the process of writing DFM documents: Writecomponentresfie

The process has two parameters, filename and instance. The filename parameter specifies the DFM file name to write, and the instance parameter is a tcomponent type that specifies the name of the part to write, typically a subclass of the Tform object. This procedure writes the instance part and all parts owned by it to the DFM file.

The significance of this process is that you can create the Delphi form part and insert the part in the form, and the function will write the form to the DFM file, which supports the reusability of the dynamic DFM file.

The procedures for this procedure are as follows:

Procedure Writecomponentresfile (const filename:string; Instance:tcomponent);

Var

Stream:tstream;

Begin

Stream: = Tfilestream.create (FileName, fmcreate);

Try

Stream.writecomponentres (Instance.classname, Instance);

Finally

Stream.free;

End

End

function, the file is created with FileStream, and the instance is written to the stream using the Writecomponetres method of the Stream object.

20.3.1.2 read the DFM file function: Readcomponentresfile

The Readcomponentresfile function has two parameters, filename and instance. The filename parameter specifies that the DFM filename be read, and the instance parameter specifies the part to be read from the DFM file. The function will instance and all the parts it owns from the DFM file and return the part.

The significance of this function is to support the reusability of DFM files with the use of the writecomponentresfile process.

The procedure for this function is this:

function readcomponentresfile (const filename:string; Instance:tcomponent):

Tcomponent;

Var

Stream:tstream;

Begin

Stream: = Tfilestream.create (FileName, Fmopenread);

Try

Result: = Stream.readcomponentres (Instance);

Finally

Stream.free;

End

End

Use the FileStream object in the program to open the DFM file specified by filename, then read the instance with the Readcomponentres method of the Stream object, and read the result as the return value of the function.

20.3.1.3 read the parts in Delphi application resources

function Internalreadcomponentres can read the parts in the Delphi application resource. Delphi's DFM files are embedded in the application's resources after the program has been compiled, and the format has changed, that is, the resource file head is missing.

The Tresourcestream object, which is manipulating data on a resource medium, has been described in the first section. function Internalreadcomponentres with Tresourcestream. The procedure is this:

function internalreadcomponentres (const resname:string;

var instance:tcomponent): Boolean;

Var

Hrsrc:thandle;

Begin {Avoid the occurrence of "Eresnotfound" exception events}

HRSRC: = FindResource (HInstance, Pchar (resname), rt_rcdata);

Result: = hrsrc <> 0;

If not result then Exit;

FreeResource (HRSRC);

With Tresourcestream.create (HINSTANCE, Resname, Rt_rcdata) do

Try

Instance: = Readcomponent (Instance);

Finally

Free;

End

Result: = True;

End

HINSTANCE is a global variable defined by Delphi VCL that represents the handle of the current application. The function uses the resource access API function FindResource to determine whether there are resources described by Resname. FreeResource is called in the function because of the findresource and so on in the Tresourcestream creation process. The last function calls the Readcomponent method of the Stream object to read the part. Because the instance of a function is a parameter of the Var type, you can access the instance and get the part read out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.