VB:枚举托盘图标

kmlxk 发表于 2005-9-17 1:49:00

文件 Form1.frm 内容

VERSION 5.00 Begin VB.Form Form1 Caption = “Form1” ClientHeight = 3195 ClientLeft = 60 ClientTop = 345 ClientWidth = 4680 LinkTopic = “Form1” ScaleHeight = 3195 ScaleWidth = 4680 StartUpPosition = 3 ‘窗口缺省 Begin VB.CommandButton Command1 Caption = “Command1” Height = 375 Left = 2760 TabIndex = 1 Top = 2280 Width = 1455 End Begin VB.ListBox List1 Height = 1860 Left = 360 TabIndex = 0 Top = 240 Width = 3855 End End Attribute VB_Name = “Form1” Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Private Declare Function OpenProcess Lib “KERNEL32” (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib “KERNEL32” (ByVal hObject As Long) As Long Private Const READ_CONTROL As Long = &H20000; Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000; Private Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL) Private Const STANDARD_RIGHTS_EXECUTE As Long = (READ_CONTROL) Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000; Private Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL) Private Const SYNCHRONIZE As Long = &H100000; Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF;) Private Const PROCESS_TERMINATE As Long = (&H1;) Private Declare Function GetWindowThreadProcessId Lib “user32.dll” (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Const WM_USER As Long = &H400; 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 Const TB_BUTTONCOUNT As Long = (WM_USER + 24) Private Const TB_HIDEBUTTON As Long = (WM_USER + 4) Private Const TB_GETBUTTON As Long = (WM_USER + 23) Private Const TB_GETBITMAP As Long = (WM_USER + 44) Private Const TB_DELETEBUTTON As Long = (WM_USER + 22) Private Const TB_ADDBUTTONS As Long = (WM_USER + 20) Private Const TB_INSERTBUTTON As Long = (WM_USER + 21) Private Const TB_ISBUTTONHIDDEN As Long = (WM_USER + 12) Private Const ILD_NORMAL As Long = &H0; Private Declare Function SendMessage Lib “user32.dll” Alias “SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function WriteProcessMemory 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 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 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 VirtualFreeEx Lib “kernel32.dll” (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long Private Const PROCESS_VM_OPERATION As Long = (&H8;) Private Const PROCESS_VM_READ As Long = (&H10;) Private Const PROCESS_VM_WRITE As Long = (&H20;) Private Const MEM_RESERVE As Long = &H2000; Private Const MEM_COMMIT As Long = &H1000; Private Const MEM_RELEASE As Long = &H8000; Private Const PAGE_READWRITE As Long = &H4;

Private Sub Command1_Click() Dim lngTemp As Long Dim lngTray Dim lngPID As Long Dim lngPID2 As Long Dim hProcess As Long Dim lngProcess As Long Dim lngAddress As Long Dim lngCount As Long Dim lngButtons As Long Dim ret As Long Dim lngTextAdr As Long Dim lngHwndAdr As Long Dim lngButtonID As Long Dim hIcon As Long Dim lngHwnd As Long Dim lpFileName As String * 1024 Dim i As Long Dim strBuff(1024) As Byte Dim strText As String Dim lngTrayDC As Long lngTemp = FindWindow(“Shell_TrayWnd”, vbNullString) lngTemp = FindWindowEx(lngTemp, 0, “TrayNotifyWnd”, vbNullString) lngTemp = FindWindowEx(lngTemp, 0, “SysPager”, vbNullString) lngTray = FindWindowEx(lngTemp, 0, “ToolbarWindow32”, vbNullString) Debug.Print “lngTray=”; lngTray ret = GetWindowThreadProcessId(lngTray, lngPID) hProcess = OpenProcess(PROCESS_ALL_ACCESS Or PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, lngPID) lngAddress = VirtualAllocEx(hProcess, ByVal 0&, ByVal 4096&, MEM_COMMIT, PAGE_READWRITE) lngButtons = SendMessage(lngTray, TB_BUTTONCOUNT, 0, 0)

For i = 0 To lngButtons - 1
    ret = SendMessage(lngTray, TB_GETBUTTON, ByVal i, ByVal lngAddress)
    ret = ReadProcessMemory(hProcess, ByVal lngAddress + 16, ByVal VarPtr(lngTextAdr), ByVal 4, ByVal 0&)
    If lngTextAdr <> -1 Then
        ret = ReadProcessMemory(hProcess, ByVal lngTextAdr, ByVal VarPtr(strBuff(0)), ByVal 1024, ByVal 0&)
        ret = ReadProcessMemory(hProcess, ByVal lngAddress + 12, ByVal VarPtr(lngHwndAdr), ByVal 4, ByVal 0&)
        ret = ReadProcessMemory(hProcess, ByVal lngHwndAdr, ByVal VarPtr(lngHwnd), ByVal 4, ByVal 0&)
        ret = ReadProcessMemory(hProcess, ByVal lngAddress + 4, ByVal VarPtr(lngButtonID), ByVal 4, ByVal 0&)

        strText = ConverNull(strBuff)
        'lngHwnd 所属窗口handle
        'strText 托盘图标文字
        List1.AddItem lngHwnd & " - " & strText
    End If
Next i
VirtualFreeEx hProcess, ByVal lngAddress, ByVal 4096&, MEM_RELEASE
CloseHandle hProcess

End Sub

Private Sub Form_Load() Command1.Caption = “&Enumerate;” End Sub

Function ConverNull(ByVal s As String) As String Dim nullpos As Long nullpos = InStr(s, Chr$(0)) If nullpos > 0 Then ConverNull = Left(s, nullpos - 1) Else ConverNull = s End If End Function

支持原创技术分享,据说打赏我的人,都找到了女朋友!