以下是引用kings12333在2022-5-24 18:57:25的发言:
按照你说的方法已经测试好了,经测试发现对于类型为:ToolbarWindow32的可以有效,但针对VB ToolBar控件类型 msvb_lib_toolbar 无效,但可以变更按钮状态,不知为何。
不会吧?你试以下代码
Option Explicit
Private 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 dwProcId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_VM_OPERATION = &H8
Private Const PROCESS_VM_READ = &H10
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 Const MEM_COMMIT = &H1000
Private Const PAGE_READWRITE = &H4
Private Const MEM_RELEASE = &H8000&
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
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
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_COMMAND = &H111
Private Type TBBUTTON
   iBitmap
      As Long
   idCommand
    As Long
   fsState
      As Byte
   fsStyle
      As Byte
   bReserved1
   As Byte
   bReserved2
   As Byte
   dwData
       As Long
   iString
      As Long
End Type
Private Const TB_GETBUTTON = &H417
Private Sub Command1_Click()
    Dim hWnd As Long, Index As Long
    
    hWnd = 指定 toolbar 句柄
    Index = 指定 toolbar 索引,范围 = 0 ~ 按钮总数-1
    Call GotoButtonClick(hWnd, Index)
End Sub
Private Sub GotoButtonClick(ByVal hWnd As Long, ByVal Index As Long)
'hWnd
      toolbar 句柄;
'Index
     toolbar 索引,范围 = 0 ~ 按钮总数-1;
    Dim hParent As Long, ButtonID As Long
  
    hParent = GetParent(hWnd)
    ButtonID = GetButtonID(hWnd, Index)
    Call PostMessage(hParent, WM_COMMAND, ButtonID, ByVal hWnd)
End Sub
Private Function GetButtonID(ByVal hWnd As Long, ByVal Index As Long) As Long
    Dim ProcesshWnd As Long, ProcessId As Long, Buffer As Long, Len1 As Long
    Dim iButton
     As TBBUTTON
    
    Len1 = LenB(iButton)
    Call GetWindowThreadProcessId(hWnd, ProcessId)
    ProcesshWnd = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ, 0, ProcessId)
    Buffer = VirtualAllocEx(ProcesshWnd, 0, Len1, MEM_COMMIT, PAGE_READWRITE)
    
    Call SendMessage(hWnd, TB_GETBUTTON, Index, ByVal Buffer)
    Call ReadProcessMemory(ProcesshWnd, Buffer, iButton, Len1, 0)
    
    GetButtonID = iButton.idCommand
    Call VirtualFreeEx(ProcesshWnd, Buffer, 0, MEM_RELEASE)
    Call CloseHandle(ProcesshWnd)
End Function