office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access VBA 使用API 關閉繫統聲音和打開繫統聲音的 方法

2017-09-08 09:30:00
zstmtony
原創
4066

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
分享