Add a custom sound to VC + + programs (PlaySound function usage)

Source: Internet
Author: User

VC + + Programming, we can add music for our own programs, such as when we press a button or start the program, play a small piece of music.

This function uses the function:

BOOL PlaySound (LPCSTR pszsound, hmodule hwnd,dword fdwsound);

Parameter definition:

Parameter Pszsound is a string that specifies the sound to play (usually an absolute path, if the sound file is copied to the path of the executable file can be used directly with the sound file name), the parameter can be the name of the wave file, or the name of the WAV resource, or the memory of the sound data pointer, or a system event sound defined in the system registry win.ini. If this parameter is NULL, the sound that is playing is stopped.

The parameter HWND is an instance handle of the application, unless pszsound points to a resource identifier (that is, fdwsound is defined as Snd_resource), otherwise it must be set to NULL.

The parameter fdwsound is a combination of flags, as shown in the following table. The function returns true if successful, otherwise false is returned.


Sign:

Snd_application

Plays the sound using the association specified by the application.

Snd_alias

The Pszsound parameter specifies the alias of the system event in the registry or Win.ini.

snd_alias_id

The Pszsound parameter specifies the predefined sound identifier.

Snd_async

Plays the sound asynchronously, and the PlaySound function returns immediately after it starts playing.

Snd_filename

The Pszsound parameter specifies the wave file name.

Snd_loop

Repeats the sound and must be used with the SND_ASYNC flag.

Snd_memory

Plays a sound loaded into memory, at which point the pszsound is a pointer to the sound data.

Snd_nodefault

The default sound is not played, and if this flag is not available, PlaySound plays the default sound when no sound is found.

Snd_nostop

PlaySound does not interrupt the original sound to broadcast and immediately returns false.

Snd_nowait

If the driver is busy, the function does not play the sound and returns immediately.

Snd_purge

Stops all sounds related to the calling task. If the parameter pszsound is null, stop all sounds, otherwise stop pszsound the specified sound.

Snd_resource

The Pszsound parameter is the identifier of the wave resource, and the Hmod parameter is used.

Snd_sync

Plays the sound synchronously, and the PlaySound function returns after the play is finished.

Snd_system

Use PlaySound function, you need to #include <windows.h> Add the following (note: cannot be added in front):

1

2

#include <mmsystem.h>

#pragma comment (lib, "WINMM. LIB ")//can also be set in the linker

We can now add this statement to where we want it, for example, in the response function of a button, or a main frame program's Create

function (or in the initial function) (the function is executed when the function is started).

When using this function specifically, we want the function to return, but the sound is still playing, at which point the third parameter fdwsound to be set to asynchronous, namely:Snd_async, and since our first parameter indicates the path of the wave file, we also need to add a flag: Snd_filename to develop the file path. The specific code is as follows:

int cpassword::oncreate (lpcreatestruct lpcreatestruct) {if (cdialogex::oncreate (lpcreatestruct) = =-1) return-1;// TODO:  Add your dedicated create code here if (sign==0) {Csplash wndsplash;                 Create an instance of the Startup window class  wndsplash.create (Idb_screen_bitmap);  Wndsplash.centerwindow ();  Wndsplash.updatewindow ();          Send Wm_paint  Sleep (+);    Wndsplash.destroywindow ();//Destroy the splash screen window  sign++;<span style= "color: #ff0000;" ><strong> PlaySound (L "C:\\users\\zrl\\desktop\\1.wav", null,snd_filename| Snd_async); </strong></span>}return 0;}

Run the program to hear the sound we set.

Add a custom sound to VC + + programs (PlaySound function usage)

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.