當前位置:最新電影網 - qq音樂 - 如何在自己的VB程序中添加背景音樂?

如何在自己的VB程序中添加背景音樂?

不用ActiveX控件也能播放聲音文件

講壹下代碼保存至module

Option Explicit

Public Declare Function sndPlaySound Lib "winmm.dll" Alias _

"sndPlaySoundA" (ByVal lpszSoundName As String, _

ByVal uFlags As Long) As Long

Const SND_SYNC = &H0

Const SND_ASYNC = &H1

Const SND_NODEFAULT = &H2

Const SND_LOOP = &H8

Const SND_NOSTOP = &H10

接下來把下面的代碼粘貼到妳想播放聲音文件的地方,比如放在命令按鈕的Click事件中

Dim sFlags As Long

sFlags = SND_ASYNC Or SND_NODEFAULT

sndPlaySound "FileName.Wav", sFlags

把上面的FileName.Wav換成妳想要播放的聲音文件的完整路徑及文件名就行了。

如果妳僅僅是想播放壹些Windows常用的聲音,比如說退出Windows的聲音,有壹種更簡單的寫法

sndPlaySound "SystemExit",sFlags

將SystemExit換成下面這些參數,試試是什麽聲音?

SystemStart

SystemExit

SystemDefault

SystemQuestion

SystemAsterisk

SystemExclamation

SystemHand

下面是sFlags所用參數的說明:

參數 說明

SND_SYNC Plays the WAV file specified and returns only when the sound has stopped playing

SND_ASYNC Plays the WAV file an continues after the sound has started playing.

SND_NODEFAULT Do not play the default sound if the if the WAV file is not found.

SND_LOOP Plays the WAV file continuously until sndPlaySound is called again. Remember to put SND_ASYNC in the sFlag variable as well. Else your application will lock-up. To stop playing the wave file set the "FileName.Wav" to Null instead.

SND_NOSTOP Return to the beginning of the WAV file if it's already playing.