Utilization of various resources in BC ++ Builder 3.0

zhaozj2021-02-11  180

The use of various resources in BC Builder 3.0 Li Jinguan Yantai South Street CCB Technology Department is often used in the process of writing procedures. For example, if you want to change your own icon, use some lively cursors to increase interest, play some sounds and animation files, or use someone to write a good program to implement a feature. We tend to put these resources directly into the exe file to form a separate executable, which exists on how to access and use these resources at runtime. ---- During the compilation, this is to use a file, and the resource definition file ended in .rc. - - 1: RC file ---- RC file is a text file, its format is simple, defined as follows: ---- Resource Identifier Resource Type Resource Path ---- The following RC file definition A voice resource, two cursor resources, three icons resources, and an EXE file resource. Src1.rc: S1 WAV WAV1.WAV C1 CURSOR CURSOR1.CUR CUR CUR CUR CUR CURSOR CURSOR2.CUR I1 icon icone1.ico I2 icon icone2.ico i3 icon icone3.ico unzip EXEFILE PKUNZIP.EXE

---- You can add a write RC file to your Project. You can also manually compile it into binary resource files (.res file) for use directly. In BCB3.0, you can use the command line: BRCC32 SRC1.RC Src1.RES. ---- 2: Use of resources ---- Next, according to the extent of use, the use of various resources will be described in sequence. It should be noted that the method described below is true in other compilation environments (BC, VC, etc.). ---- 1: Create a new project - start BCB3.0, select File-> New-> Application to establish a new project. ---- In Project-> Add to Project, add a written SRC1.RC file. Of course, those sounds, cursors, and icon files should exist. ---- At this time, we have an empty form (Form). ---- 2: Resources that can be accessed directly with the Windows API function: Icon Loadicon () Cursor loadcursor () Accelerator table loadAccelerators () bitmap loadbitmap () menu loadMenu () string loadString ()

---- The first five API functions use methods, with two parameters. ---- The first parameter indicates the storage of resources, the second parameter is the identity of the resource in the RC file. ---- String LoadString In addition to these two parameters, there are two parameters, indicating the address and size of the string buffer. ---- A small segment of the following demonstrate the use of icons and cursors. ---- Place a button on the Form, add the following code in its OnClick event: void __fastcall tform1 :: button1click (TOBJECT * Sender) {// Transfer the cursor shape to your own defined: Screen-> Cursors [CRDEFAULT ] = LoadCursor (Hinstance, "C1"); // Change the icon to own defined: icon = new ticon (); icon-> handle = loading; application-> icon = icon; ---- The Hinstance indicates that the resource is the execution file. After compiling execution, click on the button, the cursor and icon will be replaced with new. ---- 3: You can play with the Windows API function to play with the resource used by the Windows API --- For files, animations, and other files. However, it is not like the above resources, you need to follow a certain step. As shown below, call the FindResource, LoadResource, LockResource function in turn to play a WAV sound file. ---- (Of course, the independent sound file can be played directly at runtime. We discuss the situation to compile the WAV file into the exe file) ---- Place the second button on the Form, onclick Add the following code to the event: void __fastcall tform1 :: button2click (TOBJECT * Sender) {// Define resource block char * wav_handle; // Load WAV file HRSRC H = FindResource (Hinstance, "S1", "WAV"); HGlobal H1 = loading, h); wav_handle = (char *) LockResource (h1); // Play the WAV file. Since the WAV file is loaded in memory, the SNDPLAYSOUND function is used to use the SND_MEMORY parameter SNDPLAYSOUND (WAV_HANDLE, SND_MEMORY | SND_SYNC);

---- After compiling execution, click on this button to play a sound. ---- For files such as animation, use the way similar to the WAV file. (BCB provides a TANIMATE control, you can play a silent avi file) ---- 4: Resources that cannot be used directly through the Windows API - "This resource cannot be directly accessed and executed by the Windows API. However, we can use it in a variety of ways. ---- The following demonstrates how to use pkunzip.exe, the idea is as follows: The program is running, and PKunzip.exe is separated from the EXE file, put it in the temporary directory, and execute it with shellexecute (). ---- Place the third button on the Form, place two edits, used to enter PKunzip.exe parameters. Void __fastcall tform1 :: Button3Click (TOBJECT * Sender) {char Exefile [100], TMPPATH [100]; unsigned long return; // check if pkunzip.exe has GetTemppath (100, tmpath); struffpy (ansistring (ANSISTRING TMPPATH) ANSISTRING ("// pkunzip.exe"))) .c_str ()); RET = getFileAttributes (EXEFILE); if (RET == 0xfffffffff) {// does not exist, separate pkunzip.exe TResourceStream & RS = * New TresourceStream ((int) Hinstance, Ansistring ("unzip"), "exefile"); rs.savetofile (ANSISTRING (EXEFILE)); delete &} // execute pkunzip.exe // edit1-> text and edit2-> Text is the ZIP file name and target file directory input to the runtime. Shellexecute (Hinstance, "Open", EXEFILE, ANSISTISTRING ("- D" " Edit2-> text) .c_str (), TMPPATH, SW_HIDE); Application-> MessageBox (" Decompression completion "," OK ", IDOK);} ---- This method is actually used for time and space to exchange convenience, there is a certain reference value. For example, Dynadoc's free distribution versions are to compress the real execution program and place it in a "shell" with decompression. When running, the first is "shell" to run, unpack the real execution program to the temporary directory, and then run it. If your program contains a lot of BMP, WAV files, you may wish to make your program a lot of weight loss.

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

New Post(0)