nop 发表于 2013-4-14 01:39:25

如何获取系统托盘的工具提示ToolTip ?

例如XP中鼠标放在托盘上的任务管理器上的时候显示“CPU 使用 x%”,放在喇叭上就显示“音量”,我需要获取指定进程的托盘提示信息。
使用SysTray_UDF.au3好像对中文支持不太好,经常只显示一部分(例如任务管理器只显示“CPU”,虚拟DAEMON管理器只显示“虚”)。
而GuiToolTip网上讲解的较少,我自己实验失败。


这里附上SysTray_UDF中所用函数的代码
Func _SysTrayIconTooltip($iIndex=0)
;=========================================================
;   Create the struct _TBBUTTON
;   struct {
;        int         iBitmap;
;    int         idCommand;
;    BYTE   fsState;
;    BYTE   fsStyle;
;       
;        #ifdef _WIN64
;    BYTE   bReserved   // padding for alignment
;        #elif defined(_WIN32)
;    BYTE   bReserved   // padding for alignment
;        #endif
;    DWORD_PTR   dwData;
;    INT_PTR          iString;

;   }
;=========================================================
Local $str      = "int;int;byte;byte;byte;dword;int"
Dim $TBBUTTON   = DllStructCreate($str)
Dim $TBBUTTON2   = DllStructCreate($str)
Dim $ExtraData = DllStructCreate("dword")
Dim $intTip = DllStructCreate("short")


Local $pId
Local $text
Local $procHandle
Local $index = $iIndex
Local $bytesRead
Local $info
Local $lpData
Local $trayHwnd


$trayHwnd = _FindTrayToolbarWindow()
If $trayHwnd = -1 Then
        $TBBUTTON = 0
        $TBBUTTON2 = 0
        $ExtraData = 0
        $intTip = 0
        ;SetError(1)
        Return -1
EndIf

Local $ret = DLLCall("user32.dll","int","GetWindowThreadProcessId", "hwnd", $trayHwnd, "int*", -1)
If Not @error Then
                $pId = $ret
Else
        ConsoleWrite("Error: Could not find toolbar process id, " & @error & @LF)
        $TBBUTTON = 0
        $TBBUTTON2 = 0
        $ExtraData = 0
        $intTip = 0
        Return -1
EndIf
$procHandle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', $PROCESS_ALL_ACCESS, 'int', False, 'int', $pId)
If @error Then
        ConsoleWrite("Error: Could not read toolbar process memory, " & @error & @LF)
        $TBBUTTON = 0
        $TBBUTTON2 = 0
        $ExtraData = 0
        $intTip = 0
        return -1
EndIf

$lpData = DLLCall("kernel32.dll","int","VirtualAllocEx", "int", $procHandle, "int", 0, "int", DllStructGetSize ( $TBBUTTON ), "int", 0x1000, "int", 0x04)
If @error Then
        ConsoleWrite("VirtualAllocEx Error" & @LF)
        $TBBUTTON = 0
        $TBBUTTON2 = 0
        $ExtraData = 0
        $intTip = 0
        Return -1
Else
        DLLCall("user32.dll","int","SendMessage", "hwnd", $trayHwnd, "int", $TB_GETBUTTON, "int", $index, "ptr",$lpData);e(hwnd, TB_GETBUTTON, index, (LPARAM)lpData);
        DllCall('kernel32.dll', 'int', 'ReadProcessMemory', 'int', $procHandle, 'int', $lpData, 'ptr', DllStructGetPtr($TBBUTTON2), 'int', DllStructGetSize( $TBBUTTON), 'int', $bytesRead)
        DllCall('kernel32.dll', 'int', 'ReadProcessMemory', 'int', $procHandle, 'int', DllStructGetData($TBBUTTON2,7), 'int', DllStructGetPtr($intTip), 'int', DllStructGetSize( $intTip), 'int', 0);_MemRead($procHandle, $lpData, DllStructGetSize( $TBBUTTON))

        ; go through every character
        $i = 1
        While $i < 1024
                $tipChar = ""
       
#cs       
                BOOL ReadProcessMemory(
HANDLE hProcess,
LPCVOID lpBaseAddress,
LPVOID lpBuffer,
SIZE_T nSize,
SIZE_T* lpNumberOfBytesRead
);
#ce

               
                $tipChar = Chr(DllStructGetData($intTip,1,$i))

                If $tipChar = "" Then
                        ExitLoop
                EndIf
                ;ConsoleWrite(@CRLF & $i & " Char: " & $tipChar & @LF)
                $info =$info & $tipChar

                $i = $i + 1
        Wend
       
        If $info = "" Then
                $info = "No tooltip text"
        EndIf
       
        DLLCall("kernel32.dll","int","VirtualFreeEx", "int", $procHandle, "ptr", $lpData, "int", 0, "int", 0x8000) ;DllStructGetSize ( $TBBUTTON ), "int", 0x8000))
EndIf

DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $procHandle)       
$TBBUTTON = 0
$TBBUTTON2 = 0
$ExtraData = 0
$intTip = 0
$lpData = 0
return $info
EndFunc
页: [1]
查看完整版本: 如何获取系统托盘的工具提示ToolTip ?