Option Explicit Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long Private Declare Function sndPlaySoundFromMemory Lib "winmm.dll" Alias "sndPlaySoundA" (lpszSoundName As Any, ByVal uFlags As Long) As LongPrivate Const SND_ASYNC = &H1& Private Const SND_MEMORY = &H4& Private Const SND_FILENAME = &H20000 Sub PlaySoundFromMem(ByVal ID As Integer) Dim bArr() As Byte bArr = LoadResData(ID, "MUSIC") sndPlaySoundFromMemory bArr(0), SND_ASYNC Or SND_MEMORY End Sub '资源文件里,把WAVE文件加到"MUSIC"类别下,默认为"CUSTOM"
看这里: http://www.thevbzone.com/l_res.htm
我是说不是它本身的资源文件里的,是另一个exe里的文件。
//我是说不是它本身的资源文件里的,是另一个exe里的文件再说一次,看这里: http://www.thevbzone.com/l_res.htm主要是最后面的一段,我给你贴出来: Option ExplicitPrivate Type BITMAP bmType As Long 'LONG bmWidth As Long 'LONG bmHeight As Long 'LONG bmWidthBytes As Long 'LONG bmPlanes As Integer 'WORD bmBitsPixel As Integer 'WORD bmBits As Long 'LPVOID End TypePrivate Const WM_SETICON = &H80 Private Const ICON_BIG = 1Private Const SND_APPLICATION = &H80 Private Const SND_ALIAS = &H10000 Private Const SND_ALIAS_ID = &H110000 Private Const SND_ASYNC = &H1 Private Const SND_FILENAME = &H20000 Private Const SND_LOOP = &H8 Private Const SND_MEMORY = &H4 Private Const SND_NODEFAULT = &H2 Private Const SND_NOSTOP = &H10 Private Const SND_NOWAIT = &H2000 Private Const SND_PURGE = &H40 Private Const SND_RESOURCE = &H40004 Private Const SND_SYNC = &H0Private Declare Function FindResource Lib "KERNEL32" Alias "FindResourceA" (ByVal hLib As Long, _ ByVal strName As String, ByVal strType As String) As Long Private Declare Function FreeLibrary Lib "KERNEL32" (ByVal hLib As Long) As Long 'BOOL Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" ( _ ByVal strFilePath As String) As Long Private Declare Function LoadBitmap Lib "USER32" Alias "LoadBitmapA" (ByVal hInstance As Long, _ ByVal lngBitmapID As Long) As Long Private Declare Function LoadCursor Lib "USER32" Alias "LoadCursorA" (ByVal hLib As Long, _ ByVal lngCursorID As Long) As Long Private Declare Function LoadIcon Lib "USER32" Alias "LoadIconA" (ByVal hLib As Long, _ ByVal lngIconID As Long) As Long Private Declare Function LoadString Lib "USER32" Alias "LoadStringA" (ByVal hLib As Long, _ ByVal ResourceID As Long, ByVal lpBuffer As String, ByVal nBufferSize As Long) As Long Private Declare Function LoadResource Lib "KERNEL32" (ByVal hLib As Long, _ ByVal hRes As Long) As Long Private Declare Function LockResource Lib "KERNEL32" (ByVal hRes As Long) As Long Private Declare Function SizeofResource Lib "KERNEL32" (ByVal hModule As Long, _ ByVal hResInfo As Long) As Long Private Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" (ByRef Sound As Any, _ ByVal hLib As Long, ByVal lngFlag As Long) As Long 'BOOL Private Declare Function SendMessage Lib "USER32.DLL" Alias "SendMessageA" (ByVal hWnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long Private Declare Function SetCursor Lib "USER32.DLL" (ByVal hCursor As Long) As Long Private Declare Function BitBlt Lib "GDI32" (ByVal hDC_Destination As Long, _ ByVal X_Dest As Long, ByVal Y_Dest As Long, ByVal Width_Dest As Long, _ ByVal Height_Dest As Long, ByVal hDC_Source As Long, ByVal X_Src As Long, _ ByVal Y_Src As Long, ByVal RasterOperation As Long) As Long Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hGDIObj As Long) As Long Private Declare Function DeleteObject Lib "GDI32" (ByVal hGDIObj As Long) As Long Private Declare Function GetObjectAPI Lib "GDI32" Alias "GetObjectA" (ByVal hObject As Long, _ ByVal nCount As Long, lpObject As Any) As Long Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As LongPrivate Sub Form_Load()
Const FILE_NAME As String = "Project1.dll"
Dim strFilePath As String Dim hLibrary As Long Dim hResource As Long Dim hData As Long Dim lpData As Long Dim hIcon As Long Dim hCursor As Long Dim hBitmap As Long Dim strString As String Dim lngStringLen As Long Dim BitmapInfo As BITMAP Dim hDC_Screen As Long Dim hDC_Temp As Long Dim hBMP_Prev As Long
Me.Show Me.AutoRedraw = True
' Get the path to the Resource DLL strFilePath = App.Path If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\" strFilePath = strFilePath & FILE_NAME
' Load the Resource DLL hLibrary = LoadLibrary(strFilePath & Chr(0)) If hLibrary = 0 Then MsgBox "Failed to load the specified library with error code " & Err.LastDllError Exit Sub End If
' Get an icon from the Resource DLL hIcon = LoadIcon(hLibrary, 101) If hIcon <> 0 Then SendMessage Me.hWnd, WM_SETICON, ICON_BIG, ByVal hIcon
' Get a cursor from the Resource DLL hCursor = LoadCursor(hLibrary, 101) If hCursor <> 0 Then SetCursor hCursor
' Get a string from the Resource DLL strString = String(256, Chr(0)) lngStringLen = LoadString(hLibrary, 101, strString, Len(strString)) If lngStringLen <> 0 Then Me.Caption = Left(strString, lngStringLen)
' Get a bitmap from the Resource DLL hBitmap = LoadBitmap(hLibrary, 101) If hBitmap <> 0 Then GetObjectAPI hBitmap, Len(BitmapInfo), BitmapInfo hDC_Screen = GetDC(0) hDC_Temp = CreateCompatibleDC(hDC_Screen) hBMP_Prev = SelectObject(hDC_Temp, hBitmap) BitBlt Me.hDC, 0, 0, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hDC_Temp, 0, 0, vbSrcCopy Me.Refresh SelectObject hDC_Temp, hBMP_Prev DeleteDC hDC_Temp ReleaseDC 0, hDC_Screen End If
' Get a .WAV file from the Resource DLL hResource = FindResource(hLibrary, "#" & CStr(1) & Chr(0), "WAVE" & Chr(0)) If hResource <> 0 Then hData = LoadResource(hLibrary, hResource) 'This gets a handle to the data If hData <> 0 Then lpData = LockResource(hData) 'This gets a POINTER to the data... which is what we need If lpData <> 0 Then PlaySound ByVal lpData, 0, SND_MEMORY Or SND_NODEFAULT Or SND_SYNC End If End If End If
如果是直接打开另一个WAV文件则用shell就可以办到:Shell wavFilePath,vbNormalFocusShell详细用法参照MSDN
该怎么写呢?
Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Private Declare Function sndPlaySoundFromMemory Lib "winmm.dll" Alias "sndPlaySoundA" (lpszSoundName As Any, ByVal uFlags As Long) As LongPrivate Const SND_ASYNC = &H1&
Private Const SND_MEMORY = &H4&
Private Const SND_FILENAME = &H20000
Sub PlaySoundFromMem(ByVal ID As Integer)
Dim bArr() As Byte
bArr = LoadResData(ID, "MUSIC")
sndPlaySoundFromMemory bArr(0), SND_ASYNC Or SND_MEMORY
End Sub
'资源文件里,把WAVE文件加到"MUSIC"类别下,默认为"CUSTOM"
http://www.thevbzone.com/l_res.htm
http://www.thevbzone.com/l_res.htm主要是最后面的一段,我给你贴出来:
Option ExplicitPrivate Type BITMAP
bmType As Long 'LONG
bmWidth As Long 'LONG
bmHeight As Long 'LONG
bmWidthBytes As Long 'LONG
bmPlanes As Integer 'WORD
bmBitsPixel As Integer 'WORD
bmBits As Long 'LPVOID
End TypePrivate Const WM_SETICON = &H80
Private Const ICON_BIG = 1Private Const SND_APPLICATION = &H80
Private Const SND_ALIAS = &H10000
Private Const SND_ALIAS_ID = &H110000
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Const SND_LOOP = &H8
Private Const SND_MEMORY = &H4
Private Const SND_NODEFAULT = &H2
Private Const SND_NOSTOP = &H10
Private Const SND_NOWAIT = &H2000
Private Const SND_PURGE = &H40
Private Const SND_RESOURCE = &H40004
Private Const SND_SYNC = &H0Private Declare Function FindResource Lib "KERNEL32" Alias "FindResourceA" (ByVal hLib As Long, _
ByVal strName As String, ByVal strType As String) As Long
Private Declare Function FreeLibrary Lib "KERNEL32" (ByVal hLib As Long) As Long 'BOOL
Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" ( _
ByVal strFilePath As String) As Long
Private Declare Function LoadBitmap Lib "USER32" Alias "LoadBitmapA" (ByVal hInstance As Long, _
ByVal lngBitmapID As Long) As Long
Private Declare Function LoadCursor Lib "USER32" Alias "LoadCursorA" (ByVal hLib As Long, _
ByVal lngCursorID As Long) As Long
Private Declare Function LoadIcon Lib "USER32" Alias "LoadIconA" (ByVal hLib As Long, _
ByVal lngIconID As Long) As Long
Private Declare Function LoadString Lib "USER32" Alias "LoadStringA" (ByVal hLib As Long, _
ByVal ResourceID As Long, ByVal lpBuffer As String, ByVal nBufferSize As Long) As Long
Private Declare Function LoadResource Lib "KERNEL32" (ByVal hLib As Long, _
ByVal hRes As Long) As Long
Private Declare Function LockResource Lib "KERNEL32" (ByVal hRes As Long) As Long
Private Declare Function SizeofResource Lib "KERNEL32" (ByVal hModule As Long, _
ByVal hResInfo As Long) As Long
Private Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" (ByRef Sound As Any, _
ByVal hLib As Long, ByVal lngFlag As Long) As Long 'BOOL
Private Declare Function SendMessage Lib "USER32.DLL" Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function SetCursor Lib "USER32.DLL" (ByVal hCursor As Long) As Long
Private Declare Function BitBlt Lib "GDI32" (ByVal hDC_Destination As Long, _
ByVal X_Dest As Long, ByVal Y_Dest As Long, ByVal Width_Dest As Long, _
ByVal Height_Dest As Long, ByVal hDC_Source As Long, ByVal X_Src As Long, _
ByVal Y_Src As Long, ByVal RasterOperation As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hGDIObj As Long) As Long
Private Declare Function DeleteObject Lib "GDI32" (ByVal hGDIObj As Long) As Long
Private Declare Function GetObjectAPI Lib "GDI32" Alias "GetObjectA" (ByVal hObject As Long, _
ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As LongPrivate Sub Form_Load()
Const FILE_NAME As String = "Project1.dll"
Dim strFilePath As String
Dim hLibrary As Long
Dim hResource As Long
Dim hData As Long
Dim lpData As Long
Dim hIcon As Long
Dim hCursor As Long
Dim hBitmap As Long
Dim strString As String
Dim lngStringLen As Long
Dim BitmapInfo As BITMAP
Dim hDC_Screen As Long
Dim hDC_Temp As Long
Dim hBMP_Prev As Long
Me.Show
Me.AutoRedraw = True
' Get the path to the Resource DLL
strFilePath = App.Path
If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
strFilePath = strFilePath & FILE_NAME
' Load the Resource DLL
hLibrary = LoadLibrary(strFilePath & Chr(0))
If hLibrary = 0 Then
MsgBox "Failed to load the specified library with error code " & Err.LastDllError
Exit Sub
End If
' Get an icon from the Resource DLL
hIcon = LoadIcon(hLibrary, 101)
If hIcon <> 0 Then SendMessage Me.hWnd, WM_SETICON, ICON_BIG, ByVal hIcon
' Get a cursor from the Resource DLL
hCursor = LoadCursor(hLibrary, 101)
If hCursor <> 0 Then SetCursor hCursor
' Get a string from the Resource DLL
strString = String(256, Chr(0))
lngStringLen = LoadString(hLibrary, 101, strString, Len(strString))
If lngStringLen <> 0 Then Me.Caption = Left(strString, lngStringLen)
' Get a bitmap from the Resource DLL
hBitmap = LoadBitmap(hLibrary, 101)
If hBitmap <> 0 Then
GetObjectAPI hBitmap, Len(BitmapInfo), BitmapInfo
hDC_Screen = GetDC(0)
hDC_Temp = CreateCompatibleDC(hDC_Screen)
hBMP_Prev = SelectObject(hDC_Temp, hBitmap)
BitBlt Me.hDC, 0, 0, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hDC_Temp, 0, 0, vbSrcCopy
Me.Refresh
SelectObject hDC_Temp, hBMP_Prev
DeleteDC hDC_Temp
ReleaseDC 0, hDC_Screen
End If
' Get a .WAV file from the Resource DLL
hResource = FindResource(hLibrary, "#" & CStr(1) & Chr(0), "WAVE" & Chr(0))
If hResource <> 0 Then
hData = LoadResource(hLibrary, hResource) 'This gets a handle to the data
If hData <> 0 Then
lpData = LockResource(hData) 'This gets a POINTER to the data... which is what we need
If lpData <> 0 Then
PlaySound ByVal lpData, 0, SND_MEMORY Or SND_NODEFAULT Or SND_SYNC
End If
End If
End If
' Close the Resource DLL
FreeLibrary hLibrary
End Sub