Utilization of various resources in C ++ Builder

xiaoxiao2021-03-06  54

Work unit: Yantai South Street CCB Ministry

Various resources are often used in the process of writing programs. 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 files define a sound resource, two cursor resources, three icons resources, and an EXE file resource.

SRC1.RC:

S1 WAV WAV1.WAV

C1 Cursor Cursor1.cur

C2 CURSOR CURSOR2.CUR

I1 icon icone1.ico

I2 icon icone2.ico

I3 icon icone3.ico

Unzip EXEFILE PKUNZIP.EXE

---- You can add the write RC file to you

Project (item). 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

---- Hereinafter, in order to explain the use of various resources in order to use the extensive use. It should be noted that the method described below is true in other compilation environments (BC, VC, etc.).

---- 1: Establish 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 Windows API functions:

Icon Loadicon ()

Cursor loadingcursor ()

Accelerator table loadingAccelerators ()

Bitmap loadingbitmap ()

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)

{

// Change the cursor shape to own definition:

Screen-> Cursors [crdefault] = loadingcursor (Hinstance, "C1");

// Change the icon to your definition: icon = new ticon ();

Icon-> Handle = Loadicon (Hinstance, I1 ");

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: Resources that can be used through Windows API

---- For files such as sound, animation, you can play with Windows API functions. 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 discussed here that compiling the WAV file into the exe file)

---- Place the second button on the Form, add the following code to its OnClick event:

Void __fastcall tform1 :: button2click (Tobject * Sender)

{

// Define the resource block

Char * wav_handle;

// Load WAV file

HRSRC H = FindResource (Hinstance, "S1", "WAV");

Hglobal H1 = LoadResource (Hinstance, H);

WAV_HANDLE = (char *) LockResource (H1);

// Play the WAV file. Since the WAV file is loaded in memory,

SndPlaySound function 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 Ret;

/ / Check if pkunzip.exe already exists

GetTemppath (100, TmpPath);

STRCPY (EXEFILE, (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 & RS;

}

// Execute pkunzip.exe

// edit1-> text and edit2-> text respectively entered the ZIP file name and target file directory.

Shellexecute (Hinstance, "Open", Exefile, Ansistring ("- D") Edit1-> Text " Edit2-> Text) .c_str (), tmp_, 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-73355.html

New Post(0)