Mount a text file to a resource file in Delphi

Source: Internet
Author: User

How to mount an animation file (AVI), a bitmap file (Bitmap), a cursor file (cursor), an icon file (icon), a sound file (wave), and a string to a res resource file, there are already a lot of articles (see: http://www.why100000.com/_articles/index.asp? Tab = tabdelphi & remark = computer language-Delphi ). However, there are not many materials for loading text files into resource files.
In fact, we can add a text file to the resource file as a custom resource type. The procedure is as follows:

(1) Definition and generation of resource files:
Edit the resource definition file Myres. RC with the following content:
Html1 mytext test1.htm
Html2 mytext test2.htm
Then, when Myres. RC is added to the project, the statement {$ R 'myrec. res' 'myrec. RC '} is automatically added at the beginning of the prj file '}. In the future, the resource file myrec. Res will be automatically generated during program compilation, and resources will be added to the EXE file. Here, mytext is our custom resource type. html1 and html2 are the resource identification names of the corresponding text files test1.htm and test2.htm. In the future, we will use this to distinguish different resources when calling resources. Once an EXE file is generated, myrec. RC, myrec. Res, test1.htm, and test2.htm are useless and do not need to be released together with the EXE file.

(2) Resource call:
A piece of code is used to describe (debugging passed in Delphi7.0), mainly involving operations in the memory stream. tresourcestream. Create () Is the function for creating a stream. Resources can be directly restored to a text file. You can also read all streams into the Buf array, use the stream read method, and assign the Buf value to the string variable.
Procedure tform1.button1click (Sender: tobject );
VaR
Res: tresourcestream;
Buf: array [0 .. 10000] of char;
Shtml: string;
Begin
Res: = tresourcestream. Create (hinstance, 'html1', pchar ('mytext '));

// Save the resource as a file, that is, restore the file
Res.savetofile('text1.htm ');

// You can also perform stream operations to extract the content of a text file to a string variable.
Res. Read (BUF, res. size );
Shtml: = Buf; // convert the pchar type to the string type
Showmessage (shtml); // For the showmessage function, the showmessage (BUF) is also correct.
Res. Free;
End;

Similar to the resources used to generate text files, we can add MP3, RM, mwv, Flash, JPG, GIF, and other multimedia resources to the resource files and compile them into the EXE files for use.

-------------------------------------------------------
My other articles are on my personal website:
Why 100,000 computer learning networks: http://www.why100000.com

Zhang Qing zhangking@hotmail.com, QQ: 9365822
Http://why100000.com
Http://sogo99.com
2005.7.28

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.