Playing Sound operations in BCB

Source: Internet
Author: User

Author
Li Jin

Work Unit
Yantai South Street Construction Bank Ministry of Science and Technology

 
Various resources are often used in programming. For example, you want to change your own icons for your program, use some lively cursors to increase interest, play some sound and animation files, or use a program written by others to implement a function. We often want to put these resources directly in the EXE file to form a separate executable file, which leads to a problem of how to access and use these resources at runtime.

---- During compilation, this requires a file, a resource definition file ending with. RC.

---- 1. RC file

---- The RC file is a text file. Its format is very simple and is defined as follows:

---- Resource Identifier Resource Type resource path

---- The following RC file defines a sound resource, two cursor resources, three ICON resources, and one 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 written RC file to you
Project. You can also manually compile it into a binary resource file (. Res file) that is directly used by the program ). In bcb3.0, you can use the command line: brcc32 src/downloadfiles/a/2001-12-05/1. RC src/downloadfiles/a/2001-12-05/1. Res. (BR>
---- II. resource usage

---- The following describes the usage of various resources in sequence based on the simplicity and complexity of use. It should be noted that the usage described below is the same in other compiling environments (BC, Vc, etc.

---- 1: Create a new project
---- Start bcb3.0 and choose File> New> application to create a new project.

---- Add the src1.rc file in project-> Add to project. Of course, the sound, cursor, and Icon files should all exist.

---- At this moment, we have an empty form (Form ).

---- 2: resources that can be directly accessed using Windows API functions include:

Loadicon ()
Cursor loadcursor ()
Acceleration table loadaccelerators ()
Bitmap ()
Menu loadmenu ()
String loadstring ()

---- The first five API functions use the same method, with two parameters.

---- The first parameter indicates the storage location of the resource, and the second parameter indicates the resource ID in the RC file.

---- In addition to the two parameters, the string loadstring also has two parameters, indicating the address and size of the string buffer.

---- The following section demonstrates the use of icons and optical icons.

---- Place a button on the form and add the following code to its onclick event:

Void _ fastcall tform1: button1click (tobject * sender)
{
// Change the cursor shape to your own:
Screen-> cursors [crdefault] = loadcursor (hinstance, "C1 ");
// Change the icon to the custom one:
Icon = new ticon ();
Icon-> handle = loadicon (hinstance, "I1 ");
Application-> icon = icon;
}
---- The hinstance indicates that the resource is located in the execution file. After compilation and execution, click the button to change the cursor and icon to a new one.

---- 3: resources that can be used through windows APIs
---- You can use Windows API functions to play audio, animation, and other files. However, unlike the above resources, some steps need to be followed. Call the findresource, loadresource, and lockresource functions in sequence to play a WAV audio file.

---- (Of course, independent sound files can be played directly at runtime. We will discuss how to compile a WAV file into an EXE file)

---- Place the second button on the form and add the following code to its onclick event:

Void _ fastcall tform1: button2click (tobject * sender)
{
// Define the resource block
Char * wav_handle;
// Load the wav file
Hrsrc H = findresource (hinstance, "S1", "WAV ");
Hglobal H1 = loadresource (hinstance, H );
Wav_handle = (char *) lockresource (H1 );
// Play the wav file. As wav files are loaded in the memory,
The sndplaysound function must use the snd_memory parameter.
Sndplaysound (wav_handle, snd_memory | snd_sync );
}

---- After compilation and execution, click the button to play a sound.

---- Animation and other files are used in a way similar to WAV Files. (BCB provides a tanimate control to play silent AVI files)

---- 4: resources that cannot be used directly through windows APIs
---- Such resources cannot be directly accessed and executed by Windows APIs. However, we can use it in a flexible way.

------The following example shows how to use pkunzip.exe. The idea is as follows: when the program runs, extract pkunzip.exe from the EXE file, put it in a temporary directory, and run it with ShellExecute.

---- Place the third button on the form and put two edits to input the pkunzip.exe parameter.

Void _ fastcall tform1: button3click (tobject * sender)
{
Char exefile [100], tmppath [100];
Unsigned long ret;
// Check whether pkunzip.exe already exists
Gettemppath( 100, tmppath );
Strcpy (exefile, (ansistring (tmppath) +
Ansistring ("// pkunzip.exe"). c_str ());
Ret = getfileattributes (exefile );
If (ret = 0 xffffffff)
{// Exists, then pkunzip.exe is separated
Tresourcestream & rs = * New tresourcestream (INT) hinstance, ansistring ("Unzip"), "exefile ");
Rs. savetofile (ansistring (exefile ));
Delete & RS;
}
// Execute pkunzip.exe
// Edit1-> text and edit2-> text are the ZIP file names and target file directories entered at runtime.
ShellExecute (hinstance, "open", exefile, ansistring ("-d") + edit1-> text + "" + edit2-> text ). c_str (), tmppath, sw_hide );
Application-> MessageBox ("decompressed", "OK", idok );
}

---- This method is actually to exchange time and space for convenience and has a certain reference value. For example, the free distribution version of dynadoc compresses the real execution program and stores it in a "shell" with the decompression function. During running, the program is first run as a shell, extract the real execution program to the temporary directory, and then run it. If your program contains a large number of BMP and WAV Files, try to make your program lose a lot of weight.

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.