Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Const MEM_COMMIT = &H1000 Private Const MEM_DECOMMIT = &H4000Private Const PAGE_READWRITE = &H4&Private Const WM_USER As Long = &H400 Private Const TB_GETBUTTON As Long = (WM_USER + 23) Private Const TB_ENABLEBUTTON As Long = (WM_USER + 1)Private Type TBBUTTON iBitmap As Long idCommand As Long fsState As Byte fsStyle As Byte bReserved(1) As Byte dwData As Long iString As Long End TypePrivate Function MAKELONG(ByVal a As Long, ByVal b As Long) As Long MAKELONG = (b And &HFFFF&) * &H10000 + (a And &HFFFF&) End FunctionFunction EnableTBButtion(ByVal hTBWnd As Long, ByVal lIndex As Long, Optional fEnabled As Boolean = True, Optional fIsIndex As Boolean = True) As Long Dim utBtn As TBBUTTON Dim lButtonID As Long Dim lpRemote_utBtn As Long Dim lProcessId As Long, hProcess As Long Call GetWindowThreadProcessId(hTBWnd, lProcessId)
If fIsIndex Then hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lProcessId) If hProcess <> 0 Then lpRemote_utBtn = VirtualAllocEx(ByVal hProcess, ByVal 0&, Len(utBtn), MEM_COMMIT, PAGE_READWRITE) Call SendMessage(hTBWnd, TB_GETBUTTON, lIndex, ByVal lpRemote_utBtn) Call ReadProcessMemory(ByVal hProcess, ByVal lpRemote_utBtn, utBtn, Len(utBtn), ByVal 0) Call VirtualFreeEx(hProcess, ByVal lpRemote_utBtn, 0, MEM_DECOMMIT) End If CloseHandle hProcess lButtonID = utBtn.idCommand Else lButtonID = lIndex End If
EnableTBButtion = SendMessage(hTBWnd, TB_ENABLEBUTTON, lButtonID, ByVal MAKELONG(Abs(fEnabled), 0)) End FunctionPrivate Sub Command2_Click() '禁用第3个按钮 Debug.Print EnableTBButtion(&H17104A4, 3 - 1, False) End SubPrivate Sub Command1_Click() '启用第3个按钮 Debug.Print EnableTBButtion(&H17104A4, 3 - 1, True) End Sub
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Const MEM_COMMIT = &H1000
Private Const MEM_DECOMMIT = &H4000Private Const PAGE_READWRITE = &H4&Private Const WM_USER As Long = &H400
Private Const TB_GETBUTTON As Long = (WM_USER + 23)
Private Const TB_ENABLEBUTTON As Long = (WM_USER + 1)Private Type TBBUTTON
iBitmap As Long
idCommand As Long
fsState As Byte
fsStyle As Byte
bReserved(1) As Byte
dwData As Long
iString As Long
End TypePrivate Function MAKELONG(ByVal a As Long, ByVal b As Long) As Long
MAKELONG = (b And &HFFFF&) * &H10000 + (a And &HFFFF&)
End FunctionFunction EnableTBButtion(ByVal hTBWnd As Long, ByVal lIndex As Long, Optional fEnabled As Boolean = True, Optional fIsIndex As Boolean = True) As Long
Dim utBtn As TBBUTTON
Dim lButtonID As Long
Dim lpRemote_utBtn As Long
Dim lProcessId As Long, hProcess As Long
Call GetWindowThreadProcessId(hTBWnd, lProcessId)
If fIsIndex Then
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lProcessId)
If hProcess <> 0 Then
lpRemote_utBtn = VirtualAllocEx(ByVal hProcess, ByVal 0&, Len(utBtn), MEM_COMMIT, PAGE_READWRITE)
Call SendMessage(hTBWnd, TB_GETBUTTON, lIndex, ByVal lpRemote_utBtn)
Call ReadProcessMemory(ByVal hProcess, ByVal lpRemote_utBtn, utBtn, Len(utBtn), ByVal 0)
Call VirtualFreeEx(hProcess, ByVal lpRemote_utBtn, 0, MEM_DECOMMIT)
End If
CloseHandle hProcess
lButtonID = utBtn.idCommand
Else
lButtonID = lIndex
End If
EnableTBButtion = SendMessage(hTBWnd, TB_ENABLEBUTTON, lButtonID, ByVal MAKELONG(Abs(fEnabled), 0))
End FunctionPrivate Sub Command2_Click()
'禁用第3个按钮
Debug.Print EnableTBButtion(&H17104A4, 3 - 1, False)
End SubPrivate Sub Command1_Click()
'启用第3个按钮
Debug.Print EnableTBButtion(&H17104A4, 3 - 1, True)
End Sub