• As seen in the previous example and by perusing the Visual Basic literature,
only one sound is available in Visual Basic - Beep. Not real exciting. By using
available DLL’s, we can add all kinds of sounds to our applications.
• A DLL routine like the Visual Basic Beep function is MessageBeep. It
also beeps the speaker but, with a sound card, you can hear different kinds of
beeps. Message Beep has a single argument, that being an long integer that describes
the type of beep you want. MessageBeep returns a long integer. The usage syntax
is:
Dim BeepType As Long, RtnValue as Long
.
.
.
RtnValue = MessageBeep(BeepType)
• BeepType has five possible values. Sounds are related to the four possible
icons available in the Message Box (these sounds are set from the Windows 95 control
panel). The DLL constants available are:
MB_ICONSTOP - Play sound associated with the critical icon
MB_ICONEXCLAMATION - Play sound associated with the exclamation icon
MB_ICONINFORMATION - Play sound associated with the information icon
MB_ICONQUESTION - Play sound associated with the question icon
MB_OK - Play sound associated with no icon
Quick Example 6 - Adding Beeps to
Message Box Displays
We can use MessageBeep to add beeps to our display of message boxes.
1. Start a new application. Add a text box and a command button.
2. Copy and paste the Declare statement for the MessageBeep function to the General
Declarations area. Also, copy and paste the following seven constants (we need
seven since some of the ones we use are equated to other constants):
Private Declare Function MessageBeep Lib
"user32" (ByVal wType As Long) As Long
Private Const MB_ICONASTERISK = &H40&
Private Const MB_ICONEXCLAMATION = &H30&
Private Const MB_ICONHAND = &H10&
Private Const MB_ICONINFORMATION = MB_ICONASTERISK
Private Const MB_ICONSTOP = MB_ICONHAND
Private Const MB_ICONQUESTION = &H20&
Private Const MB_OK = &H0&
3. In the above constant definitions, you will have to change the word Public
(which comes from the text viewer) with the word Private.
4. Use this code to the Command1_Click event.
Private Sub Command1_Click()
Dim BeepType As Long, RtnValue As Long
Select Case Val(Text1.Text)
Case 0
BeepType = MB_OK
Case 1
BeepType = MB_ICONINFORMATION
Case 2
BeepType = MB_ICONEXCLAMATION
Case 3
BeepType = MB_ICONQUESTION
Case 4
BeepType = MB_ICONSTOP
End Select
RtnValue = MessageBeep(BeepType)
MsgBox "This is a test", BeepType, "Beep Test"
End Sub
5. Run the application. Enter values from 0 to 4 in the text box and click the
command button. See if you get different beep sounds.