How to Invoke MessageBeep API to Play System Alert .WAV Sounds

ID: Q110103


The information in this article applies to:


SUMMARY

This article describes how to invoke the MessageBeep API function to play the waveform sound associated with a given Windows system alert level.

This is useful for playing a sound such as when you display a message box with the MsgBox statement.


MORE INFORMATION

The sound for each Windows alert level is identified by an entry in the [sounds] section of the WIN.INI initialization file. You can use the Windows Control Panel to change this [sounds] section.

The MessageBeep API function returns control to Visual Basic immediately after queuing a sound. The Visual Basic program executes subsequent code while the MessageBeep sound plays asynchronously.

The MessageBeep API function accepts one parameter, which can have one of the following values:


Parameter Value       Meaning
---------------------------------------------------------------------------
-1                    Produces a standard beep sound by using the computer
                      speaker.
MB_ICONASTERISK       Plays the sound identified by the SystemAsterisk
                      entry in the [sounds] section of WIN.INI.
MB_ICONEXCLAMATION    Plays the sound identified by the SystemExclamation
                      entry in the [sounds] section of WIN.INI.
MB_ICONHAND           Plays the sound identified by the SystemHand
                      entry in the [sounds] section of WIN.INI.
MB_ICONQUESTION       Plays the sound identified by the SystemQuestion
                      entry in the [sounds] section of WIN.INI.
MB_OK                 Plays the sound identified by the SystemDefault
                      entry in the [sounds] section of WIN.INI. 

Example: How to Invoke MessageBeep API Function

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Double click Form1. Add the following to the Form Load event code:
    
       Sub Form_Load ()
          Const MB_ICONQUESTION = 32     ' Warning query. See CONSTANTS.TXT.
          Const MB_ICONEXCLAMATION = 48  ' Warning message.
          Const MB_YESNO = 4             ' Yes and No buttons
          MessageBeep MB_ICONEXCLAMATION      ' Plays waveform sound.
          MsgBox "Wow!", MB_ICONEXCLAMATION   ' Displays message box.
          MessageBeep MB_ICONQUESTION
          MsgBox "Yes or No?", MB_ICONQUESTION + MB_YESNO
          End
       End Sub
    
       NOTE: The MB_ICONQUESTION and MB_ICONEXCLAMATION values are the same for
       both the MessageBeep API function and the MsgBox statement. See the
       "Parameters for MsgBox Statement" section below.
     


  3. Choose (general) from the Object menu. Add the following Declare to the general declarations section:
    
       Declare Sub MessageBeep Lib "User" (ByVal wType As Integer)
     


  4. Start the program or press the F5 key. MessageBeep plays the appropriate sound waveform file as each message box displays.


Windows Sound Events Are Not Standardized

Windows version 3.1 allows you to assign waveform audio sounds to certain events through the Control Panel. These events are:
Default Beep
Exclamation
Windows Start
Windows Exit
Critical Stop
Question
Asterisk

System sounds are dependent upon the application in which they occur. To produce a sound, an application needs to notify Windows that a sound is to occur, and then tell Windows which system sound to play. The application will specify one of the seven default system sounds or any sound event that it has added to this list.

This means that you cannot add sound events to the default list and have an application play that sound, unless the application has been specifically written to call that sound event.

Additionally, applications for Windows have not standardized on when these sound events should occur. Therefore, one application may play the Default Beep sound when an error occurs while another application might play the Critical Stop sound.

Parameters for MsgBox Statement


Const MB_ICONSTOP = 16          ' Critical message; displays STOP icon.
Const MB_ICONQUESTION = 32      ' Warning query; displays ? icon.
Const MB_ICONEXCLAMATION = 48   ' Warning message; displays ! icon.
Const MB_ICONINFORMATION = 64   ' Information message; displays i icon.

Const MB_OK = 0                 ' OK button only
Const MB_OKCANCEL = 1           ' OK and Cancel buttons
Const MB_ABORTRETRYIGNORE = 2   ' Abort, Retry, and Ignore buttons
Const MB_YESNOCANCEL = 3        ' Yes, No, and Cancel buttons
Const MB_YESNO = 4              ' Yes and No buttons
Const MB_RETRYCANCEL = 5        ' Retry and Cancel buttons

Const MB_APPLMODAL = 0          ' Application Modal Message Box
Const MB_DEFBUTTON1 = 0         ' First button is default
Const MB_DEFBUTTON2 = 256       ' Second button is default
Const MB_DEFBUTTON3 = 512       ' Third button is default
Const MB_SYSTEMMODAL = 4096      'System Modal 

The above parameters for the MsgBox statement can also be found in any of the following sources:

Additional query words: 3.00


Keywords          : kbcode 
Version           : 3.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: June 24, 1999