如何在vb窗口中播放背景音乐?

一般通过MCI控件或MCI API实现。

我以前玩过一个小游戏,用的是API,音乐部分的代码如下:

私有声明函数mciSendString Lib“winmm . dll”别名“mciSendStringA”(ByVal lpstrCommand为String,ByVal lpstrReturnString为String,ByVal uReturnLength为Long,ByVal hwndCallback为Long)为Long

私有声明函数GetShortPathName Lib "kernel32 "别名" GetShortPathNameA "(ByVal lpszLongPath为String,ByVal lpszShortPath为String,ByVal cchBuffer为Long)长

String形式的私有sWin

私有sFail作为字符串

Private sStart作为字符串

Private sFight As String

字符串形式的公共字符串

私有函数get short name(ByVal slong filename As String)作为字符串

Dim lRetVal为Long,sShortPathName为String,iLen为Integer

模糊汉字一样长

短路径名=空格(255)

iLen = LenB(短路径名)

lRetVal = GetShortPathName(slong filename,sShortPathName,iLen)

Chinese character = LenB(StrConv(sshotpathname,vbfromnunicode))-Len(sshotpathname)

get short name = Left(sshotpathname,lRetVal - ChineseCharacter)

结束功能

私有子播放(作为字符串)

mciSendString "关闭媒体",vbNullString,0,0

mciSendString“打开”和。get short name & amp;"别名媒体",vbNullString,0,0

mciSendString "播放媒体",vbNullString,0,0

末端接头

公共子播放歌曲(名称为字符串)

form 1 . TMR audio . enabled = True

选择案例名称

案例“失败”

播放sFail

sCurrent = "失败"

案例“打架”

玩sFight

sCurrent = "fight "

案例“开始”

播放开始

sCurrent = "start "

案例“赢”

玩sWin

sCurrent = "win "

结束选择

末端接头

public Sub build String(ResFolder As String)

sFail = ResFolder & amp" failv6.mp3 "

sWin = ResFolder & amp" winv6.mp3 "

sStart = ResFolder & amp" startv6.mp3 "

sFight = ResFolder & amp" fightv6.mp3 "

末端接头

私有子类_Terminate()

mciSendString "关闭媒体",vbNullString,0,0

末端接头

公共子停止点()

mciSendString "关闭媒体",vbNullString,0,0

末端接头