Add sound resources to the Visual C ++ resource file

Source: Internet
Author: User
This article describes how to add your own sound resources to the VC resource file so that your application can play the sound.

1. Use a text editor (such as NotePad) to open the resource file (. rc file)

Add your own sound Resources at the end, as shown in the following idw wave "c: // kav/sound // virus.wav"

Save the resource file.
2. Start another thread in the place where the sound is to be played in the application to play the sound:

AfxBeginThread (AFX_THREADPROC) sound, NULL, THREAD_PRIORITY_NORMAL );

3. Add a thread callback function

This section describes how to load resources. First, use the function AfxGetInstanceHandle () to obtain the handle of the instance, and then use the function FindResource to find its own sound resource.

HRSRC FindResource (
HMODULE hModule, // module handle
Lptstr lpName, // resource name
LPCTSTR lpType // resource type
)

After a sound resource is found, use LoadResource to add the resource.

HGLOBAL LoadResource (
HMODULE hModule, // module handle
HRSRC hResInfo // resource handle
);

Finally, lock the resource memory block and return the virtual memory address of the calibrated memory block. If the resource is successfully locked, the returned value points to the first byte at the beginning of the Resource:

LPVOID pv = LockResource ()

Note: If a problem occurs in any of the above four steps, the system returns and releases the corresponding memory. The next step is to load data based on the file data type.

UINT CPlaySoundView: sound (LPVOID pParam)
{
HINSTANCE h = AfxGetInstanceHandle ();
HRSRC hr = FindResource (h, "IDW", "WAVE ");
HGLOBAL hg = LoadResource (h, hr );
LPSTR lp = (LPSTR) LockResource (hg );
SndPlaySound (lp, SND_MEMORY | SND_SYNC );
FreeResource (hg );
Return 0;
}

 

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.