下面代码导致程序未响应,请修改下面代码:Private Declare Function SendMessageW Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Const WM_APPCOMMAND = &H319 Const APPCOMMAND_VOLUME_UP = &HA Const APPCOMMAND_VOLUME_DOWN = &H9 Const APPCOMMAND_VOLUME_MUTE = &H8 Const HWND_BROADCAST = &HFFFF& Public Sub SetSystemVolume(ByVal level As Integer) Dim command As Long '设置音量大小 command = ((level And &HFFFF&) Or ((APPCOMMAND_VOLUME_UP * &H10000) And &HFFFF0000)) SendMessageW HWND_BROADCAST, WM_APPCOMMAND, 0, command End Sub Public Sub MuteSystemVolume() '静音 SendMessageW HWND_BROADCAST, WM_APPCOMMAND, 0, (APPCOMMAND_VOLUME_MUTE * &H10000) End Sub
时间: 2023-06-18 15:02:36 浏览: 71
请尝试将代码中的 `SendMessageW` 函数改为 `SendMessage` 函数,修改后的代码如下:
```
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_APPCOMMAND = &H319
Const APPCOMMAND_VOLUME_UP = &HA
Const APPCOMMAND_VOLUME_DOWN = &H9
Const APPCOMMAND_VOLUME_MUTE = &H8
Const HWND_BROADCAST = &HFFFF&
Public Sub SetSystemVolume(ByVal level As Integer)
Dim command As Long
command = ((level And &HFFFF&) Or ((APPCOMMAND_VOLUME_UP * &H10000) And &HFFFF0000))
SendMessage HWND_BROADCAST, WM_APPCOMMAND, 0, ByVal command
End Sub
Public Sub MuteSystemVolume()
SendMessage HWND_BROADCAST, WM_APPCOMMAND, 0, ByVal (APPCOMMAND_VOLUME_MUTE * &H10000)
End Sub
```
这里使用了 `Alias` 关键字来指定 `SendMessage` 函数的别名为 `SendMessageA`,这样可以避免在 64 位系统下出现问题。同时,将 `lParam` 参数的类型改为 `Any`,并使用 `ByVal` 关键字传递参数,也可以避免程序未响应的问题。
阅读全文