模块 Private Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, lpdwVolume As Long) As Long Private Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long Private Declare Function waveOutGetVolume Lib "winmm.dll" ( _ ByVal uDeviceID As Long, _ lpdwVolume As Long _ ) As Long Private Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Type VolumeSetting LeftVol As Integer RightVol As Integer End TypePublic Const HIGHEST_VOLUME_SETTING = 12 Public Const WAVE_MAPPER = -1& '下面是获取音量的函数: Public Function lGetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long Dim bReturnValue As Boolean Dim Volume As VolumeSetting Dim lAPIReturnVal As Long Dim lBothVolumes As Long lAPIReturnVal = waveOutGetVolume(lDeviceID, lBothVolumes) lDataLen = Len(Volume) CopyMemory Volume.LeftVol, lBothVolumes, lDataLen lLeftVol = HIGHEST_VOLUME_SETTING * lUnsigned(Volume.LeftVol) / 65535 lRightVol = HIGHEST_VOLUME_SETTING * lUnsigned(Volume.RightVol) / 65535 lGetVolume = lAPIReturnVal End Function'下面是设置音量的函数: Public Function lSetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long Dim bReturnValue As Boolean Dim Volume As VolumeSetting Dim lAPIReturnVal As Long Dim lBothVolumes As Long Volume.LeftVol = nSigned(lLeftVol * 65535 / HIGHEST_VOLUME_SETTING) Volume.RightVol = nSigned(lRightVol * 65535 / HIGHEST_VOLUME_SETTING) lDataLen = Len(Volume) CopyMemory lBothVolumes, Volume.LeftVol, lDataLen lAPIReturnVal = waveOutSetVolume(lDeviceID, lBothVolumes) lSetVolume = lAPIReturnVal End Function'** -> * 转换函数 Public Function nSigned(ByVal lUnsignedInt As Long) As Integer Dim nReturnVal As Integer If lUnsignedInt > 65535 Or lUnsignedInt < 0 Then MsgBox "Error in conversion from Unsigned to nSigned Integer" nSignedInt = 0 Exit Function End If If lUnsignedInt > 32767 Then nReturnVal = lUnsignedInt - 65536 Else nReturnVal = lUnsignedInt End If nSigned = nReturnVal End Function '转换函数 Public Function lUnsigned(ByVal nSignedInt As Integer) As Long Dim lReturnVal As Long If nSignedInt < 0 Then lReturnVal = nSignedInt + 65536 Else lReturnVal = nSignedInt End IfIf lReturnVal > 65535 Or lReturnVal < 0 Then MsgBox "Error in conversion from nSigned to Unsigned Integer" lReturnVal = 0 End If lUnsigned = lReturnValEnd Function 左 Dim L As Long, lleft As Long, lright As Long lleft = lUnsigned(0) lright = lUnsigned(HIGHEST_VOLUME_SETTING)L = lSetVolume(lleft, lright, WAVE_MAPPER) 右 Dim L As Long, lleft As Long, lright As Long lleft = lUnsigned(HIGHEST_VOLUME_SETTING) lright = lUnsigned(0) L = lSetVolume(lleft, lright, WAVE_MAPPER) 混合 Dim L As Long, lleft As Long, lright As Long lleft = lUnsigned(12) lright = lUnsigned(12) L = lSetVolume(lleft, lright, WAVE_MAPPER)
Private Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
Private Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long
Private Declare Function waveOutGetVolume Lib "winmm.dll" ( _
ByVal uDeviceID As Long, _
lpdwVolume As Long _
) As Long
Private Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Type VolumeSetting
LeftVol As Integer
RightVol As Integer
End TypePublic Const HIGHEST_VOLUME_SETTING = 12
Public Const WAVE_MAPPER = -1&
'下面是获取音量的函数:
Public Function lGetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long
Dim bReturnValue As Boolean
Dim Volume As VolumeSetting
Dim lAPIReturnVal As Long
Dim lBothVolumes As Long
lAPIReturnVal = waveOutGetVolume(lDeviceID, lBothVolumes)
lDataLen = Len(Volume)
CopyMemory Volume.LeftVol, lBothVolumes, lDataLen
lLeftVol = HIGHEST_VOLUME_SETTING * lUnsigned(Volume.LeftVol) / 65535
lRightVol = HIGHEST_VOLUME_SETTING * lUnsigned(Volume.RightVol) / 65535
lGetVolume = lAPIReturnVal
End Function'下面是设置音量的函数:
Public Function lSetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long
Dim bReturnValue As Boolean
Dim Volume As VolumeSetting
Dim lAPIReturnVal As Long
Dim lBothVolumes As Long
Volume.LeftVol = nSigned(lLeftVol * 65535 / HIGHEST_VOLUME_SETTING)
Volume.RightVol = nSigned(lRightVol * 65535 / HIGHEST_VOLUME_SETTING)
lDataLen = Len(Volume)
CopyMemory lBothVolumes, Volume.LeftVol, lDataLen
lAPIReturnVal = waveOutSetVolume(lDeviceID, lBothVolumes)
lSetVolume = lAPIReturnVal
End Function'** -> * 转换函数
Public Function nSigned(ByVal lUnsignedInt As Long) As Integer
Dim nReturnVal As Integer
If lUnsignedInt > 65535 Or lUnsignedInt < 0 Then
MsgBox "Error in conversion from Unsigned to nSigned Integer"
nSignedInt = 0
Exit Function
End If
If lUnsignedInt > 32767 Then
nReturnVal = lUnsignedInt - 65536
Else
nReturnVal = lUnsignedInt
End If
nSigned = nReturnVal
End Function
'转换函数
Public Function lUnsigned(ByVal nSignedInt As Integer) As Long
Dim lReturnVal As Long
If nSignedInt < 0 Then
lReturnVal = nSignedInt + 65536
Else
lReturnVal = nSignedInt
End IfIf lReturnVal > 65535 Or lReturnVal < 0 Then
MsgBox "Error in conversion from nSigned to Unsigned Integer"
lReturnVal = 0
End If
lUnsigned = lReturnValEnd Function
左
Dim L As Long, lleft As Long, lright As Long
lleft = lUnsigned(0)
lright = lUnsigned(HIGHEST_VOLUME_SETTING)L = lSetVolume(lleft, lright, WAVE_MAPPER)
右
Dim L As Long, lleft As Long, lright As Long lleft = lUnsigned(HIGHEST_VOLUME_SETTING)
lright = lUnsigned(0)
L = lSetVolume(lleft, lright, WAVE_MAPPER)
混合
Dim L As Long, lleft As Long, lright As Long lleft = lUnsigned(12)
lright = lUnsigned(12)
L = lSetVolume(lleft, lright, WAVE_MAPPER)