VB获取托盘图标提示文字

Option Explicit

Private Const TBSTATE_HIDDEN = &H8
Private Const MEM_COMMIT As Long = &H1000
Private Const PAGE_READWRITE As Long = &H4

Private Const WM_USER = &H400
Private Const TB_GETBUTTON As Long = (WM_USER + 23)
Private Const TB_BUTTONCOUNT As Long = (WM_USER + 24)

Private Const SYNCHRONIZE As Long = &H100000
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)

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 Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

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 OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, lpBaseAddress As Any, 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 Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
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 Sub Command1_Click()
    Dim lpRect As RECT
    Dim lpHwnd As Long
    Dim lpHeight As Long
   
    Dim lTrayPid As Long
    Dim lCount   As Long
    Dim lret     As Long
    Dim hProcess As Long
    Dim lAddress As Long
    Dim udtTb    As TBBUTTON
    Dim asTip(0 To 1024) As Byte
    Dim sTip    As String
    Dim i       As Integer
   
    lpHwnd = FindWindow("Shell_TrayWnd", vbNullString) '任务栏句柄
    If lpHwnd <> 0 Then
        lpHwnd = FindWindowEx(lpHwnd, 0, "TrayNotifyWnd", vbNullString)
        lpHwnd = FindWindowEx(lpHwnd, 0, "SysPager", vbNullString)
        If lpHwnd <> 0 Then
            lpHwnd = FindWindowEx(lpHwnd, 0, "ToolbarWindow32", vbNullString) '托盘句柄
        End If
    End If
   
    lret = GetWindowThreadProcessId(lpHwnd, lTrayPid)
    lCount = SendMessage(lpHwnd, TB_BUTTONCOUNT, 0, ByVal 0&)   '获取托盘图标个数
   
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, lTrayPid)
    lAddress = VirtualAllocEx(hProcess, ByVal 0&, ByVal 4096&, MEM_COMMIT, PAGE_READWRITE)
   
    For i = 0 To lCount - 1
        lret = SendMessage(lpHwnd, TB_GETBUTTON, ByVal i, ByVal lAddress)
        lret = ReadProcessMemory(hProcess, ByVal lAddress, ByVal VarPtr(udtTb), ByVal Len(udtTb), ByVal 0&)
       
        If Not CBool((udtTb.fsState And TBSTATE_HIDDEN)) Then
            lret = ReadProcessMemory(hProcess, ByVal udtTb.iString, ByVal VarPtr(asTip(0)), ByVal 1024, ByVal 0&)
            sTip = asTip
        Else
            sTip = "[Hidden Icon]"
        End If
        Print Left(sTip, InStr(1, sTip, Chr(0)) - 1)
    Next i
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值