简化了下代码做了个示例 esc退出Global Const $CLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
Global Const $IID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
Global Const $tagIMMDeviceEnumerator = _
"EnumAudioEndpoints hresult(int;dword;ptr*);" & _
"GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _
"GetDevice hresult(wstr;ptr*);" & _
"RegisterEndpointNotificationCallback hresult(ptr);" & _
"UnregisterEndpointNotificationCallback hresult(ptr)"
Global Const $IID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}"
Global Const $tagIMMDevice = _
"Activate hresult(struct*;dword;ptr;ptr*);" & _
"OpenPropertyStore hresult(dword;ptr*);" & _
"GetId hresult(wstr*);" & _
"GetState hresult(dword*)"
Global Const $eRender = 0, $eConsole = 0
Global Const $IID_IAudioEndpointVolume = "{5CDF2C82-841E-4546-9722-0CF74078229A}"
Global Const $tagIAudioEndpointVolume = _
"RegisterControlChangeNotify hresult(ptr);" & _
"UnregisterControlChangeNotify hresult(ptr);" & _
"GetChannelCount hresult(uint*);" & _
"SetMasterVolumeLevel hresult(float;ptr);" & _
"SetMasterVolumeLevelScalar hresult(float;ptr);" & _
"GetMasterVolumeLevel hresult(float*);" & _
"GetMasterVolumeLevelScalar hresult(float*);" & _
"SetChannelVolumeLevel hresult(uint;float;ptr);" & _
"SetChannelVolumeLevelScalar hresult(uint;float;ptr);" & _
"GetChannelVolumeLevel hresult(uint;float*);" & _
"GetChannelVolumeLevelScalar hresult(uint;float*);" & _
"SetMute hresult(int;ptr);" & _
"GetMute hresult(int*);" & _
"GetVolumeStepInfo hresult(uint*;uint*);" & _
"VolumeStepUp hresult(ptr);" & _
"VolumeStepDown hresult(ptr);" & _
"QueryHardwareSupport hresult(dword*);" & _
"GetVolumeRange hresult(float*;float*;float*)"
Global Const $CLSCTX_INPROC_SERVER = 1
Global $__g_oEndpointVolume = 0
HotKeySet("{esc}","Exitfunc")
do
$s=_GetMasterVolumeLevelScalar()
ToolTip ($s)
Until 0
Func Exitfunc()
Exit 0
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetMasterVolumeLevelScalar
; Description ...: Get master volume level in scalar units, 0.0 to 100.0.
; Syntax ........: _GetMasterVolumeLevelScalar()
; Parameters ....:
; Return values .: Success - Master volume level in scalar units, 0.0 to 100.0 (float)
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetMasterVolumeLevelScalar()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $iVol
If __SUCCEEDED($__g_oEndpointVolume.GetMasterVolumeLevelScalar($iVol)) Then
Return ($iVol * 100)
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
Func _EndpointVolume_Init()
If IsObj($__g_oEndpointVolume) Then Return
$__g_oEndpointVolume = __GetDefaultEndpointVolume()
If @error Then Return SetError(@error)
EndFunc
Func __SUCCEEDED($hr)
Return ($hr >= 0)
EndFunc
Func __GetDefaultEndpointVolume()
Local $oIAudioEndpointVolume = 0, $err = 1
; create device enumerator interface
Local $oDevEnum = ObjCreateInterface($CLSID_MMDeviceEnumerator, $IID_IMMDeviceEnumerator, $tagIMMDeviceEnumerator)
If IsObj($oDevEnum) Then
$err = 2
; get default audio endpoint interface pointer
Local $pDefaultDevice = 0
If __SUCCEEDED($oDevEnum.GetDefaultAudioEndpoint($eRender, $eConsole, $pDefaultDevice)) Then
$err = 3
; create default audio endpoint interface
Local $oIMMDevice = ObjCreateInterface($pDefaultDevice, $IID_IMMDevice, $tagIMMDevice)
If IsObj($oIMMDevice) Then
$err = 4
; get endpoint volume interface pointer
Local $pEndpointVolume = 0
If __SUCCEEDED($oIMMDevice.Activate(__uuidof($IID_IAudioEndpointVolume), $CLSCTX_INPROC_SERVER, 0, $pEndpointVolume)) Then
$err = 5
; create endpoint volume interface
$oIAudioEndpointVolume = ObjCreateInterface($pEndpointVolume, $IID_IAudioEndpointVolume, $tagIAudioEndpointVolume)
EndIf
$oIMMDevice = 0
EndIf
EndIf
$oDevEnum = 0
EndIf
;
If IsObj($oIAudioEndpointVolume) Then
Return $oIAudioEndpointVolume
Else
Return SetError($err, 0, 0)
EndIf
EndFunc
Func __uuidof($sGUID)
Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID)
If @error Then Return SetError(@error, @extended, 0)
Return $tGUID
EndFunc
|