Access VBA 使用API 关闭系统声音和打开系统声音的 方法

2017-09-08 09:30:00
zstmtony
原创
278

Access VBA 使用API 关闭系统声音和打开系统声音的 方法

即让整个系统静音及取消静音的代码如下



------VBA 实现 源码--------------------
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hwnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const BM_GETCHECK = &HF0      ' 取得复选框状态
Private Const BM_SETCHECK = &HF1      '设置复选框状态
' Private Const BST_UNCHECKED = &00      设置复选框为未选中状态
' Private Const BST_CHECKED = &01           设置复选框为选中状态
Private Sub Command1_Click()
     Dim hwnd0 As Long      ' 用于记录“音量控制”程序的句柄
     Shell "sndvol32.exe"        ' 启动“音量控制”程序
     hwnd0 = FindWindow(vbNullString, "主音量")
     Dim hwnd1 As Long     ' 用于记录“全部静音”复选框句柄
     hwnd1 = FindWindowEx(hwnd0, 0&, "Button", "全部静音(&M)")   ' 复选框的类名是"Button"
     Dim State As Long       ' 用于记录复选框状态,如果复选框处于未选中状态,则返回0,选中状态返回1
     State = SendMessage(hwnd1, BM_GETCHECK, ByVal CLng(0), ByVal CLng(0))
     SendMessage hwnd1, BM_SETCHECK, 1, 0 '使系统静音
     SendMessage hwnd1, BM_SETCHECK, 0, 0 '使系统发音
End Sub
分享