Add custom sound (PlaySound function usage) and vcplaysound to the VC ++ Program

Source: Internet
Author: User

Add custom sound (PlaySound function usage) and vcplaysound to the VC ++ Program

In VC ++ programming, we can add music to our program. For example, when we press a button or start the program, we can play a short piece of music.

This function uses the following functions:

 

BOOL PlaySound (LPCSTR pszSound, HMODULE hwnd, DWORD fdwSound );

Parameter definition:

The pszSound parameter specifies the string for playing the sound (usually the absolute path. If you copy the sound file to the path where the executable file is located, you can directly use the sound file name ), this parameter can be the name of the WAVE file, the name of the WAV resource, the pointer to the audio data in the memory, or WIN in the system registry. system Event sound defined in INI. If this parameter is NULL, the playing sound is stopped.

The hwnd parameter is the application instance handle. It must be set to NULL unless pszSound points to a resource identifier (fdwSound is defined as SND_RESOURCE.

The fdwSound parameter is a combination of flags, as shown in the following table. If the call succeeds, the function returns TRUE; otherwise, FALSE.


Flag:

SND_APPLICATION

Play 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 a predefined sound identifier.

SND_ASYNC

Play the sound asynchronously. The PlaySound function returns the result immediately after the playing starts.

SND_FILENAME

The pszSound parameter specifies the WAVE file name.

SND_LOOP

The replay sound must be used with the SND_ASYNC flag.

SND_MEMORY

Play the sound loaded into the memory. In this case, pszSound is a pointer to the sound data.

SND_NODEFAULT

Do not play the default sound. If this flag is not displayed, PlaySound will play the default sound when no sound is found.

SND_NOSTOP

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

SND_NOWAIT

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

SND_PURGE

Stop all sounds related to the call task. If the parameter pszSound is NULL, all sound is stopped. Otherwise, the sound specified by pszSound is stopped.

SND_RESOURCE

The pszSound parameter is the identifier of the WAVE resource, and the hmod parameter is used.

SND_SYNC

The PlaySound function returns the synchronous playback sound after playback.

SND_SYSTEM

 

When using the PlaySound function, you must add the following after # include <windows. h> (Note: it cannot be added before ):

1

2

# Include <mmsystem. h>

# Pragma comment (lib, "WINMM. LIB") // You can also set it in the linker.

At this point, we can add this statement to what we want, for example, the button response function, or the create statement of a main framework program.

Function (or initial function ).

When using this function, we want the function to return, but the sound is still playing. In this case, the third parameter fdwSound must be set to asynchronous, that is, SND_ASYNC, at the same time, because our first parameter specifies the path of the wave file, we also need to add a flag:SND_FILENAMETo specify the file path. The Code is as follows:

 

Int CPassword: OnCreate (maid) {if (CDialogEx: OnCreate (maid) =-1) return-1; // TODO: add your dedicated creation code if (sign = 0) {CSplash wndSplash; // create the wndSplash instance of the startup window class. create (IDB_SCREEN_BITMAP); wndSplash. centerWindow (); wndSplash. updateWindow (); // send WM_PAINT Sleep (3000); wndSplash. destroyWindow (); // destroy the initial 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.

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.